Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux...
[linux-2.6] / drivers / s390 / net / qeth_l2_main.c
1 /*
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
11 #define KMSG_COMPONENT "qeth"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
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/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
28 static int qeth_l2_set_offline(struct ccwgroup_device *);
29 static int qeth_l2_stop(struct net_device *);
30 static int qeth_l2_send_delmac(struct qeth_card *, __u8 *);
31 static 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));
36 static void qeth_l2_set_multicast_list(struct net_device *);
37 static int qeth_l2_recover(void *);
38
39 static int qeth_l2_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
40 {
41         struct qeth_card *card = dev->ml_priv;
42         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)
81                 QETH_DBF_TEXT_(TRACE, 2, "ioce%d", rc);
82         return rc;
83 }
84
85 static 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
103 static struct net_device *qeth_l2_netdev_by_devno(unsigned char *read_dev_no)
104 {
105         struct qeth_card *card;
106         struct net_device *ndev;
107         __u16 temp_dev_no;
108         unsigned long flags;
109         struct ccw_dev_id read_devid;
110
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) {
115                 ccw_device_get_id(CARD_RDEV(card), &read_devid);
116                 if (read_devid.devno == temp_dev_no) {
117                         ndev = card->dev;
118                         break;
119                 }
120         }
121         read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
122         return ndev;
123 }
124
125 static 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
132         QETH_DBF_TEXT(TRACE, 2, "L2Sgmacb");
133         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) {
137                 QETH_DBF_MESSAGE(2, "Group MAC %pM already existing on %s \n",
138                           mac, QETH_CARD_IFNAME(card));
139                 cmd->hdr.return_code = 0;
140         }
141         if (cmd->hdr.return_code)
142                 QETH_DBF_MESSAGE(2, "Could not set group MAC %pM on %s: %x\n",
143                           mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
144         return 0;
145 }
146
147 static int qeth_l2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
148 {
149         QETH_DBF_TEXT(TRACE, 2, "L2Sgmac");
150         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
151                                           qeth_l2_send_setgroupmac_cb);
152 }
153
154 static 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
161         QETH_DBF_TEXT(TRACE, 2, "L2Dgmacb");
162         cmd = (struct qeth_ipa_cmd *) data;
163         mac = &cmd->data.setdelmac.mac[0];
164         if (cmd->hdr.return_code)
165                 QETH_DBF_MESSAGE(2, "Could not delete group MAC %pM on %s: %x\n",
166                           mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
167         return 0;
168 }
169
170 static int qeth_l2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
171 {
172         QETH_DBF_TEXT(TRACE, 2, "L2Dgmac");
173         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
174                                           qeth_l2_send_delgroupmac_cb);
175 }
176
177 static void qeth_l2_add_mc(struct qeth_card *card, __u8 *mac, int vmac)
178 {
179         struct qeth_mc_mac *mc;
180         int rc;
181
182         mc = kmalloc(sizeof(struct qeth_mc_mac), GFP_ATOMIC);
183
184         if (!mc)
185                 return;
186
187         memcpy(mc->mc_addr, mac, OSA_ADDR_LEN);
188         mc->mc_addrlen = OSA_ADDR_LEN;
189         mc->is_vmac = vmac;
190
191         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)
199                 list_add_tail(&mc->list, &card->mc_list);
200         else
201                 kfree(mc);
202 }
203
204 static 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) {
210                 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);
215                 list_del(&mc->list);
216                 kfree(mc);
217         }
218         spin_unlock_bh(&card->mclock);
219 }
220
221 static 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
253 static void qeth_l2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
254                         struct sk_buff *skb, int ipv, int cast_type)
255 {
256         struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb_mac_header(skb);
257
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
279 static 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
284         QETH_DBF_TEXT(TRACE, 2, "L2sdvcb");
285         cmd = (struct qeth_ipa_cmd *) data;
286         if (cmd->hdr.return_code) {
287                 QETH_DBF_MESSAGE(2, "Error in processing VLAN %i on %s: 0x%x. "
288                           "Continuing\n", cmd->data.setdelvlan.vlan_id,
289                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
290                 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);
293         }
294         return 0;
295 }
296
297 static 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
303         QETH_DBF_TEXT_(TRACE, 4, "L2sdv%x", ipacmd);
304         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
311 static void qeth_l2_process_vlans(struct qeth_card *card, int clear)
312 {
313         struct qeth_vlan_vid *id;
314         QETH_DBF_TEXT(TRACE, 3, "L2prcvln");
315         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
327 static void qeth_l2_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
328 {
329         struct qeth_card *card = dev->ml_priv;
330         struct qeth_vlan_vid *id;
331
332         QETH_DBF_TEXT_(TRACE, 4, "aid:%d", vid);
333         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);
340         }
341 }
342
343 static void qeth_l2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
344 {
345         struct qeth_vlan_vid *id, *tmpid = NULL;
346         struct qeth_card *card = dev->ml_priv;
347
348         QETH_DBF_TEXT_(TRACE, 4, "kid:%d", vid);
349         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
365 static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
366 {
367         int rc = 0;
368
369         QETH_DBF_TEXT(SETUP , 2, "stopcard");
370         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
371
372         qeth_set_allowed_threads(card, 0, 1);
373         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);
387                         QETH_DBF_TEXT_(SETUP, 2, "Lerr%d", rc);
388                 }
389                 card->state = CARD_STATE_SOFTSETUP;
390         }
391         if (card->state == CARD_STATE_SOFTSETUP) {
392                 qeth_l2_process_vlans(card, 1);
393                 if (!card->use_hard_stop)
394                         qeth_l2_del_all_mc(card);
395                 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
412 static 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;
443                         if (skb->protocol == htons(ETH_P_802_2))
444                                 *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
445                         len = skb->len;
446                         netif_rx(skb);
447                         break;
448                 case QETH_HEADER_TYPE_OSN:
449                         if (card->info.type == QETH_CARD_TYPE_OSN) {
450                                 skb_push(skb, sizeof(struct qeth_hdr));
451                                 skb_copy_to_linear_data(skb, hdr,
452                                                 sizeof(struct qeth_hdr));
453                                 len = skb->len;
454                                 card->osn_info.data_cb(skb);
455                                 break;
456                         }
457                         /* else unknown */
458                 default:
459                         dev_kfree_skb_any(skb);
460                         QETH_DBF_TEXT(TRACE, 3, "inbunkno");
461                         QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
462                         continue;
463                 }
464                 card->dev->last_rx = jiffies;
465                 card->stats.rx_packets++;
466                 card->stats.rx_bytes += len;
467         }
468 }
469
470 static 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
479         QETH_DBF_TEXT(TRACE, 2, "L2sdmac");
480         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
487 static 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
493         QETH_DBF_TEXT(TRACE, 2, "L2Smaccb");
494         cmd = (struct qeth_ipa_cmd *) data;
495         if (cmd->hdr.return_code) {
496                 QETH_DBF_TEXT_(TRACE, 2, "L2er%x", cmd->hdr.return_code);
497                 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);
503                 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);
510         }
511         return 0;
512 }
513
514 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
515 {
516         QETH_DBF_TEXT(TRACE, 2, "L2Setmac");
517         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
518                                           qeth_l2_send_setmac_cb);
519 }
520
521 static 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
527         QETH_DBF_TEXT(TRACE, 2, "L2Dmaccb");
528         cmd = (struct qeth_ipa_cmd *) data;
529         if (cmd->hdr.return_code) {
530                 QETH_DBF_TEXT_(TRACE, 2, "err%d", cmd->hdr.return_code);
531                 cmd->hdr.return_code = -EIO;
532                 return 0;
533         }
534         card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
535
536         return 0;
537 }
538
539 static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
540 {
541         QETH_DBF_TEXT(TRACE, 2, "L2Delmac");
542         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
548 static int qeth_l2_request_initial_mac(struct qeth_card *card)
549 {
550         int rc = 0;
551         char vendor_pre[] = {0x02, 0x00, 0x00};
552
553         QETH_DBF_TEXT(SETUP, 2, "doL2init");
554         QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
555
556         rc = qeth_query_setadapterparms(card);
557         if (rc) {
558                 QETH_DBF_MESSAGE(2, "could not query adapter parameters on "
559                         "device %s: x%x\n", CARD_BUS_ID(card), rc);
560         }
561
562         if ((card->info.type == QETH_CARD_TYPE_IQD) || 
563             (card->info.guestlan)) {
564                 rc = qeth_setadpparms_change_macaddr(card);
565                 if (rc) {
566                         QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
567                                 "device %s: x%x\n", CARD_BUS_ID(card), rc);
568                         QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
569                         return rc;
570                 }
571                 QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
572         } else {
573                 random_ether_addr(card->dev->dev_addr);
574                 memcpy(card->dev->dev_addr, vendor_pre, 3);
575         }
576         return 0;
577 }
578
579 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
580 {
581         struct sockaddr *addr = p;
582         struct qeth_card *card = dev->ml_priv;
583         int rc = 0;
584
585         QETH_DBF_TEXT(TRACE, 3, "setmac");
586
587         if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
588                 QETH_DBF_TEXT(TRACE, 3, "setmcINV");
589                 return -EOPNOTSUPP;
590         }
591
592         if (card->info.type == QETH_CARD_TYPE_OSN) {
593                 QETH_DBF_TEXT(TRACE, 3, "setmcOSN");
594                 return -EOPNOTSUPP;
595         }
596         QETH_DBF_TEXT_(TRACE, 3, "%s", CARD_BUS_ID(card));
597         QETH_DBF_HEX(TRACE, 3, addr->sa_data, OSA_ADDR_LEN);
598         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
604 static void qeth_l2_set_multicast_list(struct net_device *dev)
605 {
606         struct qeth_card *card = dev->ml_priv;
607         struct dev_addr_list *dm;
608
609         if (card->info.type == QETH_CARD_TYPE_OSN)
610                 return ;
611
612         QETH_DBF_TEXT(TRACE, 3, "setmulti");
613         qeth_l2_del_all_mc(card);
614         spin_lock_bh(&card->mclock);
615         for (dm = dev->mc_list; dm; dm = dm->next)
616                 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
621         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
627 static 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;
632         struct qeth_card *card = dev->ml_priv;
633         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;
641         int data_offset = -1;
642         int elements_needed = 0;
643         int hd_len = 0;
644
645         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 {
666                 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,
684                                                 sizeof(struct qeth_hdr));
685                         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                 }
692         }
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) {
698                         QETH_DBF_MESSAGE(2, "could not create eddp context\n");
699                         goto tx_drop;
700                 }
701         } else {
702                 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);
707                         goto tx_drop;
708                 }
709         }
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,
720                                         elements, ctx, data_offset, hd_len);
721         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
747                 if (data_offset >= 0)
748                         kmem_cache_free(qeth_core_header_cache, hdr);
749
750                 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
764 tx_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);
770         netif_wake_queue(dev);
771         return NETDEV_TX_OK;
772 }
773
774 static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev,
775                         unsigned int qdio_err, unsigned int queue,
776                         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
784         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         }
790         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;
798         }
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];
802                 if (!(qdio_err &&
803                       qeth_check_qdio_errors(buffer->buffer, qdio_err, "qinerr")))
804                         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
814 static int qeth_l2_open(struct net_device *dev)
815 {
816         struct qeth_card *card = dev->ml_priv;
817
818         QETH_DBF_TEXT(TRACE, 4, "qethopen");
819         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))) {
824                 QETH_DBF_TEXT(TRACE, 4, "nomacadr");
825                 return -EPERM;
826         }
827         card->data.state = CH_STATE_UP;
828         card->state = CARD_STATE_UP;
829         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
837 static int qeth_l2_stop(struct net_device *dev)
838 {
839         struct qeth_card *card = dev->ml_priv;
840
841         QETH_DBF_TEXT(TRACE, 4, "qethstop");
842         netif_tx_disable(dev);
843         if (card->state == CARD_STATE_UP)
844                 card->state = CARD_STATE_SOFTSETUP;
845         return 0;
846 }
847
848 static 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
863 static 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
883 static int qeth_l2_ethtool_set_tso(struct net_device *dev, u32 data)
884 {
885         struct qeth_card *card = dev->ml_priv;
886
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
899 static 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,
906         .set_tso = qeth_l2_ethtool_set_tso,
907         .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,
911         .get_settings = qeth_core_ethtool_get_settings,
912 };
913
914 static 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
921 static 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
941         card->dev->ml_priv = card;
942         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
966 static 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);
973         QETH_DBF_TEXT(SETUP, 2, "setonlin");
974         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
975
976         qeth_set_allowed_threads(card, QETH_RECOVER_THREAD, 1);
977         recover_flag = card->state;
978         rc = ccw_device_set_online(CARD_RDEV(card));
979         if (rc) {
980                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
981                 return -EIO;
982         }
983         rc = ccw_device_set_online(CARD_WDEV(card));
984         if (rc) {
985                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
986                 return -EIO;
987         }
988         rc = ccw_device_set_online(CARD_DDEV(card));
989         if (rc) {
990                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
991                 return -EIO;
992         }
993
994         rc = qeth_core_hardsetup_card(card);
995         if (rc) {
996                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
997                 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 */
1010         QETH_DBF_TEXT(SETUP, 2, "softsetp");
1011
1012         rc = qeth_send_startlan(card);
1013         if (rc) {
1014                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1015                 if (rc == 0xe080) {
1016                         dev_warn(&card->gdev->dev,
1017                                 "The LAN is offline\n");
1018                         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) {
1033                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1034                 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;
1055 out_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
1068 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1069 {
1070         return __qeth_l2_set_online(gdev, 0);
1071 }
1072
1073 static 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
1080         QETH_DBF_TEXT(SETUP, 3, "setoffl");
1081         QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1082
1083         if (card->dev && netif_carrier_ok(card->dev))
1084                 netif_carrier_off(card->dev);
1085         recover_flag = card->state;
1086         qeth_l2_stop_card(card, recovery_mode);
1087         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)
1093                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1094         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
1101 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1102 {
1103         return __qeth_l2_set_offline(cgdev, 0);
1104 }
1105
1106 static int qeth_l2_recover(void *ptr)
1107 {
1108         struct qeth_card *card;
1109         int rc = 0;
1110
1111         card = (struct qeth_card *) ptr;
1112         QETH_DBF_TEXT(TRACE, 2, "recover1");
1113         QETH_DBF_HEX(TRACE, 2, &card, sizeof(void *));
1114         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1115                 return 0;
1116         QETH_DBF_TEXT(TRACE, 2, "recover2");
1117         dev_warn(&card->gdev->dev,
1118                 "A recovery process has been started for the device\n");
1119         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)
1126                 dev_info(&card->gdev->dev,
1127                         "Device successfully recovered!\n");
1128         else {
1129                 rtnl_lock();
1130                 dev_close(card->dev);
1131                 rtnl_unlock();
1132                 dev_warn(&card->gdev->dev, "The qeth device driver "
1133                         "failed to recover an error on the device\n");
1134         }
1135         return 0;
1136 }
1137
1138 static int __init qeth_l2_init(void)
1139 {
1140         pr_info("register layer 2 discipline\n");
1141         return 0;
1142 }
1143
1144 static void __exit qeth_l2_exit(void)
1145 {
1146         pr_info("unregister layer 2 discipline\n");
1147 }
1148
1149 static 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
1156 struct 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 };
1163 EXPORT_SYMBOL_GPL(qeth_l2_ccwgroup_driver);
1164
1165 static 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
1171         QETH_DBF_TEXT(TRACE, 5, "osndctrd");
1172
1173         wait_event(card->wait_q,
1174                    atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1175         qeth_prepare_control_data(card, len, iob);
1176         QETH_DBF_TEXT(TRACE, 6, "osnoirqp");
1177         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) {
1182                 QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
1183                            "ccw_device_start rc = %i\n", rc);
1184                 QETH_DBF_TEXT_(TRACE, 2, " err%d", rc);
1185                 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
1192 static 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
1197         QETH_DBF_TEXT(TRACE, 4, "osndipa");
1198
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
1209 int 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
1215         QETH_DBF_TEXT(TRACE, 2, "osnsdmc");
1216         if (!dev)
1217                 return -ENODEV;
1218         card = dev->ml_priv;
1219         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 }
1229 EXPORT_SYMBOL(qeth_osn_assist);
1230
1231 int 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
1237         QETH_DBF_TEXT(TRACE, 2, "osnreg");
1238         *dev = qeth_l2_netdev_by_devno(read_dev_no);
1239         if (*dev == NULL)
1240                 return -ENODEV;
1241         card = (*dev)->ml_priv;
1242         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 }
1250 EXPORT_SYMBOL(qeth_osn_register);
1251
1252 void qeth_osn_deregister(struct net_device *dev)
1253 {
1254         struct qeth_card *card;
1255
1256         QETH_DBF_TEXT(TRACE, 2, "osndereg");
1257         if (!dev)
1258                 return;
1259         card = dev->ml_priv;
1260         if (!card)
1261                 return;
1262         card->osn_info.assist_cb = NULL;
1263         card->osn_info.data_cb = NULL;
1264         return;
1265 }
1266 EXPORT_SYMBOL(qeth_osn_deregister);
1267
1268 module_init(qeth_l2_init);
1269 module_exit(qeth_l2_exit);
1270 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1271 MODULE_DESCRIPTION("qeth layer 2 discipline");
1272 MODULE_LICENSE("GPL");