blob: 2c48591ced444530448b01c1881109da4acc34d5 [file] [log] [blame]
Frank Blaschka4a71df52008-02-15 09:19:42 +01001/*
2 * drivers/s390/net/qeth_l2_main.c
3 *
4 * Copyright IBM Corp. 2007
5 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6 * Frank Pavlic <fpavlic@de.ibm.com>,
7 * Thomas Spatzier <tspat@de.ibm.com>,
8 * Frank Blaschka <frank.blaschka@de.ibm.com>
9 */
10
Frank Blaschka74eacdb2008-12-25 13:39:49 +010011#define KMSG_COMPONENT "qeth"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Frank Blaschka4a71df52008-02-15 09:19:42 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/string.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/etherdevice.h>
20#include <linux/mii.h>
21#include <linux/ip.h>
22
23#include <asm/s390_rdev.h>
24
25#include "qeth_core.h"
26#include "qeth_core_offl.h"
27
Frank Blaschka4a71df52008-02-15 09:19:42 +010028static int qeth_l2_set_offline(struct ccwgroup_device *);
29static int qeth_l2_stop(struct net_device *);
30static int qeth_l2_send_delmac(struct qeth_card *, __u8 *);
31static int qeth_l2_send_setdelmac(struct qeth_card *, __u8 *,
32 enum qeth_ipa_cmds,
33 int (*reply_cb) (struct qeth_card *,
34 struct qeth_reply*,
35 unsigned long));
36static void qeth_l2_set_multicast_list(struct net_device *);
37static int qeth_l2_recover(void *);
38
39static int qeth_l2_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
40{
Heiko Carstens509e2562008-07-26 02:24:10 -070041 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +010042 struct mii_ioctl_data *mii_data;
43 int rc = 0;
44
45 if (!card)
46 return -ENODEV;
47
48 if ((card->state != CARD_STATE_UP) &&
49 (card->state != CARD_STATE_SOFTSETUP))
50 return -ENODEV;
51
52 if (card->info.type == QETH_CARD_TYPE_OSN)
53 return -EPERM;
54
55 switch (cmd) {
56 case SIOC_QETH_ADP_SET_SNMP_CONTROL:
57 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
58 break;
59 case SIOC_QETH_GET_CARD_TYPE:
60 if ((card->info.type == QETH_CARD_TYPE_OSAE) &&
61 !card->info.guestlan)
62 return 1;
63 return 0;
64 break;
65 case SIOCGMIIPHY:
66 mii_data = if_mii(rq);
67 mii_data->phy_id = 0;
68 break;
69 case SIOCGMIIREG:
70 mii_data = if_mii(rq);
71 if (mii_data->phy_id != 0)
72 rc = -EINVAL;
73 else
74 mii_data->val_out = qeth_mdio_read(dev,
75 mii_data->phy_id, mii_data->reg_num);
76 break;
77 default:
78 rc = -EOPNOTSUPP;
79 }
80 if (rc)
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +020081 QETH_DBF_TEXT_(TRACE, 2, "ioce%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +010082 return rc;
83}
84
85static int qeth_l2_verify_dev(struct net_device *dev)
86{
87 struct qeth_card *card;
88 unsigned long flags;
89 int rc = 0;
90
91 read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
92 list_for_each_entry(card, &qeth_core_card_list.list, list) {
93 if (card->dev == dev) {
94 rc = QETH_REAL_CARD;
95 break;
96 }
97 }
98 read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
99
100 return rc;
101}
102
103static struct net_device *qeth_l2_netdev_by_devno(unsigned char *read_dev_no)
104{
105 struct qeth_card *card;
106 struct net_device *ndev;
Cornelia Huckf06f6f32008-06-06 12:37:45 +0200107 __u16 temp_dev_no;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100108 unsigned long flags;
Cornelia Huckf06f6f32008-06-06 12:37:45 +0200109 struct ccw_dev_id read_devid;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100110
111 ndev = NULL;
112 memcpy(&temp_dev_no, read_dev_no, 2);
113 read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
114 list_for_each_entry(card, &qeth_core_card_list.list, list) {
Cornelia Huckf06f6f32008-06-06 12:37:45 +0200115 ccw_device_get_id(CARD_RDEV(card), &read_devid);
116 if (read_devid.devno == temp_dev_no) {
Frank Blaschka4a71df52008-02-15 09:19:42 +0100117 ndev = card->dev;
118 break;
119 }
120 }
121 read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
122 return ndev;
123}
124
125static int qeth_l2_send_setgroupmac_cb(struct qeth_card *card,
126 struct qeth_reply *reply,
127 unsigned long data)
128{
129 struct qeth_ipa_cmd *cmd;
130 __u8 *mac;
131
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200132 QETH_DBF_TEXT(TRACE, 2, "L2Sgmacb");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100133 cmd = (struct qeth_ipa_cmd *) data;
134 mac = &cmd->data.setdelmac.mac[0];
135 /* MAC already registered, needed in couple/uncouple case */
136 if (cmd->hdr.return_code == 0x2005) {
Johannes Berg7c510e42008-10-27 17:47:26 -0700137 QETH_DBF_MESSAGE(2, "Group MAC %pM already existing on %s \n",
138 mac, QETH_CARD_IFNAME(card));
Frank Blaschka4a71df52008-02-15 09:19:42 +0100139 cmd->hdr.return_code = 0;
140 }
141 if (cmd->hdr.return_code)
Johannes Berg7c510e42008-10-27 17:47:26 -0700142 QETH_DBF_MESSAGE(2, "Could not set group MAC %pM on %s: %x\n",
143 mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100144 return 0;
145}
146
147static int qeth_l2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
148{
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200149 QETH_DBF_TEXT(TRACE, 2, "L2Sgmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100150 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
151 qeth_l2_send_setgroupmac_cb);
152}
153
154static int qeth_l2_send_delgroupmac_cb(struct qeth_card *card,
155 struct qeth_reply *reply,
156 unsigned long data)
157{
158 struct qeth_ipa_cmd *cmd;
159 __u8 *mac;
160
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200161 QETH_DBF_TEXT(TRACE, 2, "L2Dgmacb");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100162 cmd = (struct qeth_ipa_cmd *) data;
163 mac = &cmd->data.setdelmac.mac[0];
164 if (cmd->hdr.return_code)
Johannes Berg7c510e42008-10-27 17:47:26 -0700165 QETH_DBF_MESSAGE(2, "Could not delete group MAC %pM on %s: %x\n",
166 mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100167 return 0;
168}
169
170static int qeth_l2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
171{
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200172 QETH_DBF_TEXT(TRACE, 2, "L2Dgmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100173 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
174 qeth_l2_send_delgroupmac_cb);
175}
176
Frank Blaschka7db22662008-08-15 08:02:59 +0200177static void qeth_l2_add_mc(struct qeth_card *card, __u8 *mac, int vmac)
Frank Blaschka4a71df52008-02-15 09:19:42 +0100178{
179 struct qeth_mc_mac *mc;
Frank Blaschka7db22662008-08-15 08:02:59 +0200180 int rc;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100181
182 mc = kmalloc(sizeof(struct qeth_mc_mac), GFP_ATOMIC);
183
Frank Blaschka14cc21b2008-06-06 12:37:46 +0200184 if (!mc)
Frank Blaschka4a71df52008-02-15 09:19:42 +0100185 return;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100186
187 memcpy(mc->mc_addr, mac, OSA_ADDR_LEN);
188 mc->mc_addrlen = OSA_ADDR_LEN;
Frank Blaschka7db22662008-08-15 08:02:59 +0200189 mc->is_vmac = vmac;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100190
Frank Blaschka7db22662008-08-15 08:02:59 +0200191 if (vmac) {
192 rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
193 NULL);
194 } else {
195 rc = qeth_l2_send_setgroupmac(card, mac);
196 }
197
198 if (!rc)
Frank Blaschka4a71df52008-02-15 09:19:42 +0100199 list_add_tail(&mc->list, &card->mc_list);
200 else
201 kfree(mc);
202}
203
204static void qeth_l2_del_all_mc(struct qeth_card *card)
205{
206 struct qeth_mc_mac *mc, *tmp;
207
208 spin_lock_bh(&card->mclock);
209 list_for_each_entry_safe(mc, tmp, &card->mc_list, list) {
Frank Blaschka7db22662008-08-15 08:02:59 +0200210 if (mc->is_vmac)
211 qeth_l2_send_setdelmac(card, mc->mc_addr,
212 IPA_CMD_DELVMAC, NULL);
213 else
214 qeth_l2_send_delgroupmac(card, mc->mc_addr);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100215 list_del(&mc->list);
216 kfree(mc);
217 }
218 spin_unlock_bh(&card->mclock);
219}
220
221static void qeth_l2_get_packet_type(struct qeth_card *card,
222 struct qeth_hdr *hdr, struct sk_buff *skb)
223{
224 __u16 hdr_mac;
225
226 if (!memcmp(skb->data + QETH_HEADER_SIZE,
227 skb->dev->broadcast, 6)) {
228 /* broadcast? */
229 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
230 return;
231 }
232 hdr_mac = *((__u16 *)skb->data);
233 /* tr multicast? */
234 switch (card->info.link_type) {
235 case QETH_LINK_TYPE_HSTR:
236 case QETH_LINK_TYPE_LANE_TR:
237 if ((hdr_mac == QETH_TR_MAC_NC) ||
238 (hdr_mac == QETH_TR_MAC_C))
239 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
240 else
241 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
242 break;
243 /* eth or so multicast? */
244 default:
245 if ((hdr_mac == QETH_ETH_MAC_V4) ||
246 (hdr_mac == QETH_ETH_MAC_V6))
247 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
248 else
249 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
250 }
251}
252
253static void qeth_l2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
254 struct sk_buff *skb, int ipv, int cast_type)
255{
Frank Blaschka683d7182008-08-01 16:39:13 +0200256 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb_mac_header(skb);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100257
258 memset(hdr, 0, sizeof(struct qeth_hdr));
259 hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
260
261 /* set byte byte 3 to casting flags */
262 if (cast_type == RTN_MULTICAST)
263 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
264 else if (cast_type == RTN_BROADCAST)
265 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
266 else
267 qeth_l2_get_packet_type(card, hdr, skb);
268
269 hdr->hdr.l2.pkt_length = skb->len-QETH_HEADER_SIZE;
270 /* VSWITCH relies on the VLAN
271 * information to be present in
272 * the QDIO header */
273 if (veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)) {
274 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
275 hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
276 }
277}
278
279static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
280 struct qeth_reply *reply, unsigned long data)
281{
282 struct qeth_ipa_cmd *cmd;
283
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200284 QETH_DBF_TEXT(TRACE, 2, "L2sdvcb");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100285 cmd = (struct qeth_ipa_cmd *) data;
286 if (cmd->hdr.return_code) {
Frank Blaschka14cc21b2008-06-06 12:37:46 +0200287 QETH_DBF_MESSAGE(2, "Error in processing VLAN %i on %s: 0x%x. "
Frank Blaschka4a71df52008-02-15 09:19:42 +0100288 "Continuing\n", cmd->data.setdelvlan.vlan_id,
289 QETH_CARD_IFNAME(card), cmd->hdr.return_code);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200290 QETH_DBF_TEXT_(TRACE, 2, "L2VL%4x", cmd->hdr.command);
291 QETH_DBF_TEXT_(TRACE, 2, "L2%s", CARD_BUS_ID(card));
292 QETH_DBF_TEXT_(TRACE, 2, "err%d", cmd->hdr.return_code);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100293 }
294 return 0;
295}
296
297static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
298 enum qeth_ipa_cmds ipacmd)
299{
300 struct qeth_ipa_cmd *cmd;
301 struct qeth_cmd_buffer *iob;
302
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200303 QETH_DBF_TEXT_(TRACE, 4, "L2sdv%x", ipacmd);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100304 iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
305 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
306 cmd->data.setdelvlan.vlan_id = i;
307 return qeth_send_ipa_cmd(card, iob,
308 qeth_l2_send_setdelvlan_cb, NULL);
309}
310
311static void qeth_l2_process_vlans(struct qeth_card *card, int clear)
312{
313 struct qeth_vlan_vid *id;
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200314 QETH_DBF_TEXT(TRACE, 3, "L2prcvln");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100315 spin_lock_bh(&card->vlanlock);
316 list_for_each_entry(id, &card->vid_list, list) {
317 if (clear)
318 qeth_l2_send_setdelvlan(card, id->vid,
319 IPA_CMD_DELVLAN);
320 else
321 qeth_l2_send_setdelvlan(card, id->vid,
322 IPA_CMD_SETVLAN);
323 }
324 spin_unlock_bh(&card->vlanlock);
325}
326
327static void qeth_l2_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
328{
Heiko Carstens509e2562008-07-26 02:24:10 -0700329 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100330 struct qeth_vlan_vid *id;
331
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200332 QETH_DBF_TEXT_(TRACE, 4, "aid:%d", vid);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100333 id = kmalloc(sizeof(struct qeth_vlan_vid), GFP_ATOMIC);
334 if (id) {
335 id->vid = vid;
336 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
337 spin_lock_bh(&card->vlanlock);
338 list_add_tail(&id->list, &card->vid_list);
339 spin_unlock_bh(&card->vlanlock);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100340 }
341}
342
343static void qeth_l2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
344{
345 struct qeth_vlan_vid *id, *tmpid = NULL;
Heiko Carstens509e2562008-07-26 02:24:10 -0700346 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100347
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200348 QETH_DBF_TEXT_(TRACE, 4, "kid:%d", vid);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100349 spin_lock_bh(&card->vlanlock);
350 list_for_each_entry(id, &card->vid_list, list) {
351 if (id->vid == vid) {
352 list_del(&id->list);
353 tmpid = id;
354 break;
355 }
356 }
357 spin_unlock_bh(&card->vlanlock);
358 if (tmpid) {
359 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
360 kfree(tmpid);
361 }
362 qeth_l2_set_multicast_list(card->dev);
363}
364
365static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
366{
367 int rc = 0;
368
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200369 QETH_DBF_TEXT(SETUP , 2, "stopcard");
370 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
Frank Blaschka4a71df52008-02-15 09:19:42 +0100371
372 qeth_set_allowed_threads(card, 0, 1);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100373 if (card->read.state == CH_STATE_UP &&
374 card->write.state == CH_STATE_UP &&
375 (card->state == CARD_STATE_UP)) {
376 if (recovery_mode &&
377 card->info.type != QETH_CARD_TYPE_OSN) {
378 qeth_l2_stop(card->dev);
379 } else {
380 rtnl_lock();
381 dev_close(card->dev);
382 rtnl_unlock();
383 }
384 if (!card->use_hard_stop) {
385 __u8 *mac = &card->dev->dev_addr[0];
386 rc = qeth_l2_send_delmac(card, mac);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200387 QETH_DBF_TEXT_(SETUP, 2, "Lerr%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100388 }
389 card->state = CARD_STATE_SOFTSETUP;
390 }
391 if (card->state == CARD_STATE_SOFTSETUP) {
392 qeth_l2_process_vlans(card, 1);
Ursula Braun28a7e4c2008-09-19 12:56:03 +0200393 if (!card->use_hard_stop)
394 qeth_l2_del_all_mc(card);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100395 qeth_clear_ipacmd_list(card);
396 card->state = CARD_STATE_HARDSETUP;
397 }
398 if (card->state == CARD_STATE_HARDSETUP) {
399 qeth_qdio_clear_card(card, 0);
400 qeth_clear_qdio_buffers(card);
401 qeth_clear_working_pool_list(card);
402 card->state = CARD_STATE_DOWN;
403 }
404 if (card->state == CARD_STATE_DOWN) {
405 qeth_clear_cmd_buffers(&card->read);
406 qeth_clear_cmd_buffers(&card->write);
407 }
408 card->use_hard_stop = 0;
409 return rc;
410}
411
412static void qeth_l2_process_inbound_buffer(struct qeth_card *card,
413 struct qeth_qdio_buffer *buf, int index)
414{
415 struct qdio_buffer_element *element;
416 struct sk_buff *skb;
417 struct qeth_hdr *hdr;
418 int offset;
419 unsigned int len;
420
421 /* get first element of current buffer */
422 element = (struct qdio_buffer_element *)&buf->buffer->element[0];
423 offset = 0;
424 if (card->options.performance_stats)
425 card->perf_stats.bufs_rec++;
426 while ((skb = qeth_core_get_next_skb(card, buf->buffer, &element,
427 &offset, &hdr))) {
428 skb->dev = card->dev;
429 /* is device UP ? */
430 if (!(card->dev->flags & IFF_UP)) {
431 dev_kfree_skb_any(skb);
432 continue;
433 }
434
435 switch (hdr->hdr.l2.id) {
436 case QETH_HEADER_TYPE_LAYER2:
437 skb->pkt_type = PACKET_HOST;
438 skb->protocol = eth_type_trans(skb, skb->dev);
439 if (card->options.checksum_type == NO_CHECKSUMMING)
440 skb->ip_summed = CHECKSUM_UNNECESSARY;
441 else
442 skb->ip_summed = CHECKSUM_NONE;
Ursula Braun12883722008-04-01 10:26:56 +0200443 if (skb->protocol == htons(ETH_P_802_2))
444 *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100445 len = skb->len;
446 netif_rx(skb);
447 break;
448 case QETH_HEADER_TYPE_OSN:
Ursula Braun2d488c22008-10-24 11:16:55 +0200449 if (card->info.type == QETH_CARD_TYPE_OSN) {
450 skb_push(skb, sizeof(struct qeth_hdr));
451 skb_copy_to_linear_data(skb, hdr,
Frank Blaschka4a71df52008-02-15 09:19:42 +0100452 sizeof(struct qeth_hdr));
Ursula Braun2d488c22008-10-24 11:16:55 +0200453 len = skb->len;
454 card->osn_info.data_cb(skb);
455 break;
456 }
457 /* else unknown */
Frank Blaschka4a71df52008-02-15 09:19:42 +0100458 default:
459 dev_kfree_skb_any(skb);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200460 QETH_DBF_TEXT(TRACE, 3, "inbunkno");
461 QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100462 continue;
463 }
464 card->dev->last_rx = jiffies;
465 card->stats.rx_packets++;
466 card->stats.rx_bytes += len;
467 }
468}
469
470static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
471 enum qeth_ipa_cmds ipacmd,
472 int (*reply_cb) (struct qeth_card *,
473 struct qeth_reply*,
474 unsigned long))
475{
476 struct qeth_ipa_cmd *cmd;
477 struct qeth_cmd_buffer *iob;
478
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200479 QETH_DBF_TEXT(TRACE, 2, "L2sdmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100480 iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
481 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
482 cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
483 memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
484 return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
485}
486
487static int qeth_l2_send_setmac_cb(struct qeth_card *card,
488 struct qeth_reply *reply,
489 unsigned long data)
490{
491 struct qeth_ipa_cmd *cmd;
492
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200493 QETH_DBF_TEXT(TRACE, 2, "L2Smaccb");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100494 cmd = (struct qeth_ipa_cmd *) data;
495 if (cmd->hdr.return_code) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200496 QETH_DBF_TEXT_(TRACE, 2, "L2er%x", cmd->hdr.return_code);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100497 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
498 cmd->hdr.return_code = -EIO;
499 } else {
500 card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
501 memcpy(card->dev->dev_addr, cmd->data.setdelmac.mac,
502 OSA_ADDR_LEN);
Frank Blaschka74eacdb2008-12-25 13:39:49 +0100503 dev_info(&card->gdev->dev,
504 "MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
505 "successfully registered on device %s\n",
506 card->dev->dev_addr[0], card->dev->dev_addr[1],
507 card->dev->dev_addr[2], card->dev->dev_addr[3],
508 card->dev->dev_addr[4], card->dev->dev_addr[5],
509 card->dev->name);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100510 }
511 return 0;
512}
513
514static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
515{
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200516 QETH_DBF_TEXT(TRACE, 2, "L2Setmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100517 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
518 qeth_l2_send_setmac_cb);
519}
520
521static int qeth_l2_send_delmac_cb(struct qeth_card *card,
522 struct qeth_reply *reply,
523 unsigned long data)
524{
525 struct qeth_ipa_cmd *cmd;
526
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200527 QETH_DBF_TEXT(TRACE, 2, "L2Dmaccb");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100528 cmd = (struct qeth_ipa_cmd *) data;
529 if (cmd->hdr.return_code) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200530 QETH_DBF_TEXT_(TRACE, 2, "err%d", cmd->hdr.return_code);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100531 cmd->hdr.return_code = -EIO;
532 return 0;
533 }
534 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
535
536 return 0;
537}
538
539static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
540{
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200541 QETH_DBF_TEXT(TRACE, 2, "L2Delmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100542 if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
543 return 0;
544 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
545 qeth_l2_send_delmac_cb);
546}
547
548static int qeth_l2_request_initial_mac(struct qeth_card *card)
549{
550 int rc = 0;
551 char vendor_pre[] = {0x02, 0x00, 0x00};
552
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200553 QETH_DBF_TEXT(SETUP, 2, "doL2init");
554 QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
Frank Blaschka4a71df52008-02-15 09:19:42 +0100555
556 rc = qeth_query_setadapterparms(card);
557 if (rc) {
Frank Blaschka14cc21b2008-06-06 12:37:46 +0200558 QETH_DBF_MESSAGE(2, "could not query adapter parameters on "
559 "device %s: x%x\n", CARD_BUS_ID(card), rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100560 }
561
Ursula Braunf1d58672008-09-19 12:56:02 +0200562 if ((card->info.type == QETH_CARD_TYPE_IQD) ||
563 (card->info.guestlan)) {
Frank Blaschka4a71df52008-02-15 09:19:42 +0100564 rc = qeth_setadpparms_change_macaddr(card);
565 if (rc) {
Frank Blaschka14cc21b2008-06-06 12:37:46 +0200566 QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
567 "device %s: x%x\n", CARD_BUS_ID(card), rc);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200568 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100569 return rc;
570 }
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200571 QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100572 } else {
573 random_ether_addr(card->dev->dev_addr);
574 memcpy(card->dev->dev_addr, vendor_pre, 3);
575 }
576 return 0;
577}
578
579static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
580{
581 struct sockaddr *addr = p;
Heiko Carstens509e2562008-07-26 02:24:10 -0700582 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100583 int rc = 0;
584
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200585 QETH_DBF_TEXT(TRACE, 3, "setmac");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100586
587 if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200588 QETH_DBF_TEXT(TRACE, 3, "setmcINV");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100589 return -EOPNOTSUPP;
590 }
591
592 if (card->info.type == QETH_CARD_TYPE_OSN) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200593 QETH_DBF_TEXT(TRACE, 3, "setmcOSN");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100594 return -EOPNOTSUPP;
595 }
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200596 QETH_DBF_TEXT_(TRACE, 3, "%s", CARD_BUS_ID(card));
597 QETH_DBF_HEX(TRACE, 3, addr->sa_data, OSA_ADDR_LEN);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100598 rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
599 if (!rc)
600 rc = qeth_l2_send_setmac(card, addr->sa_data);
601 return rc;
602}
603
604static void qeth_l2_set_multicast_list(struct net_device *dev)
605{
Heiko Carstens509e2562008-07-26 02:24:10 -0700606 struct qeth_card *card = dev->ml_priv;
Frank Blaschka7db22662008-08-15 08:02:59 +0200607 struct dev_addr_list *dm;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100608
609 if (card->info.type == QETH_CARD_TYPE_OSN)
610 return ;
611
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200612 QETH_DBF_TEXT(TRACE, 3, "setmulti");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100613 qeth_l2_del_all_mc(card);
614 spin_lock_bh(&card->mclock);
615 for (dm = dev->mc_list; dm; dm = dm->next)
Frank Blaschka7db22662008-08-15 08:02:59 +0200616 qeth_l2_add_mc(card, dm->da_addr, 0);
617
618 for (dm = dev->uc_list; dm; dm = dm->next)
619 qeth_l2_add_mc(card, dm->da_addr, 1);
620
Frank Blaschka4a71df52008-02-15 09:19:42 +0100621 spin_unlock_bh(&card->mclock);
622 if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
623 return;
624 qeth_setadp_promisc_mode(card);
625}
626
627static int qeth_l2_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
628{
629 int rc;
630 struct qeth_hdr *hdr = NULL;
631 int elements = 0;
Heiko Carstens509e2562008-07-26 02:24:10 -0700632 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100633 struct sk_buff *new_skb = skb;
634 int ipv = qeth_get_ip_version(skb);
635 int cast_type = qeth_get_cast_type(card, skb);
636 struct qeth_qdio_out_q *queue = card->qdio.out_qs
637 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
638 int tx_bytes = skb->len;
639 enum qeth_large_send_types large_send = QETH_LARGE_SEND_NO;
640 struct qeth_eddp_context *ctx = NULL;
Frank Blaschka683d7182008-08-01 16:39:13 +0200641 int data_offset = -1;
642 int elements_needed = 0;
643 int hd_len = 0;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100644
Frank Blaschka4a71df52008-02-15 09:19:42 +0100645 if ((card->state != CARD_STATE_UP) || !card->lan_online) {
646 card->stats.tx_carrier_errors++;
647 goto tx_drop;
648 }
649
650 if ((card->info.type == QETH_CARD_TYPE_OSN) &&
651 (skb->protocol == htons(ETH_P_IPV6)))
652 goto tx_drop;
653
654 if (card->options.performance_stats) {
655 card->perf_stats.outbound_cnt++;
656 card->perf_stats.outbound_start_time = qeth_get_micros();
657 }
658 netif_stop_queue(dev);
659
660 if (skb_is_gso(skb))
661 large_send = QETH_LARGE_SEND_EDDP;
662
663 if (card->info.type == QETH_CARD_TYPE_OSN)
664 hdr = (struct qeth_hdr *)skb->data;
665 else {
Frank Blaschka683d7182008-08-01 16:39:13 +0200666 if ((card->info.type == QETH_CARD_TYPE_IQD) && (!large_send) &&
667 (skb_shinfo(skb)->nr_frags == 0)) {
668 new_skb = skb;
669 data_offset = ETH_HLEN;
670 hd_len = ETH_HLEN;
671 hdr = kmem_cache_alloc(qeth_core_header_cache,
672 GFP_ATOMIC);
673 if (!hdr)
674 goto tx_drop;
675 elements_needed++;
676 skb_reset_mac_header(new_skb);
677 qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
678 hdr->hdr.l2.pkt_length = new_skb->len;
679 memcpy(((char *)hdr) + sizeof(struct qeth_hdr),
680 skb_mac_header(new_skb), ETH_HLEN);
681 } else {
682 /* create a clone with writeable headroom */
683 new_skb = skb_realloc_headroom(skb,
Frank Blaschkaf90b7442008-04-24 10:15:24 +0200684 sizeof(struct qeth_hdr));
Frank Blaschka683d7182008-08-01 16:39:13 +0200685 if (!new_skb)
686 goto tx_drop;
687 hdr = (struct qeth_hdr *)skb_push(new_skb,
688 sizeof(struct qeth_hdr));
689 skb_set_mac_header(new_skb, sizeof(struct qeth_hdr));
690 qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
691 }
Frank Blaschka4a71df52008-02-15 09:19:42 +0100692 }
693
694 if (large_send == QETH_LARGE_SEND_EDDP) {
695 ctx = qeth_eddp_create_context(card, new_skb, hdr,
696 skb->sk->sk_protocol);
697 if (ctx == NULL) {
Frank Blaschka14cc21b2008-06-06 12:37:46 +0200698 QETH_DBF_MESSAGE(2, "could not create eddp context\n");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100699 goto tx_drop;
700 }
701 } else {
Frank Blaschka683d7182008-08-01 16:39:13 +0200702 elements = qeth_get_elements_no(card, (void *)hdr, new_skb,
703 elements_needed);
704 if (!elements) {
705 if (data_offset >= 0)
706 kmem_cache_free(qeth_core_header_cache, hdr);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100707 goto tx_drop;
Frank Blaschka683d7182008-08-01 16:39:13 +0200708 }
Frank Blaschka4a71df52008-02-15 09:19:42 +0100709 }
710
711 if ((large_send == QETH_LARGE_SEND_NO) &&
712 (skb->ip_summed == CHECKSUM_PARTIAL))
713 qeth_tx_csum(new_skb);
714
715 if (card->info.type != QETH_CARD_TYPE_IQD)
716 rc = qeth_do_send_packet(card, queue, new_skb, hdr,
717 elements, ctx);
718 else
719 rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
Frank Blaschka683d7182008-08-01 16:39:13 +0200720 elements, ctx, data_offset, hd_len);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100721 if (!rc) {
722 card->stats.tx_packets++;
723 card->stats.tx_bytes += tx_bytes;
724 if (new_skb != skb)
725 dev_kfree_skb_any(skb);
726 if (card->options.performance_stats) {
727 if (large_send != QETH_LARGE_SEND_NO) {
728 card->perf_stats.large_send_bytes += tx_bytes;
729 card->perf_stats.large_send_cnt++;
730 }
731 if (skb_shinfo(new_skb)->nr_frags > 0) {
732 card->perf_stats.sg_skbs_sent++;
733 /* nr_frags + skb->data */
734 card->perf_stats.sg_frags_sent +=
735 skb_shinfo(new_skb)->nr_frags + 1;
736 }
737 }
738
739 if (ctx != NULL) {
740 qeth_eddp_put_context(ctx);
741 dev_kfree_skb_any(new_skb);
742 }
743 } else {
744 if (ctx != NULL)
745 qeth_eddp_put_context(ctx);
746
Frank Blaschka683d7182008-08-01 16:39:13 +0200747 if (data_offset >= 0)
748 kmem_cache_free(qeth_core_header_cache, hdr);
749
Frank Blaschka4a71df52008-02-15 09:19:42 +0100750 if (rc == -EBUSY) {
751 if (new_skb != skb)
752 dev_kfree_skb_any(new_skb);
753 return NETDEV_TX_BUSY;
754 } else
755 goto tx_drop;
756 }
757
758 netif_wake_queue(dev);
759 if (card->options.performance_stats)
760 card->perf_stats.outbound_time += qeth_get_micros() -
761 card->perf_stats.outbound_start_time;
762 return rc;
763
764tx_drop:
765 card->stats.tx_dropped++;
766 card->stats.tx_errors++;
767 if ((new_skb != skb) && new_skb)
768 dev_kfree_skb_any(new_skb);
769 dev_kfree_skb_any(skb);
Frank Blaschkad0ec0f52008-06-06 12:37:48 +0200770 netif_wake_queue(dev);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100771 return NETDEV_TX_OK;
772}
773
774static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev,
Jan Glauber779e6e12008-07-17 17:16:48 +0200775 unsigned int qdio_err, unsigned int queue,
Frank Blaschka4a71df52008-02-15 09:19:42 +0100776 int first_element, int count, unsigned long card_ptr)
777{
778 struct net_device *net_dev;
779 struct qeth_card *card;
780 struct qeth_qdio_buffer *buffer;
781 int index;
782 int i;
783
Frank Blaschka4a71df52008-02-15 09:19:42 +0100784 card = (struct qeth_card *) card_ptr;
785 net_dev = card->dev;
786 if (card->options.performance_stats) {
787 card->perf_stats.inbound_cnt++;
788 card->perf_stats.inbound_start_time = qeth_get_micros();
789 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200790 if (qdio_err & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
791 QETH_DBF_TEXT(TRACE, 1, "qdinchk");
792 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
793 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", first_element,
794 count);
795 QETH_DBF_TEXT_(TRACE, 1, "%04X", queue);
796 qeth_schedule_recovery(card);
797 return;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100798 }
799 for (i = first_element; i < (first_element + count); ++i) {
800 index = i % QDIO_MAX_BUFFERS_PER_Q;
801 buffer = &card->qdio.in_q->bufs[index];
Jan Glauber779e6e12008-07-17 17:16:48 +0200802 if (!(qdio_err &&
803 qeth_check_qdio_errors(buffer->buffer, qdio_err, "qinerr")))
Frank Blaschka4a71df52008-02-15 09:19:42 +0100804 qeth_l2_process_inbound_buffer(card, buffer, index);
805 /* clear buffer and give back to hardware */
806 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
807 qeth_queue_input_buffer(card, index);
808 }
809 if (card->options.performance_stats)
810 card->perf_stats.inbound_time += qeth_get_micros() -
811 card->perf_stats.inbound_start_time;
812}
813
814static int qeth_l2_open(struct net_device *dev)
815{
Heiko Carstens509e2562008-07-26 02:24:10 -0700816 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100817
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200818 QETH_DBF_TEXT(TRACE, 4, "qethopen");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100819 if (card->state != CARD_STATE_SOFTSETUP)
820 return -ENODEV;
821
822 if ((card->info.type != QETH_CARD_TYPE_OSN) &&
823 (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200824 QETH_DBF_TEXT(TRACE, 4, "nomacadr");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100825 return -EPERM;
826 }
827 card->data.state = CH_STATE_UP;
828 card->state = CARD_STATE_UP;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100829 netif_start_queue(dev);
830
831 if (!card->lan_online && netif_carrier_ok(dev))
832 netif_carrier_off(dev);
833 return 0;
834}
835
836
837static int qeth_l2_stop(struct net_device *dev)
838{
Heiko Carstens509e2562008-07-26 02:24:10 -0700839 struct qeth_card *card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100840
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200841 QETH_DBF_TEXT(TRACE, 4, "qethstop");
Frank Blaschka4a71df52008-02-15 09:19:42 +0100842 netif_tx_disable(dev);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100843 if (card->state == CARD_STATE_UP)
844 card->state = CARD_STATE_SOFTSETUP;
845 return 0;
846}
847
848static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
849{
850 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
851
852 INIT_LIST_HEAD(&card->vid_list);
853 INIT_LIST_HEAD(&card->mc_list);
854 card->options.layer2 = 1;
855 card->discipline.input_handler = (qdio_handler_t *)
856 qeth_l2_qdio_input_handler;
857 card->discipline.output_handler = (qdio_handler_t *)
858 qeth_qdio_output_handler;
859 card->discipline.recover = qeth_l2_recover;
860 return 0;
861}
862
863static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
864{
865 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
866
867 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
868
869 if (cgdev->state == CCWGROUP_ONLINE) {
870 card->use_hard_stop = 1;
871 qeth_l2_set_offline(cgdev);
872 }
873
874 if (card->dev) {
875 unregister_netdev(card->dev);
876 card->dev = NULL;
877 }
878
879 qeth_l2_del_all_mc(card);
880 return;
881}
882
Frank Blaschkaefe3df62008-04-24 10:15:27 +0200883static int qeth_l2_ethtool_set_tso(struct net_device *dev, u32 data)
884{
Heiko Carstens509e2562008-07-26 02:24:10 -0700885 struct qeth_card *card = dev->ml_priv;
Frank Blaschkaefe3df62008-04-24 10:15:27 +0200886
887 if (data) {
888 if (card->options.large_send == QETH_LARGE_SEND_NO) {
889 card->options.large_send = QETH_LARGE_SEND_EDDP;
890 dev->features |= NETIF_F_TSO;
891 }
892 } else {
893 dev->features &= ~NETIF_F_TSO;
894 card->options.large_send = QETH_LARGE_SEND_NO;
895 }
896 return 0;
897}
898
Frank Blaschka4a71df52008-02-15 09:19:42 +0100899static struct ethtool_ops qeth_l2_ethtool_ops = {
900 .get_link = ethtool_op_get_link,
901 .get_tx_csum = ethtool_op_get_tx_csum,
902 .set_tx_csum = ethtool_op_set_tx_hw_csum,
903 .get_sg = ethtool_op_get_sg,
904 .set_sg = ethtool_op_set_sg,
905 .get_tso = ethtool_op_get_tso,
Frank Blaschkaefe3df62008-04-24 10:15:27 +0200906 .set_tso = qeth_l2_ethtool_set_tso,
Frank Blaschka4a71df52008-02-15 09:19:42 +0100907 .get_strings = qeth_core_get_strings,
908 .get_ethtool_stats = qeth_core_get_ethtool_stats,
909 .get_stats_count = qeth_core_get_stats_count,
910 .get_drvinfo = qeth_core_get_drvinfo,
Frank Blaschka3f9975a2008-04-24 10:15:23 +0200911 .get_settings = qeth_core_ethtool_get_settings,
Frank Blaschka4a71df52008-02-15 09:19:42 +0100912};
913
914static struct ethtool_ops qeth_l2_osn_ops = {
915 .get_strings = qeth_core_get_strings,
916 .get_ethtool_stats = qeth_core_get_ethtool_stats,
917 .get_stats_count = qeth_core_get_stats_count,
918 .get_drvinfo = qeth_core_get_drvinfo,
919};
920
921static int qeth_l2_setup_netdev(struct qeth_card *card)
922{
923 switch (card->info.type) {
924 case QETH_CARD_TYPE_OSAE:
925 card->dev = alloc_etherdev(0);
926 break;
927 case QETH_CARD_TYPE_IQD:
928 card->dev = alloc_netdev(0, "hsi%d", ether_setup);
929 break;
930 case QETH_CARD_TYPE_OSN:
931 card->dev = alloc_netdev(0, "osn%d", ether_setup);
932 card->dev->flags |= IFF_NOARP;
933 break;
934 default:
935 card->dev = alloc_etherdev(0);
936 }
937
938 if (!card->dev)
939 return -ENODEV;
940
Heiko Carstens509e2562008-07-26 02:24:10 -0700941 card->dev->ml_priv = card;
Frank Blaschka4a71df52008-02-15 09:19:42 +0100942 card->dev->tx_timeout = &qeth_tx_timeout;
943 card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
944 card->dev->open = qeth_l2_open;
945 card->dev->stop = qeth_l2_stop;
946 card->dev->hard_start_xmit = qeth_l2_hard_start_xmit;
947 card->dev->do_ioctl = qeth_l2_do_ioctl;
948 card->dev->get_stats = qeth_get_stats;
949 card->dev->change_mtu = qeth_change_mtu;
950 card->dev->set_multicast_list = qeth_l2_set_multicast_list;
951 card->dev->vlan_rx_kill_vid = qeth_l2_vlan_rx_kill_vid;
952 card->dev->vlan_rx_add_vid = qeth_l2_vlan_rx_add_vid;
953 card->dev->set_mac_address = qeth_l2_set_mac_address;
954 card->dev->mtu = card->info.initial_mtu;
955 if (card->info.type != QETH_CARD_TYPE_OSN)
956 SET_ETHTOOL_OPS(card->dev, &qeth_l2_ethtool_ops);
957 else
958 SET_ETHTOOL_OPS(card->dev, &qeth_l2_osn_ops);
959 card->dev->features |= NETIF_F_HW_VLAN_FILTER;
960 card->info.broadcast_capable = 1;
961 qeth_l2_request_initial_mac(card);
962 SET_NETDEV_DEV(card->dev, &card->gdev->dev);
963 return register_netdev(card->dev);
964}
965
966static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
967{
968 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
969 int rc = 0;
970 enum qeth_card_states recover_flag;
971
972 BUG_ON(!card);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200973 QETH_DBF_TEXT(SETUP, 2, "setonlin");
974 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
Frank Blaschka4a71df52008-02-15 09:19:42 +0100975
976 qeth_set_allowed_threads(card, QETH_RECOVER_THREAD, 1);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100977 recover_flag = card->state;
978 rc = ccw_device_set_online(CARD_RDEV(card));
979 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200980 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100981 return -EIO;
982 }
983 rc = ccw_device_set_online(CARD_WDEV(card));
984 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200985 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100986 return -EIO;
987 }
988 rc = ccw_device_set_online(CARD_DDEV(card));
989 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200990 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100991 return -EIO;
992 }
993
994 rc = qeth_core_hardsetup_card(card);
995 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +0200996 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +0100997 goto out_remove;
998 }
999
1000 if (!card->dev && qeth_l2_setup_netdev(card))
1001 goto out_remove;
1002
1003 if (card->info.type != QETH_CARD_TYPE_OSN)
1004 qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
1005
1006 card->state = CARD_STATE_HARDSETUP;
1007 qeth_print_status_message(card);
1008
1009 /* softsetup */
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001010 QETH_DBF_TEXT(SETUP, 2, "softsetp");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001011
1012 rc = qeth_send_startlan(card);
1013 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001014 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +01001015 if (rc == 0xe080) {
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001016 dev_warn(&card->gdev->dev,
1017 "The LAN is offline\n");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001018 card->lan_online = 0;
1019 }
1020 return rc;
1021 } else
1022 card->lan_online = 1;
1023
1024 if (card->info.type != QETH_CARD_TYPE_OSN) {
1025 qeth_set_large_send(card, card->options.large_send);
1026 qeth_l2_process_vlans(card, 0);
1027 }
1028
1029 netif_tx_disable(card->dev);
1030
1031 rc = qeth_init_qdio_queues(card);
1032 if (rc) {
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001033 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +01001034 goto out_remove;
1035 }
1036 card->state = CARD_STATE_SOFTSETUP;
1037 netif_carrier_on(card->dev);
1038
1039 qeth_set_allowed_threads(card, 0xffffffff, 0);
1040 if (recover_flag == CARD_STATE_RECOVER) {
1041 if (recovery_mode &&
1042 card->info.type != QETH_CARD_TYPE_OSN) {
1043 qeth_l2_open(card->dev);
1044 } else {
1045 rtnl_lock();
1046 dev_open(card->dev);
1047 rtnl_unlock();
1048 }
1049 /* this also sets saved unicast addresses */
1050 qeth_l2_set_multicast_list(card->dev);
1051 }
1052 /* let user_space know that device is online */
1053 kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1054 return 0;
1055out_remove:
1056 card->use_hard_stop = 1;
1057 qeth_l2_stop_card(card, 0);
1058 ccw_device_set_offline(CARD_DDEV(card));
1059 ccw_device_set_offline(CARD_WDEV(card));
1060 ccw_device_set_offline(CARD_RDEV(card));
1061 if (recover_flag == CARD_STATE_RECOVER)
1062 card->state = CARD_STATE_RECOVER;
1063 else
1064 card->state = CARD_STATE_DOWN;
1065 return -ENODEV;
1066}
1067
1068static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1069{
1070 return __qeth_l2_set_online(gdev, 0);
1071}
1072
1073static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1074 int recovery_mode)
1075{
1076 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1077 int rc = 0, rc2 = 0, rc3 = 0;
1078 enum qeth_card_states recover_flag;
1079
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001080 QETH_DBF_TEXT(SETUP, 3, "setoffl");
1081 QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
Frank Blaschka4a71df52008-02-15 09:19:42 +01001082
1083 if (card->dev && netif_carrier_ok(card->dev))
1084 netif_carrier_off(card->dev);
1085 recover_flag = card->state;
Ursula Braun0f5623c2008-10-24 11:16:52 +02001086 qeth_l2_stop_card(card, recovery_mode);
Frank Blaschka4a71df52008-02-15 09:19:42 +01001087 rc = ccw_device_set_offline(CARD_DDEV(card));
1088 rc2 = ccw_device_set_offline(CARD_WDEV(card));
1089 rc3 = ccw_device_set_offline(CARD_RDEV(card));
1090 if (!rc)
1091 rc = (rc2) ? rc2 : rc3;
1092 if (rc)
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001093 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +01001094 if (recover_flag == CARD_STATE_UP)
1095 card->state = CARD_STATE_RECOVER;
1096 /* let user_space know that device is offline */
1097 kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1098 return 0;
1099}
1100
1101static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1102{
1103 return __qeth_l2_set_offline(cgdev, 0);
1104}
1105
1106static int qeth_l2_recover(void *ptr)
1107{
1108 struct qeth_card *card;
1109 int rc = 0;
1110
1111 card = (struct qeth_card *) ptr;
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001112 QETH_DBF_TEXT(TRACE, 2, "recover1");
1113 QETH_DBF_HEX(TRACE, 2, &card, sizeof(void *));
Frank Blaschka4a71df52008-02-15 09:19:42 +01001114 if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1115 return 0;
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001116 QETH_DBF_TEXT(TRACE, 2, "recover2");
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001117 dev_warn(&card->gdev->dev,
1118 "A recovery process has been started for the device\n");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001119 card->use_hard_stop = 1;
1120 __qeth_l2_set_offline(card->gdev, 1);
1121 rc = __qeth_l2_set_online(card->gdev, 1);
1122 /* don't run another scheduled recovery */
1123 qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1124 qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1125 if (!rc)
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001126 dev_info(&card->gdev->dev,
1127 "Device successfully recovered!\n");
Ursula Braun28a7e4c2008-09-19 12:56:03 +02001128 else {
1129 rtnl_lock();
1130 dev_close(card->dev);
1131 rtnl_unlock();
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001132 dev_warn(&card->gdev->dev, "The qeth device driver "
1133 "failed to recover an error on the device\n");
Ursula Braun28a7e4c2008-09-19 12:56:03 +02001134 }
Frank Blaschka4a71df52008-02-15 09:19:42 +01001135 return 0;
1136}
1137
1138static int __init qeth_l2_init(void)
1139{
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001140 pr_info("register layer 2 discipline\n");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001141 return 0;
1142}
1143
1144static void __exit qeth_l2_exit(void)
1145{
Frank Blaschka74eacdb2008-12-25 13:39:49 +01001146 pr_info("unregister layer 2 discipline\n");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001147}
1148
1149static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1150{
1151 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1152 qeth_qdio_clear_card(card, 0);
1153 qeth_clear_qdio_buffers(card);
1154}
1155
1156struct ccwgroup_driver qeth_l2_ccwgroup_driver = {
1157 .probe = qeth_l2_probe_device,
1158 .remove = qeth_l2_remove_device,
1159 .set_online = qeth_l2_set_online,
1160 .set_offline = qeth_l2_set_offline,
1161 .shutdown = qeth_l2_shutdown,
1162};
1163EXPORT_SYMBOL_GPL(qeth_l2_ccwgroup_driver);
1164
1165static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1166 struct qeth_cmd_buffer *iob)
1167{
1168 unsigned long flags;
1169 int rc = 0;
1170
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001171 QETH_DBF_TEXT(TRACE, 5, "osndctrd");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001172
1173 wait_event(card->wait_q,
1174 atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1175 qeth_prepare_control_data(card, len, iob);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001176 QETH_DBF_TEXT(TRACE, 6, "osnoirqp");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001177 spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1178 rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1179 (addr_t) iob, 0, 0);
1180 spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1181 if (rc) {
Frank Blaschka14cc21b2008-06-06 12:37:46 +02001182 QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
Frank Blaschka4a71df52008-02-15 09:19:42 +01001183 "ccw_device_start rc = %i\n", rc);
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001184 QETH_DBF_TEXT_(TRACE, 2, " err%d", rc);
Frank Blaschka4a71df52008-02-15 09:19:42 +01001185 qeth_release_buffer(iob->channel, iob);
1186 atomic_set(&card->write.irq_pending, 0);
1187 wake_up(&card->wait_q);
1188 }
1189 return rc;
1190}
1191
1192static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1193 struct qeth_cmd_buffer *iob, int data_len)
1194{
1195 u16 s1, s2;
1196
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001197 QETH_DBF_TEXT(TRACE, 4, "osndipa");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001198
1199 qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1200 s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1201 s2 = (u16)data_len;
1202 memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1203 memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1204 memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1205 memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1206 return qeth_osn_send_control_data(card, s1, iob);
1207}
1208
1209int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1210{
1211 struct qeth_cmd_buffer *iob;
1212 struct qeth_card *card;
1213 int rc;
1214
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001215 QETH_DBF_TEXT(TRACE, 2, "osnsdmc");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001216 if (!dev)
1217 return -ENODEV;
Heiko Carstens509e2562008-07-26 02:24:10 -07001218 card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +01001219 if (!card)
1220 return -ENODEV;
1221 if ((card->state != CARD_STATE_UP) &&
1222 (card->state != CARD_STATE_SOFTSETUP))
1223 return -ENODEV;
1224 iob = qeth_wait_for_buffer(&card->write);
1225 memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1226 rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1227 return rc;
1228}
1229EXPORT_SYMBOL(qeth_osn_assist);
1230
1231int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1232 int (*assist_cb)(struct net_device *, void *),
1233 int (*data_cb)(struct sk_buff *))
1234{
1235 struct qeth_card *card;
1236
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001237 QETH_DBF_TEXT(TRACE, 2, "osnreg");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001238 *dev = qeth_l2_netdev_by_devno(read_dev_no);
1239 if (*dev == NULL)
1240 return -ENODEV;
Heiko Carstens509e2562008-07-26 02:24:10 -07001241 card = (*dev)->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +01001242 if (!card)
1243 return -ENODEV;
1244 if ((assist_cb == NULL) || (data_cb == NULL))
1245 return -EINVAL;
1246 card->osn_info.assist_cb = assist_cb;
1247 card->osn_info.data_cb = data_cb;
1248 return 0;
1249}
1250EXPORT_SYMBOL(qeth_osn_register);
1251
1252void qeth_osn_deregister(struct net_device *dev)
1253{
1254 struct qeth_card *card;
1255
Peter Tiedemannd11ba0c2008-04-01 10:26:58 +02001256 QETH_DBF_TEXT(TRACE, 2, "osndereg");
Frank Blaschka4a71df52008-02-15 09:19:42 +01001257 if (!dev)
1258 return;
Heiko Carstens509e2562008-07-26 02:24:10 -07001259 card = dev->ml_priv;
Frank Blaschka4a71df52008-02-15 09:19:42 +01001260 if (!card)
1261 return;
1262 card->osn_info.assist_cb = NULL;
1263 card->osn_info.data_cb = NULL;
1264 return;
1265}
1266EXPORT_SYMBOL(qeth_osn_deregister);
1267
1268module_init(qeth_l2_init);
1269module_exit(qeth_l2_exit);
1270MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1271MODULE_DESCRIPTION("qeth layer 2 discipline");
1272MODULE_LICENSE("GPL");