2 * drivers/s390/net/qeth_l2_main.c
4 * Copyright IBM Corp. 2007, 2009
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>
11 #define KMSG_COMPONENT "qeth"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #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/slab.h>
20 #include <linux/etherdevice.h>
21 #include <linux/mii.h>
23 #include <linux/list.h>
25 #include "qeth_core.h"
27 static int qeth_l2_set_offline(struct ccwgroup_device *);
28 static int qeth_l2_stop(struct net_device *);
29 static int qeth_l2_send_delmac(struct qeth_card *, __u8 *);
30 static int qeth_l2_send_setdelmac(struct qeth_card *, __u8 *,
32 int (*reply_cb) (struct qeth_card *,
35 static void qeth_l2_set_multicast_list(struct net_device *);
36 static int qeth_l2_recover(void *);
38 static int qeth_l2_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
40 struct qeth_card *card = dev->ml_priv;
41 struct mii_ioctl_data *mii_data;
47 if ((card->state != CARD_STATE_UP) &&
48 (card->state != CARD_STATE_SOFTSETUP))
51 if (card->info.type == QETH_CARD_TYPE_OSN)
55 case SIOC_QETH_ADP_SET_SNMP_CONTROL:
56 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
58 case SIOC_QETH_GET_CARD_TYPE:
59 if ((card->info.type == QETH_CARD_TYPE_OSD ||
60 card->info.type == QETH_CARD_TYPE_OSM ||
61 card->info.type == QETH_CARD_TYPE_OSX) &&
67 mii_data = if_mii(rq);
71 mii_data = if_mii(rq);
72 if (mii_data->phy_id != 0)
75 mii_data->val_out = qeth_mdio_read(dev,
76 mii_data->phy_id, mii_data->reg_num);
78 case SIOC_QETH_QUERY_OAT:
79 rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
85 QETH_CARD_TEXT_(card, 2, "ioce%d", rc);
89 static int qeth_l2_verify_dev(struct net_device *dev)
91 struct qeth_card *card;
95 read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
96 list_for_each_entry(card, &qeth_core_card_list.list, list) {
97 if (card->dev == dev) {
102 read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
107 static struct net_device *qeth_l2_netdev_by_devno(unsigned char *read_dev_no)
109 struct qeth_card *card;
110 struct net_device *ndev;
113 struct ccw_dev_id read_devid;
116 memcpy(&temp_dev_no, read_dev_no, 2);
117 read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
118 list_for_each_entry(card, &qeth_core_card_list.list, list) {
119 ccw_device_get_id(CARD_RDEV(card), &read_devid);
120 if (read_devid.devno == temp_dev_no) {
125 read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
129 static int qeth_l2_send_setgroupmac_cb(struct qeth_card *card,
130 struct qeth_reply *reply,
133 struct qeth_ipa_cmd *cmd;
136 QETH_CARD_TEXT(card, 2, "L2Sgmacb");
137 cmd = (struct qeth_ipa_cmd *) data;
138 mac = &cmd->data.setdelmac.mac[0];
139 /* MAC already registered, needed in couple/uncouple case */
140 if (cmd->hdr.return_code == IPA_RC_L2_DUP_MAC) {
141 QETH_DBF_MESSAGE(2, "Group MAC %pM already existing on %s \n",
142 mac, QETH_CARD_IFNAME(card));
143 cmd->hdr.return_code = 0;
145 if (cmd->hdr.return_code)
146 QETH_DBF_MESSAGE(2, "Could not set group MAC %pM on %s: %x\n",
147 mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
151 static int qeth_l2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
153 QETH_CARD_TEXT(card, 2, "L2Sgmac");
154 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
155 qeth_l2_send_setgroupmac_cb);
158 static int qeth_l2_send_delgroupmac_cb(struct qeth_card *card,
159 struct qeth_reply *reply,
162 struct qeth_ipa_cmd *cmd;
165 QETH_CARD_TEXT(card, 2, "L2Dgmacb");
166 cmd = (struct qeth_ipa_cmd *) data;
167 mac = &cmd->data.setdelmac.mac[0];
168 if (cmd->hdr.return_code)
169 QETH_DBF_MESSAGE(2, "Could not delete group MAC %pM on %s: %x\n",
170 mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
174 static int qeth_l2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
176 QETH_CARD_TEXT(card, 2, "L2Dgmac");
177 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
178 qeth_l2_send_delgroupmac_cb);
181 static void qeth_l2_add_mc(struct qeth_card *card, __u8 *mac, int vmac)
183 struct qeth_mc_mac *mc;
186 mc = kmalloc(sizeof(struct qeth_mc_mac), GFP_ATOMIC);
191 memcpy(mc->mc_addr, mac, OSA_ADDR_LEN);
192 mc->mc_addrlen = OSA_ADDR_LEN;
196 rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
199 rc = qeth_l2_send_setgroupmac(card, mac);
203 list_add_tail(&mc->list, &card->mc_list);
208 static void qeth_l2_del_all_mc(struct qeth_card *card, int del)
210 struct qeth_mc_mac *mc, *tmp;
212 spin_lock_bh(&card->mclock);
213 list_for_each_entry_safe(mc, tmp, &card->mc_list, list) {
216 qeth_l2_send_setdelmac(card, mc->mc_addr,
217 IPA_CMD_DELVMAC, NULL);
219 qeth_l2_send_delgroupmac(card, mc->mc_addr);
224 spin_unlock_bh(&card->mclock);
227 static inline int qeth_l2_get_cast_type(struct qeth_card *card,
230 if (card->info.type == QETH_CARD_TYPE_OSN)
232 if (is_broadcast_ether_addr(skb->data))
233 return RTN_BROADCAST;
234 if (is_multicast_ether_addr(skb->data))
235 return RTN_MULTICAST;
239 static void qeth_l2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
240 struct sk_buff *skb, int ipv, int cast_type)
242 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb_mac_header(skb);
244 memset(hdr, 0, sizeof(struct qeth_hdr));
245 hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
247 /* set byte byte 3 to casting flags */
248 if (cast_type == RTN_MULTICAST)
249 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
250 else if (cast_type == RTN_BROADCAST)
251 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
253 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
255 hdr->hdr.l2.pkt_length = skb->len-QETH_HEADER_SIZE;
256 /* VSWITCH relies on the VLAN
257 * information to be present in
259 if (veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)) {
260 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
261 hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
265 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
266 struct qeth_reply *reply, unsigned long data)
268 struct qeth_ipa_cmd *cmd;
270 QETH_CARD_TEXT(card, 2, "L2sdvcb");
271 cmd = (struct qeth_ipa_cmd *) data;
272 if (cmd->hdr.return_code) {
273 QETH_DBF_MESSAGE(2, "Error in processing VLAN %i on %s: 0x%x. "
274 "Continuing\n", cmd->data.setdelvlan.vlan_id,
275 QETH_CARD_IFNAME(card), cmd->hdr.return_code);
276 QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
277 QETH_CARD_TEXT_(card, 2, "err%d", cmd->hdr.return_code);
282 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
283 enum qeth_ipa_cmds ipacmd)
285 struct qeth_ipa_cmd *cmd;
286 struct qeth_cmd_buffer *iob;
288 QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
289 iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
290 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
291 cmd->data.setdelvlan.vlan_id = i;
292 return qeth_send_ipa_cmd(card, iob,
293 qeth_l2_send_setdelvlan_cb, NULL);
296 static void qeth_l2_process_vlans(struct qeth_card *card)
298 struct qeth_vlan_vid *id;
299 QETH_CARD_TEXT(card, 3, "L2prcvln");
300 spin_lock_bh(&card->vlanlock);
301 list_for_each_entry(id, &card->vid_list, list) {
302 qeth_l2_send_setdelvlan(card, id->vid, IPA_CMD_SETVLAN);
304 spin_unlock_bh(&card->vlanlock);
307 static int qeth_l2_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
309 struct qeth_card *card = dev->ml_priv;
310 struct qeth_vlan_vid *id;
312 QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
315 if (card->info.type == QETH_CARD_TYPE_OSM) {
316 QETH_CARD_TEXT(card, 3, "aidOSM");
319 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
320 QETH_CARD_TEXT(card, 3, "aidREC");
323 id = kmalloc(sizeof(struct qeth_vlan_vid), GFP_ATOMIC);
326 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
327 spin_lock_bh(&card->vlanlock);
328 list_add_tail(&id->list, &card->vid_list);
329 spin_unlock_bh(&card->vlanlock);
336 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
338 struct qeth_vlan_vid *id, *tmpid = NULL;
339 struct qeth_card *card = dev->ml_priv;
341 QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
342 if (card->info.type == QETH_CARD_TYPE_OSM) {
343 QETH_CARD_TEXT(card, 3, "kidOSM");
346 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
347 QETH_CARD_TEXT(card, 3, "kidREC");
350 spin_lock_bh(&card->vlanlock);
351 list_for_each_entry(id, &card->vid_list, list) {
352 if (id->vid == vid) {
358 spin_unlock_bh(&card->vlanlock);
360 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
363 qeth_l2_set_multicast_list(card->dev);
367 static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
371 QETH_DBF_TEXT(SETUP , 2, "stopcard");
372 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
374 qeth_set_allowed_threads(card, 0, 1);
375 if (card->read.state == CH_STATE_UP &&
376 card->write.state == CH_STATE_UP &&
377 (card->state == CARD_STATE_UP)) {
379 card->info.type != QETH_CARD_TYPE_OSN) {
380 qeth_l2_stop(card->dev);
383 dev_close(card->dev);
386 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
387 card->state = CARD_STATE_SOFTSETUP;
389 if (card->state == CARD_STATE_SOFTSETUP) {
390 qeth_l2_del_all_mc(card, 0);
391 qeth_clear_ipacmd_list(card);
392 card->state = CARD_STATE_HARDSETUP;
394 if (card->state == CARD_STATE_HARDSETUP) {
395 qeth_qdio_clear_card(card, 0);
396 qeth_clear_qdio_buffers(card);
397 qeth_clear_working_pool_list(card);
398 card->state = CARD_STATE_DOWN;
400 if (card->state == CARD_STATE_DOWN) {
401 qeth_clear_cmd_buffers(&card->read);
402 qeth_clear_cmd_buffers(&card->write);
407 static int qeth_l2_process_inbound_buffer(struct qeth_card *card,
408 int budget, int *done)
412 struct qeth_hdr *hdr;
418 skb = qeth_core_get_next_skb(card,
419 &card->qdio.in_q->bufs[card->rx.b_index],
420 &card->rx.b_element, &card->rx.e_offset, &hdr);
425 skb->dev = card->dev;
426 switch (hdr->hdr.l2.id) {
427 case QETH_HEADER_TYPE_LAYER2:
428 skb->pkt_type = PACKET_HOST;
429 skb->protocol = eth_type_trans(skb, skb->dev);
430 skb->ip_summed = CHECKSUM_NONE;
431 if (skb->protocol == htons(ETH_P_802_2))
432 *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
434 netif_receive_skb(skb);
436 case QETH_HEADER_TYPE_OSN:
437 if (card->info.type == QETH_CARD_TYPE_OSN) {
438 skb_push(skb, sizeof(struct qeth_hdr));
439 skb_copy_to_linear_data(skb, hdr,
440 sizeof(struct qeth_hdr));
442 card->osn_info.data_cb(skb);
447 dev_kfree_skb_any(skb);
448 QETH_CARD_TEXT(card, 3, "inbunkno");
449 QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
454 card->stats.rx_packets++;
455 card->stats.rx_bytes += len;
460 static int qeth_l2_poll(struct napi_struct *napi, int budget)
462 struct qeth_card *card = container_of(napi, struct qeth_card, napi);
464 struct qeth_qdio_buffer *buffer;
466 int new_budget = budget;
468 if (card->options.performance_stats) {
469 card->perf_stats.inbound_cnt++;
470 card->perf_stats.inbound_start_time = qeth_get_micros();
474 if (!card->rx.b_count) {
475 card->rx.qdio_err = 0;
476 card->rx.b_count = qdio_get_next_buffers(
477 card->data.ccwdev, 0, &card->rx.b_index,
479 if (card->rx.b_count <= 0) {
480 card->rx.b_count = 0;
484 &card->qdio.in_q->bufs[card->rx.b_index]
486 card->rx.e_offset = 0;
489 while (card->rx.b_count) {
490 buffer = &card->qdio.in_q->bufs[card->rx.b_index];
491 if (!(card->rx.qdio_err &&
492 qeth_check_qdio_errors(card, buffer->buffer,
493 card->rx.qdio_err, "qinerr")))
494 work_done += qeth_l2_process_inbound_buffer(
495 card, new_budget, &done);
500 if (card->options.performance_stats)
501 card->perf_stats.bufs_rec++;
502 qeth_put_buffer_pool_entry(card,
504 qeth_queue_input_buffer(card, card->rx.b_index);
506 if (card->rx.b_count) {
508 (card->rx.b_index + 1) %
509 QDIO_MAX_BUFFERS_PER_Q;
512 ->bufs[card->rx.b_index]
514 card->rx.e_offset = 0;
518 if (work_done >= budget)
521 new_budget = budget - work_done;
526 if (qdio_start_irq(card->data.ccwdev, 0))
527 napi_schedule(&card->napi);
529 if (card->options.performance_stats)
530 card->perf_stats.inbound_time += qeth_get_micros() -
531 card->perf_stats.inbound_start_time;
535 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
536 enum qeth_ipa_cmds ipacmd,
537 int (*reply_cb) (struct qeth_card *,
541 struct qeth_ipa_cmd *cmd;
542 struct qeth_cmd_buffer *iob;
544 QETH_CARD_TEXT(card, 2, "L2sdmac");
545 iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
546 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
547 cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
548 memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
549 return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
552 static int qeth_l2_send_setmac_cb(struct qeth_card *card,
553 struct qeth_reply *reply,
556 struct qeth_ipa_cmd *cmd;
558 QETH_CARD_TEXT(card, 2, "L2Smaccb");
559 cmd = (struct qeth_ipa_cmd *) data;
560 if (cmd->hdr.return_code) {
561 QETH_CARD_TEXT_(card, 2, "L2er%x", cmd->hdr.return_code);
562 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
563 switch (cmd->hdr.return_code) {
564 case IPA_RC_L2_DUP_MAC:
565 case IPA_RC_L2_DUP_LAYER3_MAC:
566 dev_warn(&card->gdev->dev,
567 "MAC address %pM already exists\n",
568 cmd->data.setdelmac.mac);
570 case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
571 case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
572 dev_warn(&card->gdev->dev,
573 "MAC address %pM is not authorized\n",
574 cmd->data.setdelmac.mac);
580 card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
581 memcpy(card->dev->dev_addr, cmd->data.setdelmac.mac,
583 dev_info(&card->gdev->dev,
584 "MAC address %pM successfully registered on device %s\n",
585 card->dev->dev_addr, card->dev->name);
590 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
592 QETH_CARD_TEXT(card, 2, "L2Setmac");
593 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
594 qeth_l2_send_setmac_cb);
597 static int qeth_l2_send_delmac_cb(struct qeth_card *card,
598 struct qeth_reply *reply,
601 struct qeth_ipa_cmd *cmd;
603 QETH_CARD_TEXT(card, 2, "L2Dmaccb");
604 cmd = (struct qeth_ipa_cmd *) data;
605 if (cmd->hdr.return_code) {
606 QETH_CARD_TEXT_(card, 2, "err%d", cmd->hdr.return_code);
609 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
614 static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
616 QETH_CARD_TEXT(card, 2, "L2Delmac");
617 if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
619 return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
620 qeth_l2_send_delmac_cb);
623 static int qeth_l2_request_initial_mac(struct qeth_card *card)
626 char vendor_pre[] = {0x02, 0x00, 0x00};
628 QETH_DBF_TEXT(SETUP, 2, "doL2init");
629 QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
631 rc = qeth_query_setadapterparms(card);
633 QETH_DBF_MESSAGE(2, "could not query adapter parameters on "
634 "device %s: x%x\n", CARD_BUS_ID(card), rc);
637 if (card->info.type == QETH_CARD_TYPE_IQD ||
638 card->info.type == QETH_CARD_TYPE_OSM ||
639 card->info.type == QETH_CARD_TYPE_OSX ||
640 card->info.guestlan) {
641 rc = qeth_setadpparms_change_macaddr(card);
643 QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
644 "device %s: x%x\n", CARD_BUS_ID(card), rc);
645 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
648 QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
650 random_ether_addr(card->dev->dev_addr);
651 memcpy(card->dev->dev_addr, vendor_pre, 3);
656 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
658 struct sockaddr *addr = p;
659 struct qeth_card *card = dev->ml_priv;
662 QETH_CARD_TEXT(card, 3, "setmac");
664 if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
665 QETH_CARD_TEXT(card, 3, "setmcINV");
669 if (card->info.type == QETH_CARD_TYPE_OSN ||
670 card->info.type == QETH_CARD_TYPE_OSM ||
671 card->info.type == QETH_CARD_TYPE_OSX) {
672 QETH_CARD_TEXT(card, 3, "setmcTYP");
675 QETH_CARD_HEX(card, 3, addr->sa_data, OSA_ADDR_LEN);
676 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
677 QETH_CARD_TEXT(card, 3, "setmcREC");
680 rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
682 rc = qeth_l2_send_setmac(card, addr->sa_data);
683 return rc ? -EINVAL : 0;
686 static void qeth_l2_set_multicast_list(struct net_device *dev)
688 struct qeth_card *card = dev->ml_priv;
689 struct netdev_hw_addr *ha;
691 if (card->info.type == QETH_CARD_TYPE_OSN)
694 QETH_CARD_TEXT(card, 3, "setmulti");
695 if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
696 (card->state != CARD_STATE_UP))
698 qeth_l2_del_all_mc(card, 1);
699 spin_lock_bh(&card->mclock);
700 netdev_for_each_mc_addr(ha, dev)
701 qeth_l2_add_mc(card, ha->addr, 0);
703 netdev_for_each_uc_addr(ha, dev)
704 qeth_l2_add_mc(card, ha->addr, 1);
706 spin_unlock_bh(&card->mclock);
707 if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
709 qeth_setadp_promisc_mode(card);
712 static int qeth_l2_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
715 struct qeth_hdr *hdr = NULL;
717 struct qeth_card *card = dev->ml_priv;
718 struct sk_buff *new_skb = skb;
719 int ipv = qeth_get_ip_version(skb);
720 int cast_type = qeth_l2_get_cast_type(card, skb);
721 struct qeth_qdio_out_q *queue = card->qdio.out_qs
722 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
723 int tx_bytes = skb->len;
724 int data_offset = -1;
725 int elements_needed = 0;
728 if ((card->state != CARD_STATE_UP) || !card->lan_online) {
729 card->stats.tx_carrier_errors++;
733 if ((card->info.type == QETH_CARD_TYPE_OSN) &&
734 (skb->protocol == htons(ETH_P_IPV6)))
737 if (card->options.performance_stats) {
738 card->perf_stats.outbound_cnt++;
739 card->perf_stats.outbound_start_time = qeth_get_micros();
741 netif_stop_queue(dev);
743 if (card->info.type == QETH_CARD_TYPE_OSN)
744 hdr = (struct qeth_hdr *)skb->data;
746 if (card->info.type == QETH_CARD_TYPE_IQD) {
748 data_offset = ETH_HLEN;
750 hdr = kmem_cache_alloc(qeth_core_header_cache,
755 skb_reset_mac_header(new_skb);
756 qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
757 hdr->hdr.l2.pkt_length = new_skb->len;
758 memcpy(((char *)hdr) + sizeof(struct qeth_hdr),
759 skb_mac_header(new_skb), ETH_HLEN);
761 /* create a clone with writeable headroom */
762 new_skb = skb_realloc_headroom(skb,
763 sizeof(struct qeth_hdr));
766 hdr = (struct qeth_hdr *)skb_push(new_skb,
767 sizeof(struct qeth_hdr));
768 skb_set_mac_header(new_skb, sizeof(struct qeth_hdr));
769 qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
773 elements = qeth_get_elements_no(card, (void *)hdr, new_skb,
776 if (data_offset >= 0)
777 kmem_cache_free(qeth_core_header_cache, hdr);
781 if (card->info.type != QETH_CARD_TYPE_IQD) {
782 if (qeth_hdr_chk_and_bounce(new_skb,
783 sizeof(struct qeth_hdr_layer2)))
785 rc = qeth_do_send_packet(card, queue, new_skb, hdr,
788 rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
789 elements, data_offset, hd_len);
791 card->stats.tx_packets++;
792 card->stats.tx_bytes += tx_bytes;
794 dev_kfree_skb_any(skb);
797 if (data_offset >= 0)
798 kmem_cache_free(qeth_core_header_cache, hdr);
802 dev_kfree_skb_any(new_skb);
803 return NETDEV_TX_BUSY;
808 netif_wake_queue(dev);
809 if (card->options.performance_stats)
810 card->perf_stats.outbound_time += qeth_get_micros() -
811 card->perf_stats.outbound_start_time;
815 card->stats.tx_dropped++;
816 card->stats.tx_errors++;
817 if ((new_skb != skb) && new_skb)
818 dev_kfree_skb_any(new_skb);
819 dev_kfree_skb_any(skb);
820 netif_wake_queue(dev);
824 static int __qeth_l2_open(struct net_device *dev)
826 struct qeth_card *card = dev->ml_priv;
829 QETH_CARD_TEXT(card, 4, "qethopen");
830 if (card->state == CARD_STATE_UP)
832 if (card->state != CARD_STATE_SOFTSETUP)
835 if ((card->info.type != QETH_CARD_TYPE_OSN) &&
836 (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
837 QETH_CARD_TEXT(card, 4, "nomacadr");
840 card->data.state = CH_STATE_UP;
841 card->state = CARD_STATE_UP;
842 netif_start_queue(dev);
844 if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
845 napi_enable(&card->napi);
846 napi_schedule(&card->napi);
852 static int qeth_l2_open(struct net_device *dev)
854 struct qeth_card *card = dev->ml_priv;
856 QETH_CARD_TEXT(card, 5, "qethope_");
857 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
858 QETH_CARD_TEXT(card, 3, "openREC");
861 return __qeth_l2_open(dev);
864 static int qeth_l2_stop(struct net_device *dev)
866 struct qeth_card *card = dev->ml_priv;
868 QETH_CARD_TEXT(card, 4, "qethstop");
869 netif_tx_disable(dev);
870 if (card->state == CARD_STATE_UP) {
871 card->state = CARD_STATE_SOFTSETUP;
872 napi_disable(&card->napi);
877 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
879 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
881 INIT_LIST_HEAD(&card->vid_list);
882 INIT_LIST_HEAD(&card->mc_list);
883 card->options.layer2 = 1;
884 card->info.hwtrap = 0;
885 card->discipline.start_poll = qeth_qdio_start_poll;
886 card->discipline.input_handler = (qdio_handler_t *)
887 qeth_qdio_input_handler;
888 card->discipline.output_handler = (qdio_handler_t *)
889 qeth_qdio_output_handler;
890 card->discipline.recover = qeth_l2_recover;
894 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
896 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
898 qeth_set_allowed_threads(card, 0, 1);
899 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
901 if (cgdev->state == CCWGROUP_ONLINE)
902 qeth_l2_set_offline(cgdev);
905 unregister_netdev(card->dev);
911 static const struct ethtool_ops qeth_l2_ethtool_ops = {
912 .get_link = ethtool_op_get_link,
913 .get_strings = qeth_core_get_strings,
914 .get_ethtool_stats = qeth_core_get_ethtool_stats,
915 .get_sset_count = qeth_core_get_sset_count,
916 .get_drvinfo = qeth_core_get_drvinfo,
917 .get_settings = qeth_core_ethtool_get_settings,
920 static const struct ethtool_ops qeth_l2_osn_ops = {
921 .get_strings = qeth_core_get_strings,
922 .get_ethtool_stats = qeth_core_get_ethtool_stats,
923 .get_sset_count = qeth_core_get_sset_count,
924 .get_drvinfo = qeth_core_get_drvinfo,
927 static const struct net_device_ops qeth_l2_netdev_ops = {
928 .ndo_open = qeth_l2_open,
929 .ndo_stop = qeth_l2_stop,
930 .ndo_get_stats = qeth_get_stats,
931 .ndo_start_xmit = qeth_l2_hard_start_xmit,
932 .ndo_validate_addr = eth_validate_addr,
933 .ndo_set_rx_mode = qeth_l2_set_multicast_list,
934 .ndo_do_ioctl = qeth_l2_do_ioctl,
935 .ndo_set_mac_address = qeth_l2_set_mac_address,
936 .ndo_change_mtu = qeth_change_mtu,
937 .ndo_vlan_rx_add_vid = qeth_l2_vlan_rx_add_vid,
938 .ndo_vlan_rx_kill_vid = qeth_l2_vlan_rx_kill_vid,
939 .ndo_tx_timeout = qeth_tx_timeout,
942 static int qeth_l2_setup_netdev(struct qeth_card *card)
944 switch (card->info.type) {
945 case QETH_CARD_TYPE_IQD:
946 card->dev = alloc_netdev(0, "hsi%d", ether_setup);
948 case QETH_CARD_TYPE_OSN:
949 card->dev = alloc_netdev(0, "osn%d", ether_setup);
950 card->dev->flags |= IFF_NOARP;
953 card->dev = alloc_etherdev(0);
959 card->dev->ml_priv = card;
960 card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
961 card->dev->mtu = card->info.initial_mtu;
962 card->dev->netdev_ops = &qeth_l2_netdev_ops;
963 if (card->info.type != QETH_CARD_TYPE_OSN)
964 SET_ETHTOOL_OPS(card->dev, &qeth_l2_ethtool_ops);
966 SET_ETHTOOL_OPS(card->dev, &qeth_l2_osn_ops);
967 card->dev->features |= NETIF_F_HW_VLAN_FILTER;
968 card->info.broadcast_capable = 1;
969 qeth_l2_request_initial_mac(card);
970 SET_NETDEV_DEV(card->dev, &card->gdev->dev);
971 netif_napi_add(card->dev, &card->napi, qeth_l2_poll, QETH_NAPI_WEIGHT);
972 return register_netdev(card->dev);
975 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
977 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
979 enum qeth_card_states recover_flag;
982 mutex_lock(&card->discipline_mutex);
983 mutex_lock(&card->conf_mutex);
984 QETH_DBF_TEXT(SETUP, 2, "setonlin");
985 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
987 recover_flag = card->state;
988 rc = qeth_core_hardsetup_card(card);
990 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
995 if (!card->dev && qeth_l2_setup_netdev(card)) {
1000 if (card->info.type != QETH_CARD_TYPE_OSN)
1001 qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
1003 if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
1004 if (card->info.hwtrap &&
1005 qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
1006 card->info.hwtrap = 0;
1008 card->info.hwtrap = 0;
1010 card->state = CARD_STATE_HARDSETUP;
1011 memset(&card->rx, 0, sizeof(struct qeth_rx));
1012 qeth_print_status_message(card);
1015 QETH_DBF_TEXT(SETUP, 2, "softsetp");
1017 rc = qeth_send_startlan(card);
1019 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1021 dev_warn(&card->gdev->dev,
1022 "The LAN is offline\n");
1023 card->lan_online = 0;
1029 card->lan_online = 1;
1032 if ((card->info.type == QETH_CARD_TYPE_OSD) ||
1033 (card->info.type == QETH_CARD_TYPE_OSX))
1034 /* configure isolation level */
1035 qeth_set_access_ctrl_online(card);
1037 if (card->info.type != QETH_CARD_TYPE_OSN &&
1038 card->info.type != QETH_CARD_TYPE_OSM)
1039 qeth_l2_process_vlans(card);
1041 netif_tx_disable(card->dev);
1043 rc = qeth_init_qdio_queues(card);
1045 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1049 card->state = CARD_STATE_SOFTSETUP;
1050 if (card->lan_online)
1051 netif_carrier_on(card->dev);
1053 netif_carrier_off(card->dev);
1055 qeth_set_allowed_threads(card, 0xffffffff, 0);
1056 if (recover_flag == CARD_STATE_RECOVER) {
1057 if (recovery_mode &&
1058 card->info.type != QETH_CARD_TYPE_OSN) {
1059 __qeth_l2_open(card->dev);
1062 dev_open(card->dev);
1065 /* this also sets saved unicast addresses */
1066 qeth_l2_set_multicast_list(card->dev);
1068 /* let user_space know that device is online */
1069 kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1070 mutex_unlock(&card->conf_mutex);
1071 mutex_unlock(&card->discipline_mutex);
1075 qeth_l2_stop_card(card, 0);
1076 ccw_device_set_offline(CARD_DDEV(card));
1077 ccw_device_set_offline(CARD_WDEV(card));
1078 ccw_device_set_offline(CARD_RDEV(card));
1079 if (recover_flag == CARD_STATE_RECOVER)
1080 card->state = CARD_STATE_RECOVER;
1082 card->state = CARD_STATE_DOWN;
1083 mutex_unlock(&card->conf_mutex);
1084 mutex_unlock(&card->discipline_mutex);
1088 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1090 return __qeth_l2_set_online(gdev, 0);
1093 static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1096 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1097 int rc = 0, rc2 = 0, rc3 = 0;
1098 enum qeth_card_states recover_flag;
1100 mutex_lock(&card->discipline_mutex);
1101 mutex_lock(&card->conf_mutex);
1102 QETH_DBF_TEXT(SETUP, 3, "setoffl");
1103 QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1105 if (card->dev && netif_carrier_ok(card->dev))
1106 netif_carrier_off(card->dev);
1107 recover_flag = card->state;
1108 if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
1109 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1110 card->info.hwtrap = 1;
1112 qeth_l2_stop_card(card, recovery_mode);
1113 rc = ccw_device_set_offline(CARD_DDEV(card));
1114 rc2 = ccw_device_set_offline(CARD_WDEV(card));
1115 rc3 = ccw_device_set_offline(CARD_RDEV(card));
1117 rc = (rc2) ? rc2 : rc3;
1119 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1120 if (recover_flag == CARD_STATE_UP)
1121 card->state = CARD_STATE_RECOVER;
1122 /* let user_space know that device is offline */
1123 kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1124 mutex_unlock(&card->conf_mutex);
1125 mutex_unlock(&card->discipline_mutex);
1129 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1131 return __qeth_l2_set_offline(cgdev, 0);
1134 static int qeth_l2_recover(void *ptr)
1136 struct qeth_card *card;
1139 card = (struct qeth_card *) ptr;
1140 QETH_CARD_TEXT(card, 2, "recover1");
1141 if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1143 QETH_CARD_TEXT(card, 2, "recover2");
1144 dev_warn(&card->gdev->dev,
1145 "A recovery process has been started for the device\n");
1146 __qeth_l2_set_offline(card->gdev, 1);
1147 rc = __qeth_l2_set_online(card->gdev, 1);
1149 dev_info(&card->gdev->dev,
1150 "Device successfully recovered!\n");
1153 dev_close(card->dev);
1155 dev_warn(&card->gdev->dev, "The qeth device driver "
1156 "failed to recover an error on the device\n");
1158 qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1159 qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1163 static int __init qeth_l2_init(void)
1165 pr_info("register layer 2 discipline\n");
1169 static void __exit qeth_l2_exit(void)
1171 pr_info("unregister layer 2 discipline\n");
1174 static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1176 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1177 qeth_set_allowed_threads(card, 0, 1);
1178 if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
1179 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1180 qeth_qdio_clear_card(card, 0);
1181 qeth_clear_qdio_buffers(card);
1184 static int qeth_l2_pm_suspend(struct ccwgroup_device *gdev)
1186 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1189 netif_device_detach(card->dev);
1190 qeth_set_allowed_threads(card, 0, 1);
1191 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1192 if (gdev->state == CCWGROUP_OFFLINE)
1194 if (card->state == CARD_STATE_UP) {
1195 if (card->info.hwtrap)
1196 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1197 __qeth_l2_set_offline(card->gdev, 1);
1199 __qeth_l2_set_offline(card->gdev, 0);
1203 static int qeth_l2_pm_resume(struct ccwgroup_device *gdev)
1205 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1208 if (gdev->state == CCWGROUP_OFFLINE)
1211 if (card->state == CARD_STATE_RECOVER) {
1212 rc = __qeth_l2_set_online(card->gdev, 1);
1215 dev_close(card->dev);
1219 rc = __qeth_l2_set_online(card->gdev, 0);
1221 qeth_set_allowed_threads(card, 0xffffffff, 0);
1223 netif_device_attach(card->dev);
1225 dev_warn(&card->gdev->dev, "The qeth device driver "
1226 "failed to recover an error on the device\n");
1230 struct ccwgroup_driver qeth_l2_ccwgroup_driver = {
1231 .probe = qeth_l2_probe_device,
1232 .remove = qeth_l2_remove_device,
1233 .set_online = qeth_l2_set_online,
1234 .set_offline = qeth_l2_set_offline,
1235 .shutdown = qeth_l2_shutdown,
1236 .freeze = qeth_l2_pm_suspend,
1237 .thaw = qeth_l2_pm_resume,
1238 .restore = qeth_l2_pm_resume,
1240 EXPORT_SYMBOL_GPL(qeth_l2_ccwgroup_driver);
1242 static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1243 struct qeth_cmd_buffer *iob)
1245 unsigned long flags;
1248 QETH_CARD_TEXT(card, 5, "osndctrd");
1250 wait_event(card->wait_q,
1251 atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1252 qeth_prepare_control_data(card, len, iob);
1253 QETH_CARD_TEXT(card, 6, "osnoirqp");
1254 spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1255 rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1256 (addr_t) iob, 0, 0);
1257 spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1259 QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
1260 "ccw_device_start rc = %i\n", rc);
1261 QETH_CARD_TEXT_(card, 2, " err%d", rc);
1262 qeth_release_buffer(iob->channel, iob);
1263 atomic_set(&card->write.irq_pending, 0);
1264 wake_up(&card->wait_q);
1269 static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1270 struct qeth_cmd_buffer *iob, int data_len)
1274 QETH_CARD_TEXT(card, 4, "osndipa");
1276 qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1277 s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1279 memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1280 memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1281 memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1282 memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1283 return qeth_osn_send_control_data(card, s1, iob);
1286 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1288 struct qeth_cmd_buffer *iob;
1289 struct qeth_card *card;
1294 card = dev->ml_priv;
1297 QETH_CARD_TEXT(card, 2, "osnsdmc");
1298 if ((card->state != CARD_STATE_UP) &&
1299 (card->state != CARD_STATE_SOFTSETUP))
1301 iob = qeth_wait_for_buffer(&card->write);
1302 memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1303 rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1306 EXPORT_SYMBOL(qeth_osn_assist);
1308 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1309 int (*assist_cb)(struct net_device *, void *),
1310 int (*data_cb)(struct sk_buff *))
1312 struct qeth_card *card;
1314 *dev = qeth_l2_netdev_by_devno(read_dev_no);
1317 card = (*dev)->ml_priv;
1320 QETH_CARD_TEXT(card, 2, "osnreg");
1321 if ((assist_cb == NULL) || (data_cb == NULL))
1323 card->osn_info.assist_cb = assist_cb;
1324 card->osn_info.data_cb = data_cb;
1327 EXPORT_SYMBOL(qeth_osn_register);
1329 void qeth_osn_deregister(struct net_device *dev)
1331 struct qeth_card *card;
1335 card = dev->ml_priv;
1338 QETH_CARD_TEXT(card, 2, "osndereg");
1339 card->osn_info.assist_cb = NULL;
1340 card->osn_info.data_cb = NULL;
1343 EXPORT_SYMBOL(qeth_osn_deregister);
1345 module_init(qeth_l2_init);
1346 module_exit(qeth_l2_exit);
1347 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1348 MODULE_DESCRIPTION("qeth layer 2 discipline");
1349 MODULE_LICENSE("GPL");