Merge git://git.infradead.org/mtd-2.6
[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 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/etherdevice.h>
17 #include <linux/mii.h>
18 #include <linux/ip.h>
19
20 #include <asm/s390_rdev.h>
21
22 #include "qeth_core.h"
23 #include "qeth_core_offl.h"
24
25 static int qeth_l2_set_offline(struct ccwgroup_device *);
26 static int qeth_l2_stop(struct net_device *);
27 static int qeth_l2_send_delmac(struct qeth_card *, __u8 *);
28 static int qeth_l2_send_setdelmac(struct qeth_card *, __u8 *,
29                            enum qeth_ipa_cmds,
30                            int (*reply_cb) (struct qeth_card *,
31                                             struct qeth_reply*,
32                                             unsigned long));
33 static void qeth_l2_set_multicast_list(struct net_device *);
34 static int qeth_l2_recover(void *);
35
36 static int qeth_l2_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
37 {
38         struct qeth_card *card = netdev_priv(dev);
39         struct mii_ioctl_data *mii_data;
40         int rc = 0;
41
42         if (!card)
43                 return -ENODEV;
44
45         if ((card->state != CARD_STATE_UP) &&
46                 (card->state != CARD_STATE_SOFTSETUP))
47                 return -ENODEV;
48
49         if (card->info.type == QETH_CARD_TYPE_OSN)
50                 return -EPERM;
51
52         switch (cmd) {
53         case SIOC_QETH_ADP_SET_SNMP_CONTROL:
54                 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
55                 break;
56         case SIOC_QETH_GET_CARD_TYPE:
57                 if ((card->info.type == QETH_CARD_TYPE_OSAE) &&
58                     !card->info.guestlan)
59                         return 1;
60                 return 0;
61                 break;
62         case SIOCGMIIPHY:
63                 mii_data = if_mii(rq);
64                 mii_data->phy_id = 0;
65                 break;
66         case SIOCGMIIREG:
67                 mii_data = if_mii(rq);
68                 if (mii_data->phy_id != 0)
69                         rc = -EINVAL;
70                 else
71                         mii_data->val_out = qeth_mdio_read(dev,
72                                 mii_data->phy_id, mii_data->reg_num);
73                 break;
74         default:
75                 rc = -EOPNOTSUPP;
76         }
77         if (rc)
78                 QETH_DBF_TEXT_(TRACE, 2, "ioce%d", rc);
79         return rc;
80 }
81
82 static int qeth_l2_verify_dev(struct net_device *dev)
83 {
84         struct qeth_card *card;
85         unsigned long flags;
86         int rc = 0;
87
88         read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
89         list_for_each_entry(card, &qeth_core_card_list.list, list) {
90                 if (card->dev == dev) {
91                         rc = QETH_REAL_CARD;
92                         break;
93                 }
94         }
95         read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
96
97         return rc;
98 }
99
100 static struct net_device *qeth_l2_netdev_by_devno(unsigned char *read_dev_no)
101 {
102         struct qeth_card *card;
103         struct net_device *ndev;
104         unsigned char *readno;
105         __u16 temp_dev_no, card_dev_no;
106         char *endp;
107         unsigned long flags;
108
109         ndev = NULL;
110         memcpy(&temp_dev_no, read_dev_no, 2);
111         read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
112         list_for_each_entry(card, &qeth_core_card_list.list, list) {
113                 readno = CARD_RDEV_ID(card);
114                 readno += (strlen(readno) - 4);
115                 card_dev_no = simple_strtoul(readno, &endp, 16);
116                 if (card_dev_no == 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                 PRINT_WARN("Group MAC %02x:%02x:%02x:%02x:%02x:%02x " \
138                           "already existing on %s \n",
139                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
140                           QETH_CARD_IFNAME(card));
141                 cmd->hdr.return_code = 0;
142         }
143         if (cmd->hdr.return_code)
144                 PRINT_ERR("Could not set group MAC " \
145                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
146                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
147                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
148         return 0;
149 }
150
151 static int qeth_l2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
152 {
153         QETH_DBF_TEXT(TRACE, 2, "L2Sgmac");
154         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
155                                           qeth_l2_send_setgroupmac_cb);
156 }
157
158 static int qeth_l2_send_delgroupmac_cb(struct qeth_card *card,
159                                 struct qeth_reply *reply,
160                                 unsigned long data)
161 {
162         struct qeth_ipa_cmd *cmd;
163         __u8 *mac;
164
165         QETH_DBF_TEXT(TRACE, 2, "L2Dgmacb");
166         cmd = (struct qeth_ipa_cmd *) data;
167         mac = &cmd->data.setdelmac.mac[0];
168         if (cmd->hdr.return_code)
169                 PRINT_ERR("Could not delete group MAC " \
170                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
171                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
172                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
173         return 0;
174 }
175
176 static int qeth_l2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
177 {
178         QETH_DBF_TEXT(TRACE, 2, "L2Dgmac");
179         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
180                                           qeth_l2_send_delgroupmac_cb);
181 }
182
183 static void qeth_l2_add_mc(struct qeth_card *card, __u8 *mac)
184 {
185         struct qeth_mc_mac *mc;
186
187         mc = kmalloc(sizeof(struct qeth_mc_mac), GFP_ATOMIC);
188
189         if (!mc) {
190                 PRINT_ERR("no mem vor mc mac address\n");
191                 return;
192         }
193
194         memcpy(mc->mc_addr, mac, OSA_ADDR_LEN);
195         mc->mc_addrlen = OSA_ADDR_LEN;
196
197         if (!qeth_l2_send_setgroupmac(card, mac))
198                 list_add_tail(&mc->list, &card->mc_list);
199         else
200                 kfree(mc);
201 }
202
203 static void qeth_l2_del_all_mc(struct qeth_card *card)
204 {
205         struct qeth_mc_mac *mc, *tmp;
206
207         spin_lock_bh(&card->mclock);
208         list_for_each_entry_safe(mc, tmp, &card->mc_list, list) {
209                 qeth_l2_send_delgroupmac(card, mc->mc_addr);
210                 list_del(&mc->list);
211                 kfree(mc);
212         }
213         spin_unlock_bh(&card->mclock);
214 }
215
216 static void qeth_l2_get_packet_type(struct qeth_card *card,
217                         struct qeth_hdr *hdr, struct sk_buff *skb)
218 {
219         __u16 hdr_mac;
220
221         if (!memcmp(skb->data + QETH_HEADER_SIZE,
222                     skb->dev->broadcast, 6)) {
223                 /* broadcast? */
224                 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
225                 return;
226         }
227         hdr_mac = *((__u16 *)skb->data);
228         /* tr multicast? */
229         switch (card->info.link_type) {
230         case QETH_LINK_TYPE_HSTR:
231         case QETH_LINK_TYPE_LANE_TR:
232                 if ((hdr_mac == QETH_TR_MAC_NC) ||
233                     (hdr_mac == QETH_TR_MAC_C))
234                         hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
235                 else
236                         hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
237                 break;
238                 /* eth or so multicast? */
239         default:
240                 if ((hdr_mac == QETH_ETH_MAC_V4) ||
241                      (hdr_mac == QETH_ETH_MAC_V6))
242                         hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
243                 else
244                         hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
245         }
246 }
247
248 static void qeth_l2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
249                         struct sk_buff *skb, int ipv, int cast_type)
250 {
251         struct vlan_ethhdr *veth = (struct vlan_ethhdr *)((skb->data) +
252                                         QETH_HEADER_SIZE);
253
254         memset(hdr, 0, sizeof(struct qeth_hdr));
255         hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
256
257         /* set byte byte 3 to casting flags */
258         if (cast_type == RTN_MULTICAST)
259                 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
260         else if (cast_type == RTN_BROADCAST)
261                 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
262         else
263                 qeth_l2_get_packet_type(card, hdr, skb);
264
265         hdr->hdr.l2.pkt_length = skb->len-QETH_HEADER_SIZE;
266         /* VSWITCH relies on the VLAN
267          * information to be present in
268          * the QDIO header */
269         if (veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)) {
270                 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
271                 hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
272         }
273 }
274
275 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
276                         struct qeth_reply *reply, unsigned long data)
277 {
278         struct qeth_ipa_cmd *cmd;
279
280         QETH_DBF_TEXT(TRACE, 2, "L2sdvcb");
281         cmd = (struct qeth_ipa_cmd *) data;
282         if (cmd->hdr.return_code) {
283                 PRINT_ERR("Error in processing VLAN %i on %s: 0x%x. "
284                           "Continuing\n", cmd->data.setdelvlan.vlan_id,
285                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
286                 QETH_DBF_TEXT_(TRACE, 2, "L2VL%4x", cmd->hdr.command);
287                 QETH_DBF_TEXT_(TRACE, 2, "L2%s", CARD_BUS_ID(card));
288                 QETH_DBF_TEXT_(TRACE, 2, "err%d", cmd->hdr.return_code);
289         }
290         return 0;
291 }
292
293 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
294                                 enum qeth_ipa_cmds ipacmd)
295 {
296         struct qeth_ipa_cmd *cmd;
297         struct qeth_cmd_buffer *iob;
298
299         QETH_DBF_TEXT_(TRACE, 4, "L2sdv%x", ipacmd);
300         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
301         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
302         cmd->data.setdelvlan.vlan_id = i;
303         return qeth_send_ipa_cmd(card, iob,
304                                  qeth_l2_send_setdelvlan_cb, NULL);
305 }
306
307 static void qeth_l2_process_vlans(struct qeth_card *card, int clear)
308 {
309         struct qeth_vlan_vid *id;
310         QETH_DBF_TEXT(TRACE, 3, "L2prcvln");
311         spin_lock_bh(&card->vlanlock);
312         list_for_each_entry(id, &card->vid_list, list) {
313                 if (clear)
314                         qeth_l2_send_setdelvlan(card, id->vid,
315                                 IPA_CMD_DELVLAN);
316                 else
317                         qeth_l2_send_setdelvlan(card, id->vid,
318                                 IPA_CMD_SETVLAN);
319         }
320         spin_unlock_bh(&card->vlanlock);
321 }
322
323 static void qeth_l2_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
324 {
325         struct qeth_card *card = netdev_priv(dev);
326         struct qeth_vlan_vid *id;
327
328         QETH_DBF_TEXT_(TRACE, 4, "aid:%d", vid);
329         id = kmalloc(sizeof(struct qeth_vlan_vid), GFP_ATOMIC);
330         if (id) {
331                 id->vid = vid;
332                 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
333                 spin_lock_bh(&card->vlanlock);
334                 list_add_tail(&id->list, &card->vid_list);
335                 spin_unlock_bh(&card->vlanlock);
336         } else {
337                 PRINT_ERR("no memory for vid\n");
338         }
339 }
340
341 static void qeth_l2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
342 {
343         struct qeth_vlan_vid *id, *tmpid = NULL;
344         struct qeth_card *card = netdev_priv(dev);
345
346         QETH_DBF_TEXT_(TRACE, 4, "kid:%d", vid);
347         spin_lock_bh(&card->vlanlock);
348         list_for_each_entry(id, &card->vid_list, list) {
349                 if (id->vid == vid) {
350                         list_del(&id->list);
351                         tmpid = id;
352                         break;
353                 }
354         }
355         spin_unlock_bh(&card->vlanlock);
356         if (tmpid) {
357                 qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
358                 kfree(tmpid);
359         }
360         qeth_l2_set_multicast_list(card->dev);
361 }
362
363 static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
364 {
365         int rc = 0;
366
367         QETH_DBF_TEXT(SETUP , 2, "stopcard");
368         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
369
370         qeth_set_allowed_threads(card, 0, 1);
371         if (qeth_wait_for_threads(card, ~QETH_RECOVER_THREAD))
372                 return -ERESTARTSYS;
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                 qeth_l2_del_all_mc(card);
394                 qeth_clear_ipacmd_list(card);
395                 card->state = CARD_STATE_HARDSETUP;
396         }
397         if (card->state == CARD_STATE_HARDSETUP) {
398                 qeth_qdio_clear_card(card, 0);
399                 qeth_clear_qdio_buffers(card);
400                 qeth_clear_working_pool_list(card);
401                 card->state = CARD_STATE_DOWN;
402         }
403         if (card->state == CARD_STATE_DOWN) {
404                 qeth_clear_cmd_buffers(&card->read);
405                 qeth_clear_cmd_buffers(&card->write);
406         }
407         card->use_hard_stop = 0;
408         return rc;
409 }
410
411 static void qeth_l2_process_inbound_buffer(struct qeth_card *card,
412                             struct qeth_qdio_buffer *buf, int index)
413 {
414         struct qdio_buffer_element *element;
415         struct sk_buff *skb;
416         struct qeth_hdr *hdr;
417         int offset;
418         unsigned int len;
419
420         /* get first element of current buffer */
421         element = (struct qdio_buffer_element *)&buf->buffer->element[0];
422         offset = 0;
423         if (card->options.performance_stats)
424                 card->perf_stats.bufs_rec++;
425         while ((skb = qeth_core_get_next_skb(card, buf->buffer, &element,
426                                        &offset, &hdr))) {
427                 skb->dev = card->dev;
428                 /* is device UP ? */
429                 if (!(card->dev->flags & IFF_UP)) {
430                         dev_kfree_skb_any(skb);
431                         continue;
432                 }
433
434                 switch (hdr->hdr.l2.id) {
435                 case QETH_HEADER_TYPE_LAYER2:
436                         skb->pkt_type = PACKET_HOST;
437                         skb->protocol = eth_type_trans(skb, skb->dev);
438                         if (card->options.checksum_type == NO_CHECKSUMMING)
439                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
440                         else
441                                 skb->ip_summed = CHECKSUM_NONE;
442                         if (skb->protocol == htons(ETH_P_802_2))
443                                 *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
444                         len = skb->len;
445                         netif_rx(skb);
446                         break;
447                 case QETH_HEADER_TYPE_OSN:
448                         skb_push(skb, sizeof(struct qeth_hdr));
449                         skb_copy_to_linear_data(skb, hdr,
450                                                 sizeof(struct qeth_hdr));
451                         len = skb->len;
452                         card->osn_info.data_cb(skb);
453                         break;
454                 default:
455                         dev_kfree_skb_any(skb);
456                         QETH_DBF_TEXT(TRACE, 3, "inbunkno");
457                         QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
458                         continue;
459                 }
460                 card->dev->last_rx = jiffies;
461                 card->stats.rx_packets++;
462                 card->stats.rx_bytes += len;
463         }
464 }
465
466 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
467                            enum qeth_ipa_cmds ipacmd,
468                            int (*reply_cb) (struct qeth_card *,
469                                             struct qeth_reply*,
470                                             unsigned long))
471 {
472         struct qeth_ipa_cmd *cmd;
473         struct qeth_cmd_buffer *iob;
474
475         QETH_DBF_TEXT(TRACE, 2, "L2sdmac");
476         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
477         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
478         cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
479         memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
480         return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
481 }
482
483 static int qeth_l2_send_setmac_cb(struct qeth_card *card,
484                            struct qeth_reply *reply,
485                            unsigned long data)
486 {
487         struct qeth_ipa_cmd *cmd;
488
489         QETH_DBF_TEXT(TRACE, 2, "L2Smaccb");
490         cmd = (struct qeth_ipa_cmd *) data;
491         if (cmd->hdr.return_code) {
492                 QETH_DBF_TEXT_(TRACE, 2, "L2er%x", cmd->hdr.return_code);
493                 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
494                 cmd->hdr.return_code = -EIO;
495         } else {
496                 card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
497                 memcpy(card->dev->dev_addr, cmd->data.setdelmac.mac,
498                        OSA_ADDR_LEN);
499                 PRINT_INFO("MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
500                            "successfully registered on device %s\n",
501                            card->dev->dev_addr[0], card->dev->dev_addr[1],
502                            card->dev->dev_addr[2], card->dev->dev_addr[3],
503                            card->dev->dev_addr[4], card->dev->dev_addr[5],
504                            card->dev->name);
505         }
506         return 0;
507 }
508
509 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
510 {
511         QETH_DBF_TEXT(TRACE, 2, "L2Setmac");
512         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
513                                           qeth_l2_send_setmac_cb);
514 }
515
516 static int qeth_l2_send_delmac_cb(struct qeth_card *card,
517                            struct qeth_reply *reply,
518                            unsigned long data)
519 {
520         struct qeth_ipa_cmd *cmd;
521
522         QETH_DBF_TEXT(TRACE, 2, "L2Dmaccb");
523         cmd = (struct qeth_ipa_cmd *) data;
524         if (cmd->hdr.return_code) {
525                 QETH_DBF_TEXT_(TRACE, 2, "err%d", cmd->hdr.return_code);
526                 cmd->hdr.return_code = -EIO;
527                 return 0;
528         }
529         card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
530
531         return 0;
532 }
533
534 static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
535 {
536         QETH_DBF_TEXT(TRACE, 2, "L2Delmac");
537         if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
538                 return 0;
539         return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
540                                           qeth_l2_send_delmac_cb);
541 }
542
543 static int qeth_l2_request_initial_mac(struct qeth_card *card)
544 {
545         int rc = 0;
546         char vendor_pre[] = {0x02, 0x00, 0x00};
547
548         QETH_DBF_TEXT(SETUP, 2, "doL2init");
549         QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
550
551         rc = qeth_query_setadapterparms(card);
552         if (rc) {
553                 PRINT_WARN("could not query adapter parameters on device %s: "
554                            "x%x\n", CARD_BUS_ID(card), rc);
555         }
556
557         if (card->info.guestlan) {
558                 rc = qeth_setadpparms_change_macaddr(card);
559                 if (rc) {
560                         PRINT_WARN("couldn't get MAC address on "
561                            "device %s: x%x\n",
562                            CARD_BUS_ID(card), rc);
563                         QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
564                         return rc;
565                 }
566                 QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
567         } else {
568                 random_ether_addr(card->dev->dev_addr);
569                 memcpy(card->dev->dev_addr, vendor_pre, 3);
570         }
571         return 0;
572 }
573
574 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
575 {
576         struct sockaddr *addr = p;
577         struct qeth_card *card = netdev_priv(dev);
578         int rc = 0;
579
580         QETH_DBF_TEXT(TRACE, 3, "setmac");
581
582         if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
583                 QETH_DBF_TEXT(TRACE, 3, "setmcINV");
584                 return -EOPNOTSUPP;
585         }
586
587         if (card->info.type == QETH_CARD_TYPE_OSN) {
588                 PRINT_WARN("Setting MAC address on %s is not supported.\n",
589                            dev->name);
590                 QETH_DBF_TEXT(TRACE, 3, "setmcOSN");
591                 return -EOPNOTSUPP;
592         }
593         QETH_DBF_TEXT_(TRACE, 3, "%s", CARD_BUS_ID(card));
594         QETH_DBF_HEX(TRACE, 3, addr->sa_data, OSA_ADDR_LEN);
595         rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
596         if (!rc)
597                 rc = qeth_l2_send_setmac(card, addr->sa_data);
598         return rc;
599 }
600
601 static void qeth_l2_set_multicast_list(struct net_device *dev)
602 {
603         struct qeth_card *card = netdev_priv(dev);
604         struct dev_mc_list *dm;
605
606         if (card->info.type == QETH_CARD_TYPE_OSN)
607                 return ;
608
609         QETH_DBF_TEXT(TRACE, 3, "setmulti");
610         qeth_l2_del_all_mc(card);
611         spin_lock_bh(&card->mclock);
612         for (dm = dev->mc_list; dm; dm = dm->next)
613                 qeth_l2_add_mc(card, dm->dmi_addr);
614         spin_unlock_bh(&card->mclock);
615         if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
616                 return;
617         qeth_setadp_promisc_mode(card);
618 }
619
620 static int qeth_l2_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
621 {
622         int rc;
623         struct qeth_hdr *hdr = NULL;
624         int elements = 0;
625         struct qeth_card *card = netdev_priv(dev);
626         struct sk_buff *new_skb = skb;
627         int ipv = qeth_get_ip_version(skb);
628         int cast_type = qeth_get_cast_type(card, skb);
629         struct qeth_qdio_out_q *queue = card->qdio.out_qs
630                 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
631         int tx_bytes = skb->len;
632         enum qeth_large_send_types large_send = QETH_LARGE_SEND_NO;
633         struct qeth_eddp_context *ctx = NULL;
634
635         if ((card->state != CARD_STATE_UP) || !card->lan_online) {
636                 card->stats.tx_carrier_errors++;
637                 goto tx_drop;
638         }
639
640         if ((card->info.type == QETH_CARD_TYPE_OSN) &&
641             (skb->protocol == htons(ETH_P_IPV6)))
642                 goto tx_drop;
643
644         if (card->options.performance_stats) {
645                 card->perf_stats.outbound_cnt++;
646                 card->perf_stats.outbound_start_time = qeth_get_micros();
647         }
648         netif_stop_queue(dev);
649
650         if (skb_is_gso(skb))
651                 large_send = QETH_LARGE_SEND_EDDP;
652
653         if (card->info.type == QETH_CARD_TYPE_OSN)
654                 hdr = (struct qeth_hdr *)skb->data;
655         else {
656                 /* create a clone with writeable headroom */
657                 new_skb = skb_realloc_headroom(skb, sizeof(struct qeth_hdr));
658                 if (!new_skb)
659                         goto tx_drop;
660                 hdr = (struct qeth_hdr *)skb_push(new_skb,
661                                                 sizeof(struct qeth_hdr));
662                 qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
663         }
664
665         if (large_send == QETH_LARGE_SEND_EDDP) {
666                 ctx = qeth_eddp_create_context(card, new_skb, hdr,
667                                                 skb->sk->sk_protocol);
668                 if (ctx == NULL) {
669                         PRINT_WARN("could not create eddp context\n");
670                         goto tx_drop;
671                 }
672         } else {
673                 elements = qeth_get_elements_no(card, (void *)hdr, new_skb, 0);
674                 if (!elements)
675                         goto tx_drop;
676         }
677
678         if ((large_send == QETH_LARGE_SEND_NO) &&
679             (skb->ip_summed == CHECKSUM_PARTIAL))
680                 qeth_tx_csum(new_skb);
681
682         if (card->info.type != QETH_CARD_TYPE_IQD)
683                 rc = qeth_do_send_packet(card, queue, new_skb, hdr,
684                                          elements, ctx);
685         else
686                 rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
687                                               elements, ctx);
688         if (!rc) {
689                 card->stats.tx_packets++;
690                 card->stats.tx_bytes += tx_bytes;
691                 if (new_skb != skb)
692                         dev_kfree_skb_any(skb);
693                 if (card->options.performance_stats) {
694                         if (large_send != QETH_LARGE_SEND_NO) {
695                                 card->perf_stats.large_send_bytes += tx_bytes;
696                                 card->perf_stats.large_send_cnt++;
697                         }
698                         if (skb_shinfo(new_skb)->nr_frags > 0) {
699                                 card->perf_stats.sg_skbs_sent++;
700                                 /* nr_frags + skb->data */
701                                 card->perf_stats.sg_frags_sent +=
702                                         skb_shinfo(new_skb)->nr_frags + 1;
703                         }
704                 }
705
706                 if (ctx != NULL) {
707                         qeth_eddp_put_context(ctx);
708                         dev_kfree_skb_any(new_skb);
709                 }
710         } else {
711                 if (ctx != NULL)
712                         qeth_eddp_put_context(ctx);
713
714                 if (rc == -EBUSY) {
715                         if (new_skb != skb)
716                                 dev_kfree_skb_any(new_skb);
717                         return NETDEV_TX_BUSY;
718                 } else
719                         goto tx_drop;
720         }
721
722         netif_wake_queue(dev);
723         if (card->options.performance_stats)
724                 card->perf_stats.outbound_time += qeth_get_micros() -
725                         card->perf_stats.outbound_start_time;
726         return rc;
727
728 tx_drop:
729         card->stats.tx_dropped++;
730         card->stats.tx_errors++;
731         if ((new_skb != skb) && new_skb)
732                 dev_kfree_skb_any(new_skb);
733         dev_kfree_skb_any(skb);
734         return NETDEV_TX_OK;
735 }
736
737 static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev,
738                         unsigned int status, unsigned int qdio_err,
739                         unsigned int siga_err, unsigned int queue,
740                         int first_element, int count, unsigned long card_ptr)
741 {
742         struct net_device *net_dev;
743         struct qeth_card *card;
744         struct qeth_qdio_buffer *buffer;
745         int index;
746         int i;
747
748         card = (struct qeth_card *) card_ptr;
749         net_dev = card->dev;
750         if (card->options.performance_stats) {
751                 card->perf_stats.inbound_cnt++;
752                 card->perf_stats.inbound_start_time = qeth_get_micros();
753         }
754         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
755                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION) {
756                         QETH_DBF_TEXT(TRACE, 1, "qdinchk");
757                         QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
758                         QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", first_element,
759                                         count);
760                         QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", queue, status);
761                         qeth_schedule_recovery(card);
762                         return;
763                 }
764         }
765         for (i = first_element; i < (first_element + count); ++i) {
766                 index = i % QDIO_MAX_BUFFERS_PER_Q;
767                 buffer = &card->qdio.in_q->bufs[index];
768                 if (!((status & QDIO_STATUS_LOOK_FOR_ERROR) &&
769                       qeth_check_qdio_errors(buffer->buffer,
770                                              qdio_err, siga_err, "qinerr")))
771                         qeth_l2_process_inbound_buffer(card, buffer, index);
772                 /* clear buffer and give back to hardware */
773                 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
774                 qeth_queue_input_buffer(card, index);
775         }
776         if (card->options.performance_stats)
777                 card->perf_stats.inbound_time += qeth_get_micros() -
778                         card->perf_stats.inbound_start_time;
779 }
780
781 static int qeth_l2_open(struct net_device *dev)
782 {
783         struct qeth_card *card = netdev_priv(dev);
784
785         QETH_DBF_TEXT(TRACE, 4, "qethopen");
786         if (card->state != CARD_STATE_SOFTSETUP)
787                 return -ENODEV;
788
789         if ((card->info.type != QETH_CARD_TYPE_OSN) &&
790              (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
791                 QETH_DBF_TEXT(TRACE, 4, "nomacadr");
792                 return -EPERM;
793         }
794         card->data.state = CH_STATE_UP;
795         card->state = CARD_STATE_UP;
796         card->dev->flags |= IFF_UP;
797         netif_start_queue(dev);
798
799         if (!card->lan_online && netif_carrier_ok(dev))
800                 netif_carrier_off(dev);
801         return 0;
802 }
803
804
805 static int qeth_l2_stop(struct net_device *dev)
806 {
807         struct qeth_card *card = netdev_priv(dev);
808
809         QETH_DBF_TEXT(TRACE, 4, "qethstop");
810         netif_tx_disable(dev);
811         card->dev->flags &= ~IFF_UP;
812         if (card->state == CARD_STATE_UP)
813                 card->state = CARD_STATE_SOFTSETUP;
814         return 0;
815 }
816
817 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
818 {
819         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
820
821         INIT_LIST_HEAD(&card->vid_list);
822         INIT_LIST_HEAD(&card->mc_list);
823         card->options.layer2 = 1;
824         card->discipline.input_handler = (qdio_handler_t *)
825                 qeth_l2_qdio_input_handler;
826         card->discipline.output_handler = (qdio_handler_t *)
827                 qeth_qdio_output_handler;
828         card->discipline.recover = qeth_l2_recover;
829         return 0;
830 }
831
832 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
833 {
834         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
835
836         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
837
838         if (cgdev->state == CCWGROUP_ONLINE) {
839                 card->use_hard_stop = 1;
840                 qeth_l2_set_offline(cgdev);
841         }
842
843         if (card->dev) {
844                 unregister_netdev(card->dev);
845                 card->dev = NULL;
846         }
847
848         qeth_l2_del_all_mc(card);
849         return;
850 }
851
852 static int qeth_l2_ethtool_set_tso(struct net_device *dev, u32 data)
853 {
854         struct qeth_card *card = netdev_priv(dev);
855
856         if (data) {
857                 if (card->options.large_send == QETH_LARGE_SEND_NO) {
858                         card->options.large_send = QETH_LARGE_SEND_EDDP;
859                         dev->features |= NETIF_F_TSO;
860                 }
861         } else {
862                 dev->features &= ~NETIF_F_TSO;
863                 card->options.large_send = QETH_LARGE_SEND_NO;
864         }
865         return 0;
866 }
867
868 static struct ethtool_ops qeth_l2_ethtool_ops = {
869         .get_link = ethtool_op_get_link,
870         .get_tx_csum = ethtool_op_get_tx_csum,
871         .set_tx_csum = ethtool_op_set_tx_hw_csum,
872         .get_sg = ethtool_op_get_sg,
873         .set_sg = ethtool_op_set_sg,
874         .get_tso = ethtool_op_get_tso,
875         .set_tso = qeth_l2_ethtool_set_tso,
876         .get_strings = qeth_core_get_strings,
877         .get_ethtool_stats = qeth_core_get_ethtool_stats,
878         .get_stats_count = qeth_core_get_stats_count,
879         .get_drvinfo = qeth_core_get_drvinfo,
880         .get_settings = qeth_core_ethtool_get_settings,
881 };
882
883 static struct ethtool_ops qeth_l2_osn_ops = {
884         .get_strings = qeth_core_get_strings,
885         .get_ethtool_stats = qeth_core_get_ethtool_stats,
886         .get_stats_count = qeth_core_get_stats_count,
887         .get_drvinfo = qeth_core_get_drvinfo,
888 };
889
890 static int qeth_l2_setup_netdev(struct qeth_card *card)
891 {
892         switch (card->info.type) {
893         case QETH_CARD_TYPE_OSAE:
894                 card->dev = alloc_etherdev(0);
895                 break;
896         case QETH_CARD_TYPE_IQD:
897                 card->dev = alloc_netdev(0, "hsi%d", ether_setup);
898                 break;
899         case QETH_CARD_TYPE_OSN:
900                 card->dev = alloc_netdev(0, "osn%d", ether_setup);
901                 card->dev->flags |= IFF_NOARP;
902                 break;
903         default:
904                 card->dev = alloc_etherdev(0);
905         }
906
907         if (!card->dev)
908                 return -ENODEV;
909
910         card->dev->priv = card;
911         card->dev->tx_timeout = &qeth_tx_timeout;
912         card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
913         card->dev->open = qeth_l2_open;
914         card->dev->stop = qeth_l2_stop;
915         card->dev->hard_start_xmit = qeth_l2_hard_start_xmit;
916         card->dev->do_ioctl = qeth_l2_do_ioctl;
917         card->dev->get_stats = qeth_get_stats;
918         card->dev->change_mtu = qeth_change_mtu;
919         card->dev->set_multicast_list = qeth_l2_set_multicast_list;
920         card->dev->vlan_rx_kill_vid = qeth_l2_vlan_rx_kill_vid;
921         card->dev->vlan_rx_add_vid = qeth_l2_vlan_rx_add_vid;
922         card->dev->set_mac_address = qeth_l2_set_mac_address;
923         card->dev->mtu = card->info.initial_mtu;
924         if (card->info.type != QETH_CARD_TYPE_OSN)
925                 SET_ETHTOOL_OPS(card->dev, &qeth_l2_ethtool_ops);
926         else
927                 SET_ETHTOOL_OPS(card->dev, &qeth_l2_osn_ops);
928         card->dev->features |= NETIF_F_HW_VLAN_FILTER;
929         card->info.broadcast_capable = 1;
930         qeth_l2_request_initial_mac(card);
931         SET_NETDEV_DEV(card->dev, &card->gdev->dev);
932         return register_netdev(card->dev);
933 }
934
935 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
936 {
937         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
938         int rc = 0;
939         enum qeth_card_states recover_flag;
940
941         BUG_ON(!card);
942         QETH_DBF_TEXT(SETUP, 2, "setonlin");
943         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
944
945         qeth_set_allowed_threads(card, QETH_RECOVER_THREAD, 1);
946         if (qeth_wait_for_threads(card, ~QETH_RECOVER_THREAD)) {
947                 PRINT_WARN("set_online of card %s interrupted by user!\n",
948                            CARD_BUS_ID(card));
949                 return -ERESTARTSYS;
950         }
951
952         recover_flag = card->state;
953         rc = ccw_device_set_online(CARD_RDEV(card));
954         if (rc) {
955                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
956                 return -EIO;
957         }
958         rc = ccw_device_set_online(CARD_WDEV(card));
959         if (rc) {
960                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
961                 return -EIO;
962         }
963         rc = ccw_device_set_online(CARD_DDEV(card));
964         if (rc) {
965                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
966                 return -EIO;
967         }
968
969         rc = qeth_core_hardsetup_card(card);
970         if (rc) {
971                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
972                 goto out_remove;
973         }
974
975         if (!card->dev && qeth_l2_setup_netdev(card))
976                 goto out_remove;
977
978         if (card->info.type != QETH_CARD_TYPE_OSN)
979                 qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
980
981         card->state = CARD_STATE_HARDSETUP;
982         qeth_print_status_message(card);
983
984         /* softsetup */
985         QETH_DBF_TEXT(SETUP, 2, "softsetp");
986
987         rc = qeth_send_startlan(card);
988         if (rc) {
989                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
990                 if (rc == 0xe080) {
991                         PRINT_WARN("LAN on card %s if offline! "
992                                    "Waiting for STARTLAN from card.\n",
993                                    CARD_BUS_ID(card));
994                         card->lan_online = 0;
995                 }
996                 return rc;
997         } else
998                 card->lan_online = 1;
999
1000         if (card->info.type != QETH_CARD_TYPE_OSN) {
1001                 qeth_set_large_send(card, card->options.large_send);
1002                 qeth_l2_process_vlans(card, 0);
1003         }
1004
1005         netif_tx_disable(card->dev);
1006
1007         rc = qeth_init_qdio_queues(card);
1008         if (rc) {
1009                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1010                 goto out_remove;
1011         }
1012         card->state = CARD_STATE_SOFTSETUP;
1013         netif_carrier_on(card->dev);
1014
1015         qeth_set_allowed_threads(card, 0xffffffff, 0);
1016         if (recover_flag == CARD_STATE_RECOVER) {
1017                 if (recovery_mode &&
1018                     card->info.type != QETH_CARD_TYPE_OSN) {
1019                         qeth_l2_open(card->dev);
1020                 } else {
1021                         rtnl_lock();
1022                         dev_open(card->dev);
1023                         rtnl_unlock();
1024                 }
1025                 /* this also sets saved unicast addresses */
1026                 qeth_l2_set_multicast_list(card->dev);
1027         }
1028         /* let user_space know that device is online */
1029         kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1030         return 0;
1031 out_remove:
1032         card->use_hard_stop = 1;
1033         qeth_l2_stop_card(card, 0);
1034         ccw_device_set_offline(CARD_DDEV(card));
1035         ccw_device_set_offline(CARD_WDEV(card));
1036         ccw_device_set_offline(CARD_RDEV(card));
1037         if (recover_flag == CARD_STATE_RECOVER)
1038                 card->state = CARD_STATE_RECOVER;
1039         else
1040                 card->state = CARD_STATE_DOWN;
1041         return -ENODEV;
1042 }
1043
1044 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1045 {
1046         return __qeth_l2_set_online(gdev, 0);
1047 }
1048
1049 static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1050                                         int recovery_mode)
1051 {
1052         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1053         int rc = 0, rc2 = 0, rc3 = 0;
1054         enum qeth_card_states recover_flag;
1055
1056         QETH_DBF_TEXT(SETUP, 3, "setoffl");
1057         QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1058
1059         if (card->dev && netif_carrier_ok(card->dev))
1060                 netif_carrier_off(card->dev);
1061         recover_flag = card->state;
1062         if (qeth_l2_stop_card(card, recovery_mode) == -ERESTARTSYS) {
1063                 PRINT_WARN("Stopping card %s interrupted by user!\n",
1064                            CARD_BUS_ID(card));
1065                 return -ERESTARTSYS;
1066         }
1067         rc  = ccw_device_set_offline(CARD_DDEV(card));
1068         rc2 = ccw_device_set_offline(CARD_WDEV(card));
1069         rc3 = ccw_device_set_offline(CARD_RDEV(card));
1070         if (!rc)
1071                 rc = (rc2) ? rc2 : rc3;
1072         if (rc)
1073                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1074         if (recover_flag == CARD_STATE_UP)
1075                 card->state = CARD_STATE_RECOVER;
1076         /* let user_space know that device is offline */
1077         kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1078         return 0;
1079 }
1080
1081 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1082 {
1083         return __qeth_l2_set_offline(cgdev, 0);
1084 }
1085
1086 static int qeth_l2_recover(void *ptr)
1087 {
1088         struct qeth_card *card;
1089         int rc = 0;
1090
1091         card = (struct qeth_card *) ptr;
1092         QETH_DBF_TEXT(TRACE, 2, "recover1");
1093         QETH_DBF_HEX(TRACE, 2, &card, sizeof(void *));
1094         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1095                 return 0;
1096         QETH_DBF_TEXT(TRACE, 2, "recover2");
1097         PRINT_WARN("Recovery of device %s started ...\n",
1098                    CARD_BUS_ID(card));
1099         card->use_hard_stop = 1;
1100         __qeth_l2_set_offline(card->gdev, 1);
1101         rc = __qeth_l2_set_online(card->gdev, 1);
1102         /* don't run another scheduled recovery */
1103         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1104         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1105         if (!rc)
1106                 PRINT_INFO("Device %s successfully recovered!\n",
1107                            CARD_BUS_ID(card));
1108         else
1109                 PRINT_INFO("Device %s could not be recovered!\n",
1110                            CARD_BUS_ID(card));
1111         return 0;
1112 }
1113
1114 static int __init qeth_l2_init(void)
1115 {
1116         PRINT_INFO("register layer 2 discipline\n");
1117         return 0;
1118 }
1119
1120 static void __exit qeth_l2_exit(void)
1121 {
1122         PRINT_INFO("unregister layer 2 discipline\n");
1123 }
1124
1125 static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1126 {
1127         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1128         qeth_qdio_clear_card(card, 0);
1129         qeth_clear_qdio_buffers(card);
1130 }
1131
1132 struct ccwgroup_driver qeth_l2_ccwgroup_driver = {
1133         .probe = qeth_l2_probe_device,
1134         .remove = qeth_l2_remove_device,
1135         .set_online = qeth_l2_set_online,
1136         .set_offline = qeth_l2_set_offline,
1137         .shutdown = qeth_l2_shutdown,
1138 };
1139 EXPORT_SYMBOL_GPL(qeth_l2_ccwgroup_driver);
1140
1141 static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1142                            struct qeth_cmd_buffer *iob)
1143 {
1144         unsigned long flags;
1145         int rc = 0;
1146
1147         QETH_DBF_TEXT(TRACE, 5, "osndctrd");
1148
1149         wait_event(card->wait_q,
1150                    atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1151         qeth_prepare_control_data(card, len, iob);
1152         QETH_DBF_TEXT(TRACE, 6, "osnoirqp");
1153         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1154         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1155                               (addr_t) iob, 0, 0);
1156         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1157         if (rc) {
1158                 PRINT_WARN("qeth_osn_send_control_data: "
1159                            "ccw_device_start rc = %i\n", rc);
1160                 QETH_DBF_TEXT_(TRACE, 2, " err%d", rc);
1161                 qeth_release_buffer(iob->channel, iob);
1162                 atomic_set(&card->write.irq_pending, 0);
1163                 wake_up(&card->wait_q);
1164         }
1165         return rc;
1166 }
1167
1168 static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1169                         struct qeth_cmd_buffer *iob, int data_len)
1170 {
1171         u16 s1, s2;
1172
1173         QETH_DBF_TEXT(TRACE, 4, "osndipa");
1174
1175         qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1176         s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1177         s2 = (u16)data_len;
1178         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1179         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1180         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1181         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1182         return qeth_osn_send_control_data(card, s1, iob);
1183 }
1184
1185 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1186 {
1187         struct qeth_cmd_buffer *iob;
1188         struct qeth_card *card;
1189         int rc;
1190
1191         QETH_DBF_TEXT(TRACE, 2, "osnsdmc");
1192         if (!dev)
1193                 return -ENODEV;
1194         card = netdev_priv(dev);
1195         if (!card)
1196                 return -ENODEV;
1197         if ((card->state != CARD_STATE_UP) &&
1198             (card->state != CARD_STATE_SOFTSETUP))
1199                 return -ENODEV;
1200         iob = qeth_wait_for_buffer(&card->write);
1201         memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1202         rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1203         return rc;
1204 }
1205 EXPORT_SYMBOL(qeth_osn_assist);
1206
1207 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1208                   int (*assist_cb)(struct net_device *, void *),
1209                   int (*data_cb)(struct sk_buff *))
1210 {
1211         struct qeth_card *card;
1212
1213         QETH_DBF_TEXT(TRACE, 2, "osnreg");
1214         *dev = qeth_l2_netdev_by_devno(read_dev_no);
1215         if (*dev == NULL)
1216                 return -ENODEV;
1217         card = netdev_priv(*dev);
1218         if (!card)
1219                 return -ENODEV;
1220         if ((assist_cb == NULL) || (data_cb == NULL))
1221                 return -EINVAL;
1222         card->osn_info.assist_cb = assist_cb;
1223         card->osn_info.data_cb = data_cb;
1224         return 0;
1225 }
1226 EXPORT_SYMBOL(qeth_osn_register);
1227
1228 void qeth_osn_deregister(struct net_device *dev)
1229 {
1230         struct qeth_card *card;
1231
1232         QETH_DBF_TEXT(TRACE, 2, "osndereg");
1233         if (!dev)
1234                 return;
1235         card = netdev_priv(dev);
1236         if (!card)
1237                 return;
1238         card->osn_info.assist_cb = NULL;
1239         card->osn_info.data_cb = NULL;
1240         return;
1241 }
1242 EXPORT_SYMBOL(qeth_osn_deregister);
1243
1244 module_init(qeth_l2_init);
1245 module_exit(qeth_l2_exit);
1246 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1247 MODULE_DESCRIPTION("qeth layer 2 discipline");
1248 MODULE_LICENSE("GPL");