claw: invalid return codes from hard_start_xmit
[linux-2.6] / drivers / s390 / net / claw.c
1 /*
2  *  drivers/s390/net/claw.c
3  *    ESCON CLAW network driver
4  *
5  *  Linux for zSeries version
6  *    Copyright (C) 2002,2005 IBM Corporation
7  *  Author(s) Original code written by:
8  *              Kazuo Iimura (iimura@jp.ibm.com)
9  *            Rewritten by
10  *              Andy Richter (richtera@us.ibm.com)
11  *              Marc Price (mwprice@us.ibm.com)
12  *
13  *    sysfs parms:
14  *   group x.x.rrrr,x.x.wwww
15  *   read_buffer nnnnnnn
16  *   write_buffer nnnnnn
17  *   host_name  aaaaaaaa
18  *   adapter_name aaaaaaaa
19  *   api_type    aaaaaaaa
20  *
21  *  eg.
22  *   group  0.0.0200 0.0.0201
23  *   read_buffer 25
24  *   write_buffer 20
25  *   host_name LINUX390
26  *   adapter_name RS6K
27  *   api_type     TCPIP
28  *
29  *  where
30  *
31  *   The device id is decided by the order entries
32  *   are added to the group the first is claw0 the second claw1
33  *   up to CLAW_MAX_DEV
34  *
35  *   rrrr     - the first of 2 consecutive device addresses used for the
36  *              CLAW protocol.
37  *              The specified address is always used as the input (Read)
38  *              channel and the next address is used as the output channel.
39  *
40  *   wwww     - the second of 2 consecutive device addresses used for
41  *              the CLAW protocol.
42  *              The specified address is always used as the output
43  *              channel and the previous address is used as the input channel.
44  *
45  *   read_buffer        -       specifies number of input buffers to allocate.
46  *   write_buffer       -       specifies number of output buffers to allocate.
47  *   host_name          -       host name
48  *   adaptor_name       -       adaptor name
49  *   api_type           -       API type TCPIP or API will be sent and expected
50  *                              as ws_name
51  *
52  *   Note the following requirements:
53  *   1)  host_name must match the configured adapter_name on the remote side
54  *   2)  adaptor_name must match the configured host name on the remote side
55  *
56  *  Change History
57  *    1.00  Initial release shipped
58  *    1.10  Changes for Buffer allocation
59  *    1.15  Changed for 2.6 Kernel  No longer compiles on 2.4 or lower
60  *    1.25  Added Packing support
61  *    1.5
62  */
63
64 #define KMSG_COMPONENT "claw"
65
66 #include <asm/ccwdev.h>
67 #include <asm/ccwgroup.h>
68 #include <asm/debug.h>
69 #include <asm/idals.h>
70 #include <asm/io.h>
71 #include <linux/bitops.h>
72 #include <linux/ctype.h>
73 #include <linux/delay.h>
74 #include <linux/errno.h>
75 #include <linux/if_arp.h>
76 #include <linux/init.h>
77 #include <linux/interrupt.h>
78 #include <linux/ip.h>
79 #include <linux/kernel.h>
80 #include <linux/module.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/proc_fs.h>
84 #include <linux/sched.h>
85 #include <linux/signal.h>
86 #include <linux/skbuff.h>
87 #include <linux/slab.h>
88 #include <linux/string.h>
89 #include <linux/tcp.h>
90 #include <linux/timer.h>
91 #include <linux/types.h>
92
93 #include "cu3088.h"
94 #include "claw.h"
95
96 /*
97    CLAW uses the s390dbf file system  see claw_trace and claw_setup
98 */
99
100 static char version[] __initdata = "CLAW driver";
101 static char debug_buffer[255];
102 /**
103  * Debug Facility Stuff
104  */
105 static debug_info_t *claw_dbf_setup;
106 static debug_info_t *claw_dbf_trace;
107
108 /**
109  *  CLAW Debug Facility functions
110  */
111 static void
112 claw_unregister_debug_facility(void)
113 {
114         if (claw_dbf_setup)
115                 debug_unregister(claw_dbf_setup);
116         if (claw_dbf_trace)
117                 debug_unregister(claw_dbf_trace);
118 }
119
120 static int
121 claw_register_debug_facility(void)
122 {
123         claw_dbf_setup = debug_register("claw_setup", 2, 1, 8);
124         claw_dbf_trace = debug_register("claw_trace", 2, 2, 8);
125         if (claw_dbf_setup == NULL || claw_dbf_trace == NULL) {
126                 claw_unregister_debug_facility();
127                 return -ENOMEM;
128         }
129         debug_register_view(claw_dbf_setup, &debug_hex_ascii_view);
130         debug_set_level(claw_dbf_setup, 2);
131         debug_register_view(claw_dbf_trace, &debug_hex_ascii_view);
132         debug_set_level(claw_dbf_trace, 2);
133         return 0;
134 }
135
136 static inline void
137 claw_set_busy(struct net_device *dev)
138 {
139  ((struct claw_privbk *)dev->ml_priv)->tbusy = 1;
140  eieio();
141 }
142
143 static inline void
144 claw_clear_busy(struct net_device *dev)
145 {
146         clear_bit(0, &(((struct claw_privbk *) dev->ml_priv)->tbusy));
147         netif_wake_queue(dev);
148         eieio();
149 }
150
151 static inline int
152 claw_check_busy(struct net_device *dev)
153 {
154         eieio();
155         return ((struct claw_privbk *) dev->ml_priv)->tbusy;
156 }
157
158 static inline void
159 claw_setbit_busy(int nr,struct net_device *dev)
160 {
161         netif_stop_queue(dev);
162         set_bit(nr, (void *)&(((struct claw_privbk *)dev->ml_priv)->tbusy));
163 }
164
165 static inline void
166 claw_clearbit_busy(int nr,struct net_device *dev)
167 {
168         clear_bit(nr, (void *)&(((struct claw_privbk *)dev->ml_priv)->tbusy));
169         netif_wake_queue(dev);
170 }
171
172 static inline int
173 claw_test_and_setbit_busy(int nr,struct net_device *dev)
174 {
175         netif_stop_queue(dev);
176         return test_and_set_bit(nr,
177                 (void *)&(((struct claw_privbk *) dev->ml_priv)->tbusy));
178 }
179
180
181 /* Functions for the DEV methods */
182
183 static int claw_probe(struct ccwgroup_device *cgdev);
184 static void claw_remove_device(struct ccwgroup_device *cgdev);
185 static void claw_purge_skb_queue(struct sk_buff_head *q);
186 static int claw_new_device(struct ccwgroup_device *cgdev);
187 static int claw_shutdown_device(struct ccwgroup_device *cgdev);
188 static int claw_tx(struct sk_buff *skb, struct net_device *dev);
189 static int claw_change_mtu( struct net_device *dev, int new_mtu);
190 static int claw_open(struct net_device *dev);
191 static void claw_irq_handler(struct ccw_device *cdev,
192         unsigned long intparm, struct irb *irb);
193 static void claw_irq_tasklet ( unsigned long data );
194 static int claw_release(struct net_device *dev);
195 static void claw_write_retry ( struct chbk * p_ch );
196 static void claw_write_next ( struct chbk * p_ch );
197 static void claw_timer ( struct chbk * p_ch );
198
199 /* Functions */
200 static int add_claw_reads(struct net_device *dev,
201         struct ccwbk* p_first, struct ccwbk* p_last);
202 static void ccw_check_return_code (struct ccw_device *cdev, int return_code);
203 static void ccw_check_unit_check (struct chbk * p_ch, unsigned char sense );
204 static int find_link(struct net_device *dev, char *host_name, char *ws_name );
205 static int claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid);
206 static int init_ccw_bk(struct net_device *dev);
207 static void probe_error( struct ccwgroup_device *cgdev);
208 static struct net_device_stats *claw_stats(struct net_device *dev);
209 static int pages_to_order_of_mag(int num_of_pages);
210 static struct sk_buff *claw_pack_skb(struct claw_privbk *privptr);
211 /* sysfs Functions */
212 static ssize_t claw_hname_show(struct device *dev,
213         struct device_attribute *attr, char *buf);
214 static ssize_t claw_hname_write(struct device *dev,
215         struct device_attribute *attr,
216         const char *buf, size_t count);
217 static ssize_t claw_adname_show(struct device *dev,
218         struct device_attribute *attr, char *buf);
219 static ssize_t claw_adname_write(struct device *dev,
220         struct device_attribute *attr,
221         const char *buf, size_t count);
222 static ssize_t claw_apname_show(struct device *dev,
223         struct device_attribute *attr, char *buf);
224 static ssize_t claw_apname_write(struct device *dev,
225         struct device_attribute *attr,
226         const char *buf, size_t count);
227 static ssize_t claw_wbuff_show(struct device *dev,
228         struct device_attribute *attr, char *buf);
229 static ssize_t claw_wbuff_write(struct device *dev,
230         struct device_attribute *attr,
231         const char *buf, size_t count);
232 static ssize_t claw_rbuff_show(struct device *dev,
233         struct device_attribute *attr, char *buf);
234 static ssize_t claw_rbuff_write(struct device *dev,
235         struct device_attribute *attr,
236         const char *buf, size_t count);
237 static int claw_add_files(struct device *dev);
238 static void claw_remove_files(struct device *dev);
239
240 /*   Functions for System Validate  */
241 static int claw_process_control( struct net_device *dev, struct ccwbk * p_ccw);
242 static int claw_send_control(struct net_device *dev, __u8 type, __u8 link,
243        __u8 correlator, __u8 rc , char *local_name, char *remote_name);
244 static int claw_snd_conn_req(struct net_device *dev, __u8 link);
245 static int claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl);
246 static int claw_snd_sys_validate_rsp(struct net_device *dev,
247         struct clawctl * p_ctl, __u32 return_code);
248 static int claw_strt_conn_req(struct net_device *dev );
249 static void claw_strt_read(struct net_device *dev, int lock);
250 static void claw_strt_out_IO(struct net_device *dev);
251 static void claw_free_wrt_buf(struct net_device *dev);
252
253 /* Functions for unpack reads   */
254 static void unpack_read(struct net_device *dev);
255
256 /* ccwgroup table  */
257
258 static struct ccwgroup_driver claw_group_driver = {
259         .owner       = THIS_MODULE,
260         .name        = "claw",
261         .max_slaves  = 2,
262         .driver_id   = 0xC3D3C1E6,
263         .probe       = claw_probe,
264         .remove      = claw_remove_device,
265         .set_online  = claw_new_device,
266         .set_offline = claw_shutdown_device,
267 };
268
269 /*
270 *       Key functions
271 */
272
273 /*----------------------------------------------------------------*
274  *   claw_probe                                                   *
275  *      this function is called for each CLAW device.             *
276  *----------------------------------------------------------------*/
277 static int
278 claw_probe(struct ccwgroup_device *cgdev)
279 {
280         int             rc;
281         struct claw_privbk *privptr=NULL;
282
283         CLAW_DBF_TEXT(2, setup, "probe");
284         if (!get_device(&cgdev->dev))
285                 return -ENODEV;
286         privptr = kzalloc(sizeof(struct claw_privbk), GFP_KERNEL);
287         cgdev->dev.driver_data = privptr;
288         if (privptr == NULL) {
289                 probe_error(cgdev);
290                 put_device(&cgdev->dev);
291                 CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
292                 return -ENOMEM;
293         }
294         privptr->p_mtc_envelope= kzalloc( MAX_ENVELOPE_SIZE, GFP_KERNEL);
295         privptr->p_env = kzalloc(sizeof(struct claw_env), GFP_KERNEL);
296         if ((privptr->p_mtc_envelope==NULL) || (privptr->p_env==NULL)) {
297                 probe_error(cgdev);
298                 put_device(&cgdev->dev);
299                 CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
300                 return -ENOMEM;
301         }
302         memcpy(privptr->p_env->adapter_name,WS_NAME_NOT_DEF,8);
303         memcpy(privptr->p_env->host_name,WS_NAME_NOT_DEF,8);
304         memcpy(privptr->p_env->api_type,WS_NAME_NOT_DEF,8);
305         privptr->p_env->packing = 0;
306         privptr->p_env->write_buffers = 5;
307         privptr->p_env->read_buffers = 5;
308         privptr->p_env->read_size = CLAW_FRAME_SIZE;
309         privptr->p_env->write_size = CLAW_FRAME_SIZE;
310         rc = claw_add_files(&cgdev->dev);
311         if (rc) {
312                 probe_error(cgdev);
313                 put_device(&cgdev->dev);
314                 dev_err(&cgdev->dev, "Creating the /proc files for a new"
315                 " CLAW device failed\n");
316                 CLAW_DBF_TEXT_(2, setup, "probex%d", rc);
317                 return rc;
318         }
319         privptr->p_env->p_priv = privptr;
320         cgdev->cdev[0]->handler = claw_irq_handler;
321         cgdev->cdev[1]->handler = claw_irq_handler;
322         CLAW_DBF_TEXT(2, setup, "prbext 0");
323
324         return 0;
325 }  /*  end of claw_probe       */
326
327 /*-------------------------------------------------------------------*
328  *   claw_tx                                                         *
329  *-------------------------------------------------------------------*/
330
331 static int
332 claw_tx(struct sk_buff *skb, struct net_device *dev)
333 {
334         int             rc;
335         struct claw_privbk *privptr = dev->ml_priv;
336         unsigned long saveflags;
337         struct chbk *p_ch;
338
339         CLAW_DBF_TEXT(4, trace, "claw_tx");
340         p_ch=&privptr->channel[WRITE];
341         if (skb == NULL) {
342                 privptr->stats.tx_dropped++;
343                 privptr->stats.tx_errors++;
344                 CLAW_DBF_TEXT_(2, trace, "clawtx%d", -EIO);
345                 return -EIO;
346         }
347         spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
348         rc=claw_hw_tx( skb, dev, 1 );
349         spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
350         CLAW_DBF_TEXT_(4, trace, "clawtx%d", rc);
351         if (rc)
352                 rc = NETDEV_TX_BUSY;
353         return rc;
354 }   /*  end of claw_tx */
355
356 /*------------------------------------------------------------------*
357  *  pack the collect queue into an skb and return it                *
358  *   If not packing just return the top skb from the queue          *
359  *------------------------------------------------------------------*/
360
361 static struct sk_buff *
362 claw_pack_skb(struct claw_privbk *privptr)
363 {
364         struct sk_buff *new_skb,*held_skb;
365         struct chbk *p_ch = &privptr->channel[WRITE];
366         struct claw_env  *p_env = privptr->p_env;
367         int     pkt_cnt,pk_ind,so_far;
368
369         new_skb = NULL;         /* assume no dice */
370         pkt_cnt = 0;
371         CLAW_DBF_TEXT(4, trace, "PackSKBe");
372         if (!skb_queue_empty(&p_ch->collect_queue)) {
373         /* some data */
374                 held_skb = skb_dequeue(&p_ch->collect_queue);
375                 if (held_skb)
376                         dev_kfree_skb_any(held_skb);
377                 else
378                         return NULL;
379                 if (p_env->packing != DO_PACKED)
380                         return held_skb;
381                 /* get a new SKB we will pack at least one */
382                 new_skb = dev_alloc_skb(p_env->write_size);
383                 if (new_skb == NULL) {
384                         atomic_inc(&held_skb->users);
385                         skb_queue_head(&p_ch->collect_queue,held_skb);
386                         return NULL;
387                 }
388                 /* we have packed packet and a place to put it  */
389                 pk_ind = 1;
390                 so_far = 0;
391                 new_skb->cb[1] = 'P'; /* every skb on queue has pack header */
392                 while ((pk_ind) && (held_skb != NULL)) {
393                         if (held_skb->len+so_far <= p_env->write_size-8) {
394                                 memcpy(skb_put(new_skb,held_skb->len),
395                                         held_skb->data,held_skb->len);
396                                 privptr->stats.tx_packets++;
397                                 so_far += held_skb->len;
398                                 pkt_cnt++;
399                                 dev_kfree_skb_any(held_skb);
400                                 held_skb = skb_dequeue(&p_ch->collect_queue);
401                                 if (held_skb)
402                                         atomic_dec(&held_skb->users);
403                         } else {
404                                 pk_ind = 0;
405                                 atomic_inc(&held_skb->users);
406                                 skb_queue_head(&p_ch->collect_queue,held_skb);
407                         }
408                 }
409         }
410         CLAW_DBF_TEXT(4, trace, "PackSKBx");
411         return new_skb;
412 }
413
414 /*-------------------------------------------------------------------*
415  *   claw_change_mtu                                                 *
416  *                                                                   *
417  *-------------------------------------------------------------------*/
418
419 static int
420 claw_change_mtu(struct net_device *dev, int new_mtu)
421 {
422         struct claw_privbk *privptr = dev->ml_priv;
423         int buff_size;
424         CLAW_DBF_TEXT(4, trace, "setmtu");
425         buff_size = privptr->p_env->write_size;
426         if ((new_mtu < 60) || (new_mtu > buff_size)) {
427                 return -EINVAL;
428         }
429         dev->mtu = new_mtu;
430         return 0;
431 }  /*   end of claw_change_mtu */
432
433
434 /*-------------------------------------------------------------------*
435  *   claw_open                                                       *
436  *                                                                   *
437  *-------------------------------------------------------------------*/
438 static int
439 claw_open(struct net_device *dev)
440 {
441
442         int     rc;
443         int     i;
444         unsigned long       saveflags=0;
445         unsigned long       parm;
446         struct claw_privbk  *privptr;
447         DECLARE_WAITQUEUE(wait, current);
448         struct timer_list  timer;
449         struct ccwbk *p_buf;
450
451         CLAW_DBF_TEXT(4, trace, "open");
452         privptr = (struct claw_privbk *)dev->ml_priv;
453         /*   allocate and initialize CCW blocks */
454         if (privptr->buffs_alloc == 0) {
455                 rc=init_ccw_bk(dev);
456                 if (rc) {
457                         CLAW_DBF_TEXT(2, trace, "openmem");
458                         return -ENOMEM;
459                 }
460         }
461         privptr->system_validate_comp=0;
462         privptr->release_pend=0;
463         if(strncmp(privptr->p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
464                 privptr->p_env->read_size=DEF_PACK_BUFSIZE;
465                 privptr->p_env->write_size=DEF_PACK_BUFSIZE;
466                 privptr->p_env->packing=PACKING_ASK;
467         } else {
468                 privptr->p_env->packing=0;
469                 privptr->p_env->read_size=CLAW_FRAME_SIZE;
470                 privptr->p_env->write_size=CLAW_FRAME_SIZE;
471         }
472         claw_set_busy(dev);
473         tasklet_init(&privptr->channel[READ].tasklet, claw_irq_tasklet,
474                 (unsigned long) &privptr->channel[READ]);
475         for ( i = 0; i < 2;  i++) {
476                 CLAW_DBF_TEXT_(2, trace, "opn_ch%d", i);
477                 init_waitqueue_head(&privptr->channel[i].wait);
478                 /* skb_queue_head_init(&p_ch->io_queue); */
479                 if (i == WRITE)
480                         skb_queue_head_init(
481                                 &privptr->channel[WRITE].collect_queue);
482                 privptr->channel[i].flag_a = 0;
483                 privptr->channel[i].IO_active = 0;
484                 privptr->channel[i].flag  &= ~CLAW_TIMER;
485                 init_timer(&timer);
486                 timer.function = (void *)claw_timer;
487                 timer.data = (unsigned long)(&privptr->channel[i]);
488                 timer.expires = jiffies + 15*HZ;
489                 add_timer(&timer);
490                 spin_lock_irqsave(get_ccwdev_lock(
491                         privptr->channel[i].cdev), saveflags);
492                 parm = (unsigned long) &privptr->channel[i];
493                 privptr->channel[i].claw_state = CLAW_START_HALT_IO;
494                 rc = 0;
495                 add_wait_queue(&privptr->channel[i].wait, &wait);
496                 rc = ccw_device_halt(
497                         (struct ccw_device *)privptr->channel[i].cdev,parm);
498                 set_current_state(TASK_INTERRUPTIBLE);
499                 spin_unlock_irqrestore(
500                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
501                 schedule();
502                 set_current_state(TASK_RUNNING);
503                 remove_wait_queue(&privptr->channel[i].wait, &wait);
504                 if(rc != 0)
505                         ccw_check_return_code(privptr->channel[i].cdev, rc);
506                 if((privptr->channel[i].flag & CLAW_TIMER) == 0x00)
507                         del_timer(&timer);
508         }
509         if ((((privptr->channel[READ].last_dstat |
510                 privptr->channel[WRITE].last_dstat) &
511            ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) ||
512            (((privptr->channel[READ].flag |
513                 privptr->channel[WRITE].flag) & CLAW_TIMER) != 0x00)) {
514                 dev_info(&privptr->channel[READ].cdev->dev,
515                         "%s: remote side is not ready\n", dev->name);
516                 CLAW_DBF_TEXT(2, trace, "notrdy");
517
518                 for ( i = 0; i < 2;  i++) {
519                         spin_lock_irqsave(
520                                 get_ccwdev_lock(privptr->channel[i].cdev),
521                                 saveflags);
522                         parm = (unsigned long) &privptr->channel[i];
523                         privptr->channel[i].claw_state = CLAW_STOP;
524                         rc = ccw_device_halt(
525                                 (struct ccw_device *)&privptr->channel[i].cdev,
526                                 parm);
527                         spin_unlock_irqrestore(
528                                 get_ccwdev_lock(privptr->channel[i].cdev),
529                                 saveflags);
530                         if (rc != 0) {
531                                 ccw_check_return_code(
532                                         privptr->channel[i].cdev, rc);
533                         }
534                 }
535                 free_pages((unsigned long)privptr->p_buff_ccw,
536                         (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
537                 if (privptr->p_env->read_size < PAGE_SIZE) {
538                         free_pages((unsigned long)privptr->p_buff_read,
539                                (int)pages_to_order_of_mag(
540                                         privptr->p_buff_read_num));
541                 }
542                 else {
543                         p_buf=privptr->p_read_active_first;
544                         while (p_buf!=NULL) {
545                                 free_pages((unsigned long)p_buf->p_buffer,
546                                       (int)pages_to_order_of_mag(
547                                         privptr->p_buff_pages_perread ));
548                                 p_buf=p_buf->next;
549                         }
550                 }
551                 if (privptr->p_env->write_size < PAGE_SIZE ) {
552                         free_pages((unsigned long)privptr->p_buff_write,
553                              (int)pages_to_order_of_mag(
554                                 privptr->p_buff_write_num));
555                 }
556                 else {
557                         p_buf=privptr->p_write_active_first;
558                         while (p_buf!=NULL) {
559                                 free_pages((unsigned long)p_buf->p_buffer,
560                                      (int)pages_to_order_of_mag(
561                                         privptr->p_buff_pages_perwrite ));
562                                 p_buf=p_buf->next;
563                         }
564                 }
565                 privptr->buffs_alloc = 0;
566                 privptr->channel[READ].flag= 0x00;
567                 privptr->channel[WRITE].flag = 0x00;
568                 privptr->p_buff_ccw=NULL;
569                 privptr->p_buff_read=NULL;
570                 privptr->p_buff_write=NULL;
571                 claw_clear_busy(dev);
572                 CLAW_DBF_TEXT(2, trace, "open EIO");
573                 return -EIO;
574         }
575
576         /*   Send SystemValidate command */
577
578         claw_clear_busy(dev);
579         CLAW_DBF_TEXT(4, trace, "openok");
580         return 0;
581 }    /*     end of claw_open    */
582
583 /*-------------------------------------------------------------------*
584 *                                                                    *
585 *       claw_irq_handler                                             *
586 *                                                                    *
587 *--------------------------------------------------------------------*/
588 static void
589 claw_irq_handler(struct ccw_device *cdev,
590         unsigned long intparm, struct irb *irb)
591 {
592         struct chbk *p_ch = NULL;
593         struct claw_privbk *privptr = NULL;
594         struct net_device *dev = NULL;
595         struct claw_env  *p_env;
596         struct chbk *p_ch_r=NULL;
597
598         CLAW_DBF_TEXT(4, trace, "clawirq");
599         /* Bypass all 'unsolicited interrupts' */
600         if (!cdev->dev.driver_data) {
601                 dev_warn(&cdev->dev, "An uninitialized CLAW device received an"
602                         " IRQ, c-%02x d-%02x\n",
603                         irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
604                 CLAW_DBF_TEXT(2, trace, "badirq");
605                 return;
606         }
607         privptr = (struct claw_privbk *)cdev->dev.driver_data;
608
609         /* Try to extract channel from driver data. */
610         if (privptr->channel[READ].cdev == cdev)
611                 p_ch = &privptr->channel[READ];
612         else if (privptr->channel[WRITE].cdev == cdev)
613                 p_ch = &privptr->channel[WRITE];
614         else {
615                 dev_warn(&cdev->dev, "The device is not a CLAW device\n");
616                 CLAW_DBF_TEXT(2, trace, "badchan");
617                 return;
618         }
619         CLAW_DBF_TEXT_(4, trace, "IRQCH=%d", p_ch->flag);
620
621         dev = (struct net_device *) (p_ch->ndev);
622         p_env=privptr->p_env;
623
624         /* Copy interruption response block. */
625         memcpy(p_ch->irb, irb, sizeof(struct irb));
626
627         /* Check for good subchannel return code, otherwise info message */
628         if (irb->scsw.cmd.cstat && !(irb->scsw.cmd.cstat & SCHN_STAT_PCI)) {
629                 dev_info(&cdev->dev,
630                         "%s: subchannel check for device: %04x -"
631                         " Sch Stat %02x  Dev Stat %02x CPA - %04x\n",
632                         dev->name, p_ch->devno,
633                         irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
634                         irb->scsw.cmd.cpa);
635                 CLAW_DBF_TEXT(2, trace, "chanchk");
636                 /* return; */
637         }
638
639         /* Check the reason-code of a unit check */
640         if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
641                 ccw_check_unit_check(p_ch, irb->ecw[0]);
642
643         /* State machine to bring the connection up, down and to restart */
644         p_ch->last_dstat = irb->scsw.cmd.dstat;
645
646         switch (p_ch->claw_state) {
647         case CLAW_STOP:/* HALT_IO by claw_release (halt sequence) */
648                 if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
649                 (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
650                 (p_ch->irb->scsw.cmd.stctl ==
651                 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))))
652                         return;
653                 wake_up(&p_ch->wait);   /* wake up claw_release */
654                 CLAW_DBF_TEXT(4, trace, "stop");
655                 return;
656         case CLAW_START_HALT_IO: /* HALT_IO issued by claw_open  */
657                 if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
658                 (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
659                 (p_ch->irb->scsw.cmd.stctl ==
660                 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
661                         CLAW_DBF_TEXT(4, trace, "haltio");
662                         return;
663                 }
664                 if (p_ch->flag == CLAW_READ) {
665                         p_ch->claw_state = CLAW_START_READ;
666                         wake_up(&p_ch->wait); /* wake claw_open (READ)*/
667                 } else if (p_ch->flag == CLAW_WRITE) {
668                         p_ch->claw_state = CLAW_START_WRITE;
669                         /*      send SYSTEM_VALIDATE                    */
670                         claw_strt_read(dev, LOCK_NO);
671                         claw_send_control(dev,
672                                 SYSTEM_VALIDATE_REQUEST,
673                                 0, 0, 0,
674                                 p_env->host_name,
675                                 p_env->adapter_name);
676                 } else {
677                         dev_warn(&cdev->dev, "The CLAW device received"
678                                 " an unexpected IRQ, "
679                                 "c-%02x d-%02x\n",
680                                 irb->scsw.cmd.cstat,
681                                 irb->scsw.cmd.dstat);
682                         return;
683                         }
684                 CLAW_DBF_TEXT(4, trace, "haltio");
685                 return;
686         case CLAW_START_READ:
687                 CLAW_DBF_TEXT(4, trace, "ReadIRQ");
688                 if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
689                         clear_bit(0, (void *)&p_ch->IO_active);
690                         if ((p_ch->irb->ecw[0] & 0x41) == 0x41 ||
691                             (p_ch->irb->ecw[0] & 0x40) == 0x40 ||
692                             (p_ch->irb->ecw[0])        == 0) {
693                                 privptr->stats.rx_errors++;
694                                 dev_info(&cdev->dev,
695                                         "%s: Restart is required after remote "
696                                         "side recovers \n",
697                                         dev->name);
698                         }
699                         CLAW_DBF_TEXT(4, trace, "notrdy");
700                         return;
701                 }
702                 if ((p_ch->irb->scsw.cmd.cstat & SCHN_STAT_PCI) &&
703                         (p_ch->irb->scsw.cmd.dstat == 0)) {
704                         if (test_and_set_bit(CLAW_BH_ACTIVE,
705                                 (void *)&p_ch->flag_a) == 0)
706                                 tasklet_schedule(&p_ch->tasklet);
707                         else
708                                 CLAW_DBF_TEXT(4, trace, "PCINoBH");
709                         CLAW_DBF_TEXT(4, trace, "PCI_read");
710                         return;
711                 }
712                 if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
713                  (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
714                  (p_ch->irb->scsw.cmd.stctl ==
715                  (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
716                         CLAW_DBF_TEXT(4, trace, "SPend_rd");
717                         return;
718                 }
719                 clear_bit(0, (void *)&p_ch->IO_active);
720                 claw_clearbit_busy(TB_RETRY, dev);
721                 if (test_and_set_bit(CLAW_BH_ACTIVE,
722                         (void *)&p_ch->flag_a) == 0)
723                         tasklet_schedule(&p_ch->tasklet);
724                 else
725                         CLAW_DBF_TEXT(4, trace, "RdBHAct");
726                 CLAW_DBF_TEXT(4, trace, "RdIRQXit");
727                 return;
728         case CLAW_START_WRITE:
729                 if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
730                         dev_info(&cdev->dev,
731                                 "%s: Unit Check Occured in "
732                                 "write channel\n", dev->name);
733                         clear_bit(0, (void *)&p_ch->IO_active);
734                         if (p_ch->irb->ecw[0] & 0x80) {
735                                 dev_info(&cdev->dev,
736                                         "%s: Resetting Event "
737                                         "occurred:\n", dev->name);
738                                 init_timer(&p_ch->timer);
739                                 p_ch->timer.function =
740                                         (void *)claw_write_retry;
741                                 p_ch->timer.data = (unsigned long)p_ch;
742                                 p_ch->timer.expires = jiffies + 10*HZ;
743                                 add_timer(&p_ch->timer);
744                                 dev_info(&cdev->dev,
745                                         "%s: write connection "
746                                         "restarting\n", dev->name);
747                         }
748                         CLAW_DBF_TEXT(4, trace, "rstrtwrt");
749                         return;
750                 }
751                 if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
752                         clear_bit(0, (void *)&p_ch->IO_active);
753                         dev_info(&cdev->dev,
754                                 "%s: Unit Exception "
755                                 "occurred in write channel\n",
756                                 dev->name);
757                 }
758                 if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
759                 (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
760                 (p_ch->irb->scsw.cmd.stctl ==
761                 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
762                         CLAW_DBF_TEXT(4, trace, "writeUE");
763                         return;
764                 }
765                 clear_bit(0, (void *)&p_ch->IO_active);
766                 if (claw_test_and_setbit_busy(TB_TX, dev) == 0) {
767                         claw_write_next(p_ch);
768                         claw_clearbit_busy(TB_TX, dev);
769                         claw_clear_busy(dev);
770                 }
771                 p_ch_r = (struct chbk *)&privptr->channel[READ];
772                 if (test_and_set_bit(CLAW_BH_ACTIVE,
773                         (void *)&p_ch_r->flag_a) == 0)
774                         tasklet_schedule(&p_ch_r->tasklet);
775                 CLAW_DBF_TEXT(4, trace, "StWtExit");
776                 return;
777         default:
778                 dev_warn(&cdev->dev,
779                         "The CLAW device for %s received an unexpected IRQ\n",
780                          dev->name);
781                 CLAW_DBF_TEXT(2, trace, "badIRQ");
782                 return;
783         }
784
785 }       /*   end of claw_irq_handler    */
786
787
788 /*-------------------------------------------------------------------*
789 *       claw_irq_tasklet                                             *
790 *                                                                    *
791 *--------------------------------------------------------------------*/
792 static void
793 claw_irq_tasklet ( unsigned long data )
794 {
795         struct chbk * p_ch;
796         struct net_device  *dev;
797         struct claw_privbk *       privptr;
798
799         p_ch = (struct chbk *) data;
800         dev = (struct net_device *)p_ch->ndev;
801         CLAW_DBF_TEXT(4, trace, "IRQtask");
802         privptr = (struct claw_privbk *)dev->ml_priv;
803         unpack_read(dev);
804         clear_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a);
805         CLAW_DBF_TEXT(4, trace, "TskletXt");
806         return;
807 }       /*    end of claw_irq_bh    */
808
809 /*-------------------------------------------------------------------*
810 *       claw_release                                                 *
811 *                                                                    *
812 *--------------------------------------------------------------------*/
813 static int
814 claw_release(struct net_device *dev)
815 {
816         int                rc;
817         int                i;
818         unsigned long      saveflags;
819         unsigned long      parm;
820         struct claw_privbk *privptr;
821         DECLARE_WAITQUEUE(wait, current);
822         struct ccwbk*             p_this_ccw;
823         struct ccwbk*             p_buf;
824
825         if (!dev)
826                 return 0;
827         privptr = (struct claw_privbk *)dev->ml_priv;
828         if (!privptr)
829                 return 0;
830         CLAW_DBF_TEXT(4, trace, "release");
831         privptr->release_pend=1;
832         claw_setbit_busy(TB_STOP,dev);
833         for ( i = 1; i >=0 ;  i--) {
834                 spin_lock_irqsave(
835                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
836              /*   del_timer(&privptr->channel[READ].timer);  */
837                 privptr->channel[i].claw_state = CLAW_STOP;
838                 privptr->channel[i].IO_active = 0;
839                 parm = (unsigned long) &privptr->channel[i];
840                 if (i == WRITE)
841                         claw_purge_skb_queue(
842                                 &privptr->channel[WRITE].collect_queue);
843                 rc = ccw_device_halt (privptr->channel[i].cdev, parm);
844                 if (privptr->system_validate_comp==0x00)  /* never opened? */
845                    init_waitqueue_head(&privptr->channel[i].wait);
846                 add_wait_queue(&privptr->channel[i].wait, &wait);
847                 set_current_state(TASK_INTERRUPTIBLE);
848                 spin_unlock_irqrestore(
849                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
850                 schedule();
851                 set_current_state(TASK_RUNNING);
852                 remove_wait_queue(&privptr->channel[i].wait, &wait);
853                 if (rc != 0) {
854                         ccw_check_return_code(privptr->channel[i].cdev, rc);
855                 }
856         }
857         if (privptr->pk_skb != NULL) {
858                 dev_kfree_skb_any(privptr->pk_skb);
859                 privptr->pk_skb = NULL;
860         }
861         if(privptr->buffs_alloc != 1) {
862                 CLAW_DBF_TEXT(4, trace, "none2fre");
863                 return 0;
864         }
865         CLAW_DBF_TEXT(4, trace, "freebufs");
866         if (privptr->p_buff_ccw != NULL) {
867                 free_pages((unsigned long)privptr->p_buff_ccw,
868                         (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
869         }
870         CLAW_DBF_TEXT(4, trace, "freeread");
871         if (privptr->p_env->read_size < PAGE_SIZE) {
872             if (privptr->p_buff_read != NULL) {
873                 free_pages((unsigned long)privptr->p_buff_read,
874                       (int)pages_to_order_of_mag(privptr->p_buff_read_num));
875                 }
876         }
877         else {
878                 p_buf=privptr->p_read_active_first;
879                 while (p_buf!=NULL) {
880                         free_pages((unsigned long)p_buf->p_buffer,
881                              (int)pages_to_order_of_mag(
882                                 privptr->p_buff_pages_perread ));
883                         p_buf=p_buf->next;
884                 }
885         }
886          CLAW_DBF_TEXT(4, trace, "freewrit");
887         if (privptr->p_env->write_size < PAGE_SIZE ) {
888                 free_pages((unsigned long)privptr->p_buff_write,
889                       (int)pages_to_order_of_mag(privptr->p_buff_write_num));
890         }
891         else {
892                 p_buf=privptr->p_write_active_first;
893                 while (p_buf!=NULL) {
894                         free_pages((unsigned long)p_buf->p_buffer,
895                               (int)pages_to_order_of_mag(
896                               privptr->p_buff_pages_perwrite ));
897                         p_buf=p_buf->next;
898                 }
899         }
900          CLAW_DBF_TEXT(4, trace, "clearptr");
901         privptr->buffs_alloc = 0;
902         privptr->p_buff_ccw=NULL;
903         privptr->p_buff_read=NULL;
904         privptr->p_buff_write=NULL;
905         privptr->system_validate_comp=0;
906         privptr->release_pend=0;
907         /*      Remove any writes that were pending and reset all reads   */
908         p_this_ccw=privptr->p_read_active_first;
909         while (p_this_ccw!=NULL) {
910                 p_this_ccw->header.length=0xffff;
911                 p_this_ccw->header.opcode=0xff;
912                 p_this_ccw->header.flag=0x00;
913                 p_this_ccw=p_this_ccw->next;
914         }
915
916         while (privptr->p_write_active_first!=NULL) {
917                 p_this_ccw=privptr->p_write_active_first;
918                 p_this_ccw->header.flag=CLAW_PENDING;
919                 privptr->p_write_active_first=p_this_ccw->next;
920                 p_this_ccw->next=privptr->p_write_free_chain;
921                 privptr->p_write_free_chain=p_this_ccw;
922                 ++privptr->write_free_count;
923         }
924         privptr->p_write_active_last=NULL;
925         privptr->mtc_logical_link = -1;
926         privptr->mtc_skipping = 1;
927         privptr->mtc_offset=0;
928
929         if (((privptr->channel[READ].last_dstat |
930                 privptr->channel[WRITE].last_dstat) &
931                 ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) {
932                 dev_warn(&privptr->channel[READ].cdev->dev,
933                         "Deactivating %s completed with incorrect"
934                         " subchannel status "
935                         "(read %02x, write %02x)\n",
936                 dev->name,
937                 privptr->channel[READ].last_dstat,
938                 privptr->channel[WRITE].last_dstat);
939                  CLAW_DBF_TEXT(2, trace, "badclose");
940         }
941         CLAW_DBF_TEXT(4, trace, "rlsexit");
942         return 0;
943 }      /* end of claw_release     */
944
945 /*-------------------------------------------------------------------*
946 *       claw_write_retry                                             *
947 *                                                                    *
948 *--------------------------------------------------------------------*/
949
950 static void
951 claw_write_retry ( struct chbk *p_ch )
952 {
953
954         struct net_device  *dev=p_ch->ndev;
955
956         CLAW_DBF_TEXT(4, trace, "w_retry");
957         if (p_ch->claw_state == CLAW_STOP) {
958                 return;
959         }
960         claw_strt_out_IO( dev );
961         CLAW_DBF_TEXT(4, trace, "rtry_xit");
962         return;
963 }      /* end of claw_write_retry      */
964
965
966 /*-------------------------------------------------------------------*
967 *       claw_write_next                                              *
968 *                                                                    *
969 *--------------------------------------------------------------------*/
970
971 static void
972 claw_write_next ( struct chbk * p_ch )
973 {
974
975         struct net_device  *dev;
976         struct claw_privbk *privptr=NULL;
977         struct sk_buff *pk_skb;
978         int     rc;
979
980         CLAW_DBF_TEXT(4, trace, "claw_wrt");
981         if (p_ch->claw_state == CLAW_STOP)
982                 return;
983         dev = (struct net_device *) p_ch->ndev;
984         privptr = (struct claw_privbk *) dev->ml_priv;
985         claw_free_wrt_buf( dev );
986         if ((privptr->write_free_count > 0) &&
987             !skb_queue_empty(&p_ch->collect_queue)) {
988                 pk_skb = claw_pack_skb(privptr);
989                 while (pk_skb != NULL) {
990                         rc = claw_hw_tx( pk_skb, dev,1);
991                         if (privptr->write_free_count > 0) {
992                                 pk_skb = claw_pack_skb(privptr);
993                         } else
994                                 pk_skb = NULL;
995                 }
996         }
997         if (privptr->p_write_active_first!=NULL) {
998                 claw_strt_out_IO(dev);
999         }
1000         return;
1001 }      /* end of claw_write_next      */
1002
1003 /*-------------------------------------------------------------------*
1004 *                                                                    *
1005 *       claw_timer                                                   *
1006 *--------------------------------------------------------------------*/
1007
1008 static void
1009 claw_timer ( struct chbk * p_ch )
1010 {
1011         CLAW_DBF_TEXT(4, trace, "timer");
1012         p_ch->flag |= CLAW_TIMER;
1013         wake_up(&p_ch->wait);
1014         return;
1015 }      /* end of claw_timer  */
1016
1017 /*
1018 *
1019 *       functions
1020 */
1021
1022
1023 /*-------------------------------------------------------------------*
1024 *                                                                    *
1025 *     pages_to_order_of_mag                                          *
1026 *                                                                    *
1027 *    takes a number of pages from 1 to 512 and returns the           *
1028 *    log(num_pages)/log(2) get_free_pages() needs a base 2 order     *
1029 *    of magnitude get_free_pages() has an upper order of 9           *
1030 *--------------------------------------------------------------------*/
1031
1032 static int
1033 pages_to_order_of_mag(int num_of_pages)
1034 {
1035         int     order_of_mag=1;         /* assume 2 pages */
1036         int     nump=2;
1037
1038         CLAW_DBF_TEXT_(5, trace, "pages%d", num_of_pages);
1039         if (num_of_pages == 1)   {return 0; }  /* magnitude of 0 = 1 page */
1040         /* 512 pages = 2Meg on 4k page systems */
1041         if (num_of_pages >= 512) {return 9; }
1042         /* we have two or more pages order is at least 1 */
1043         for (nump=2 ;nump <= 512;nump*=2) {
1044           if (num_of_pages <= nump)
1045                   break;
1046           order_of_mag +=1;
1047         }
1048         if (order_of_mag > 9) { order_of_mag = 9; }  /* I know it's paranoid */
1049         CLAW_DBF_TEXT_(5, trace, "mag%d", order_of_mag);
1050         return order_of_mag;
1051 }
1052
1053 /*-------------------------------------------------------------------*
1054 *                                                                    *
1055 *     add_claw_reads                                                 *
1056 *                                                                    *
1057 *--------------------------------------------------------------------*/
1058 static int
1059 add_claw_reads(struct net_device *dev, struct ccwbk* p_first,
1060         struct ccwbk* p_last)
1061 {
1062         struct claw_privbk *privptr;
1063         struct ccw1  temp_ccw;
1064         struct endccw * p_end;
1065         CLAW_DBF_TEXT(4, trace, "addreads");
1066         privptr = dev->ml_priv;
1067         p_end = privptr->p_end_ccw;
1068
1069         /* first CCW and last CCW contains a new set of read channel programs
1070         *       to apend the running channel programs
1071         */
1072         if ( p_first==NULL) {
1073                 CLAW_DBF_TEXT(4, trace, "addexit");
1074                 return 0;
1075         }
1076
1077         /* set up ending CCW sequence for this segment */
1078         if (p_end->read1) {
1079                 p_end->read1=0x00;    /*  second ending CCW is now active */
1080                 /*      reset ending CCWs and setup TIC CCWs              */
1081                 p_end->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1082                 p_end->read2_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1083                 p_last->r_TIC_1.cda =(__u32)__pa(&p_end->read2_nop1);
1084                 p_last->r_TIC_2.cda =(__u32)__pa(&p_end->read2_nop1);
1085                 p_end->read2_nop2.cda=0;
1086                 p_end->read2_nop2.count=1;
1087         }
1088         else {
1089                 p_end->read1=0x01;  /* first ending CCW is now active */
1090                 /*      reset ending CCWs and setup TIC CCWs          */
1091                 p_end->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1092                 p_end->read1_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1093                 p_last->r_TIC_1.cda = (__u32)__pa(&p_end->read1_nop1);
1094                 p_last->r_TIC_2.cda = (__u32)__pa(&p_end->read1_nop1);
1095                 p_end->read1_nop2.cda=0;
1096                 p_end->read1_nop2.count=1;
1097         }
1098
1099         if ( privptr-> p_read_active_first ==NULL ) {
1100                 privptr->p_read_active_first = p_first;  /*  set new first */
1101                 privptr->p_read_active_last  = p_last;   /*  set new last  */
1102         }
1103         else {
1104
1105                 /* set up TIC ccw  */
1106                 temp_ccw.cda= (__u32)__pa(&p_first->read);
1107                 temp_ccw.count=0;
1108                 temp_ccw.flags=0;
1109                 temp_ccw.cmd_code = CCW_CLAW_CMD_TIC;
1110
1111
1112                 if (p_end->read1) {
1113
1114                /* first set of CCW's is chained to the new read              */
1115                /* chain, so the second set is chained to the active chain.   */
1116                /* Therefore modify the second set to point to the new        */
1117                /* read chain set up TIC CCWs                                 */
1118                /* make sure we update the CCW so channel doesn't fetch it    */
1119                /* when it's only half done                                   */
1120                         memcpy( &p_end->read2_nop2, &temp_ccw ,
1121                                 sizeof(struct ccw1));
1122                         privptr->p_read_active_last->r_TIC_1.cda=
1123                                 (__u32)__pa(&p_first->read);
1124                         privptr->p_read_active_last->r_TIC_2.cda=
1125                                 (__u32)__pa(&p_first->read);
1126                 }
1127                 else {
1128                         /* make sure we update the CCW so channel doesn't   */
1129                         /* fetch it when it is only half done               */
1130                         memcpy( &p_end->read1_nop2, &temp_ccw ,
1131                                 sizeof(struct ccw1));
1132                         privptr->p_read_active_last->r_TIC_1.cda=
1133                                 (__u32)__pa(&p_first->read);
1134                         privptr->p_read_active_last->r_TIC_2.cda=
1135                                 (__u32)__pa(&p_first->read);
1136                 }
1137                 /*      chain in new set of blocks                         */
1138                 privptr->p_read_active_last->next = p_first;
1139                 privptr->p_read_active_last=p_last;
1140         } /* end of if ( privptr-> p_read_active_first ==NULL)  */
1141         CLAW_DBF_TEXT(4, trace, "addexit");
1142         return 0;
1143 }    /*     end of add_claw_reads   */
1144
1145 /*-------------------------------------------------------------------*
1146  *   ccw_check_return_code                                           *
1147  *                                                                   *
1148  *-------------------------------------------------------------------*/
1149
1150 static void
1151 ccw_check_return_code(struct ccw_device *cdev, int return_code)
1152 {
1153         CLAW_DBF_TEXT(4, trace, "ccwret");
1154         if (return_code != 0) {
1155                 switch (return_code) {
1156                 case -EBUSY: /* BUSY is a transient state no action needed */
1157                         break;
1158                 case -ENODEV:
1159                         dev_err(&cdev->dev, "The remote channel adapter is not"
1160                                 " available\n");
1161                         break;
1162                 case -EINVAL:
1163                         dev_err(&cdev->dev,
1164                                 "The status of the remote channel adapter"
1165                                 " is not valid\n");
1166                         break;
1167                 default:
1168                         dev_err(&cdev->dev, "The common device layer"
1169                                 " returned error code %d\n",
1170                                   return_code);
1171                 }
1172         }
1173         CLAW_DBF_TEXT(4, trace, "ccwret");
1174 }    /*    end of ccw_check_return_code   */
1175
1176 /*-------------------------------------------------------------------*
1177 *       ccw_check_unit_check                                         *
1178 *--------------------------------------------------------------------*/
1179
1180 static void
1181 ccw_check_unit_check(struct chbk * p_ch, unsigned char sense )
1182 {
1183         struct net_device *ndev = p_ch->ndev;
1184         struct device *dev = &p_ch->cdev->dev;
1185
1186         CLAW_DBF_TEXT(4, trace, "unitchek");
1187         dev_warn(dev, "The communication peer of %s disconnected\n",
1188                 ndev->name);
1189
1190         if (sense & 0x40) {
1191                 if (sense & 0x01) {
1192                         dev_warn(dev, "The remote channel adapter for"
1193                                 " %s has been reset\n",
1194                                 ndev->name);
1195                 }
1196         }
1197         else if (sense & 0x20) {
1198                 if (sense & 0x04) {
1199                         dev_warn(dev, "A data streaming timeout occurred"
1200                                 " for %s\n",
1201                                 ndev->name);
1202                 }
1203                 else  {
1204                         dev_warn(dev, "A data transfer parity error occurred"
1205                                 " for %s\n",
1206                                 ndev->name);
1207                 }
1208         }
1209         else if (sense & 0x10) {
1210                 if (sense & 0x20) {
1211                         dev_warn(dev, "The remote channel adapter for %s"
1212                                 " is faulty\n",
1213                                 ndev->name);
1214                 }
1215                 else {
1216                         dev_warn(dev, "A read data parity error occurred"
1217                                 " for %s\n",
1218                                 ndev->name);
1219                 }
1220         }
1221
1222 }   /*    end of ccw_check_unit_check    */
1223
1224 /*-------------------------------------------------------------------*
1225 *               find_link                                            *
1226 *--------------------------------------------------------------------*/
1227 static int
1228 find_link(struct net_device *dev, char *host_name, char *ws_name )
1229 {
1230         struct claw_privbk *privptr;
1231         struct claw_env *p_env;
1232         int    rc=0;
1233
1234         CLAW_DBF_TEXT(2, setup, "findlink");
1235         privptr = dev->ml_priv;
1236         p_env=privptr->p_env;
1237         switch (p_env->packing)
1238         {
1239                 case  PACKING_ASK:
1240                         if ((memcmp(WS_APPL_NAME_PACKED, host_name, 8)!=0) ||
1241                             (memcmp(WS_APPL_NAME_PACKED, ws_name, 8)!=0 ))
1242                              rc = EINVAL;
1243                         break;
1244                 case  DO_PACKED:
1245                 case  PACK_SEND:
1246                         if ((memcmp(WS_APPL_NAME_IP_NAME, host_name, 8)!=0) ||
1247                             (memcmp(WS_APPL_NAME_IP_NAME, ws_name, 8)!=0 ))
1248                                 rc = EINVAL;
1249                         break;
1250                 default:
1251                         if ((memcmp(HOST_APPL_NAME, host_name, 8)!=0) ||
1252                             (memcmp(p_env->api_type , ws_name, 8)!=0))
1253                                 rc = EINVAL;
1254                         break;
1255         }
1256
1257         return 0;
1258 }    /*    end of find_link    */
1259
1260 /*-------------------------------------------------------------------*
1261  *   claw_hw_tx                                                      *
1262  *                                                                   *
1263  *                                                                   *
1264  *-------------------------------------------------------------------*/
1265
1266 static int
1267 claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid)
1268 {
1269         int                             rc=0;
1270         struct claw_privbk              *privptr;
1271         struct ccwbk           *p_this_ccw;
1272         struct ccwbk           *p_first_ccw;
1273         struct ccwbk           *p_last_ccw;
1274         __u32                           numBuffers;
1275         signed long                     len_of_data;
1276         unsigned long                   bytesInThisBuffer;
1277         unsigned char                   *pDataAddress;
1278         struct endccw                   *pEnd;
1279         struct ccw1                     tempCCW;
1280         struct chbk                     *p_ch;
1281         struct claw_env                 *p_env;
1282         int                             lock;
1283         struct clawph                   *pk_head;
1284         struct chbk                     *ch;
1285
1286         CLAW_DBF_TEXT(4, trace, "hw_tx");
1287         privptr = (struct claw_privbk *)(dev->ml_priv);
1288         p_ch=(struct chbk *)&privptr->channel[WRITE];
1289         p_env =privptr->p_env;
1290         claw_free_wrt_buf(dev); /* Clean up free chain if posible */
1291         /*  scan the write queue to free any completed write packets   */
1292         p_first_ccw=NULL;
1293         p_last_ccw=NULL;
1294         if ((p_env->packing >= PACK_SEND) &&
1295             (skb->cb[1] != 'P')) {
1296                 skb_push(skb,sizeof(struct clawph));
1297                 pk_head=(struct clawph *)skb->data;
1298                 pk_head->len=skb->len-sizeof(struct clawph);
1299                 if (pk_head->len%4)  {
1300                         pk_head->len+= 4-(pk_head->len%4);
1301                         skb_pad(skb,4-(pk_head->len%4));
1302                         skb_put(skb,4-(pk_head->len%4));
1303                 }
1304                 if (p_env->packing == DO_PACKED)
1305                         pk_head->link_num = linkid;
1306                 else
1307                         pk_head->link_num = 0;
1308                 pk_head->flag = 0x00;
1309                 skb_pad(skb,4);
1310                 skb->cb[1] = 'P';
1311         }
1312         if (linkid == 0) {
1313                 if (claw_check_busy(dev)) {
1314                         if (privptr->write_free_count!=0) {
1315                                 claw_clear_busy(dev);
1316                         }
1317                         else {
1318                                 claw_strt_out_IO(dev );
1319                                 claw_free_wrt_buf( dev );
1320                                 if (privptr->write_free_count==0) {
1321                                         ch = &privptr->channel[WRITE];
1322                                         atomic_inc(&skb->users);
1323                                         skb_queue_tail(&ch->collect_queue, skb);
1324                                         goto Done;
1325                                 }
1326                                 else {
1327                                         claw_clear_busy(dev);
1328                                 }
1329                         }
1330                 }
1331                 /*  tx lock  */
1332                 if (claw_test_and_setbit_busy(TB_TX,dev)) { /* set to busy */
1333                         ch = &privptr->channel[WRITE];
1334                         atomic_inc(&skb->users);
1335                         skb_queue_tail(&ch->collect_queue, skb);
1336                         claw_strt_out_IO(dev );
1337                         rc=-EBUSY;
1338                         goto Done2;
1339                 }
1340         }
1341         /*      See how many write buffers are required to hold this data */
1342         numBuffers = DIV_ROUND_UP(skb->len, privptr->p_env->write_size);
1343
1344         /*      If that number of buffers isn't available, give up for now */
1345         if (privptr->write_free_count < numBuffers ||
1346             privptr->p_write_free_chain == NULL ) {
1347
1348                 claw_setbit_busy(TB_NOBUFFER,dev);
1349                 ch = &privptr->channel[WRITE];
1350                 atomic_inc(&skb->users);
1351                 skb_queue_tail(&ch->collect_queue, skb);
1352                 CLAW_DBF_TEXT(2, trace, "clawbusy");
1353                 goto Done2;
1354         }
1355         pDataAddress=skb->data;
1356         len_of_data=skb->len;
1357
1358         while (len_of_data > 0) {
1359                 p_this_ccw=privptr->p_write_free_chain;  /* get a block */
1360                 if (p_this_ccw == NULL) { /* lost the race */
1361                         ch = &privptr->channel[WRITE];
1362                         atomic_inc(&skb->users);
1363                         skb_queue_tail(&ch->collect_queue, skb);
1364                         goto Done2;
1365                 }
1366                 privptr->p_write_free_chain=p_this_ccw->next;
1367                 p_this_ccw->next=NULL;
1368                 --privptr->write_free_count; /* -1 */
1369                 bytesInThisBuffer=len_of_data;
1370                 memcpy( p_this_ccw->p_buffer,pDataAddress, bytesInThisBuffer);
1371                 len_of_data-=bytesInThisBuffer;
1372                 pDataAddress+=(unsigned long)bytesInThisBuffer;
1373                 /*      setup write CCW         */
1374                 p_this_ccw->write.cmd_code = (linkid * 8) +1;
1375                 if (len_of_data>0) {
1376                         p_this_ccw->write.cmd_code+=MORE_to_COME_FLAG;
1377                 }
1378                 p_this_ccw->write.count=bytesInThisBuffer;
1379                 /*      now add to end of this chain    */
1380                 if (p_first_ccw==NULL)    {
1381                         p_first_ccw=p_this_ccw;
1382                 }
1383                 if (p_last_ccw!=NULL) {
1384                         p_last_ccw->next=p_this_ccw;
1385                         /*      set up TIC ccws         */
1386                         p_last_ccw->w_TIC_1.cda=
1387                                 (__u32)__pa(&p_this_ccw->write);
1388                 }
1389                 p_last_ccw=p_this_ccw;      /* save new last block */
1390         }
1391
1392         /*      FirstCCW and LastCCW now contain a new set of write channel
1393         *       programs to append to the running channel program
1394         */
1395
1396         if (p_first_ccw!=NULL) {
1397                 /*      setup ending ccw sequence for this segment           */
1398                 pEnd=privptr->p_end_ccw;
1399                 if (pEnd->write1) {
1400                         pEnd->write1=0x00;   /* second end ccw is now active */
1401                         /*      set up Tic CCWs         */
1402                         p_last_ccw->w_TIC_1.cda=
1403                                 (__u32)__pa(&pEnd->write2_nop1);
1404                         pEnd->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1405                         pEnd->write2_nop2.flags    =
1406                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1407                         pEnd->write2_nop2.cda=0;
1408                         pEnd->write2_nop2.count=1;
1409                 }
1410                 else {  /*  end of if (pEnd->write1)*/
1411                         pEnd->write1=0x01;   /* first end ccw is now active */
1412                         /*      set up Tic CCWs         */
1413                         p_last_ccw->w_TIC_1.cda=
1414                                 (__u32)__pa(&pEnd->write1_nop1);
1415                         pEnd->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1416                         pEnd->write1_nop2.flags    =
1417                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1418                         pEnd->write1_nop2.cda=0;
1419                         pEnd->write1_nop2.count=1;
1420                 }  /* end if if (pEnd->write1) */
1421
1422                 if (privptr->p_write_active_first==NULL ) {
1423                         privptr->p_write_active_first=p_first_ccw;
1424                         privptr->p_write_active_last=p_last_ccw;
1425                 }
1426                 else {
1427                         /*      set up Tic CCWs         */
1428
1429                         tempCCW.cda=(__u32)__pa(&p_first_ccw->write);
1430                         tempCCW.count=0;
1431                         tempCCW.flags=0;
1432                         tempCCW.cmd_code=CCW_CLAW_CMD_TIC;
1433
1434                         if (pEnd->write1) {
1435
1436                  /*
1437                  * first set of ending CCW's is chained to the new write
1438                  * chain, so the second set is chained to the active chain
1439                  * Therefore modify the second set to point the new write chain.
1440                  * make sure we update the CCW atomically
1441                  * so channel does not fetch it when it's only half done
1442                  */
1443                                 memcpy( &pEnd->write2_nop2, &tempCCW ,
1444                                         sizeof(struct ccw1));
1445                                 privptr->p_write_active_last->w_TIC_1.cda=
1446                                         (__u32)__pa(&p_first_ccw->write);
1447                         }
1448                         else {
1449
1450                         /*make sure we update the CCW atomically
1451                          *so channel does not fetch it when it's only half done
1452                          */
1453                                 memcpy(&pEnd->write1_nop2, &tempCCW ,
1454                                         sizeof(struct ccw1));
1455                                 privptr->p_write_active_last->w_TIC_1.cda=
1456                                         (__u32)__pa(&p_first_ccw->write);
1457
1458                         } /* end if if (pEnd->write1) */
1459
1460                         privptr->p_write_active_last->next=p_first_ccw;
1461                         privptr->p_write_active_last=p_last_ccw;
1462                 }
1463
1464         } /* endif (p_first_ccw!=NULL)  */
1465         dev_kfree_skb_any(skb);
1466         if (linkid==0) {
1467                 lock=LOCK_NO;
1468         }
1469         else  {
1470                 lock=LOCK_YES;
1471         }
1472         claw_strt_out_IO(dev );
1473         /*      if write free count is zero , set NOBUFFER       */
1474         if (privptr->write_free_count==0) {
1475                 claw_setbit_busy(TB_NOBUFFER,dev);
1476         }
1477 Done2:
1478         claw_clearbit_busy(TB_TX,dev);
1479 Done:
1480         return(rc);
1481 }    /*    end of claw_hw_tx    */
1482
1483 /*-------------------------------------------------------------------*
1484 *                                                                    *
1485 *     init_ccw_bk                                                    *
1486 *                                                                    *
1487 *--------------------------------------------------------------------*/
1488
1489 static int
1490 init_ccw_bk(struct net_device *dev)
1491 {
1492
1493         __u32   ccw_blocks_required;
1494         __u32   ccw_blocks_perpage;
1495         __u32   ccw_pages_required;
1496         __u32   claw_reads_perpage=1;
1497         __u32   claw_read_pages;
1498         __u32   claw_writes_perpage=1;
1499         __u32   claw_write_pages;
1500         void    *p_buff=NULL;
1501         struct ccwbk*p_free_chain;
1502         struct ccwbk*p_buf;
1503         struct ccwbk*p_last_CCWB;
1504         struct ccwbk*p_first_CCWB;
1505         struct endccw *p_endccw=NULL;
1506         addr_t  real_address;
1507         struct claw_privbk *privptr = dev->ml_priv;
1508         struct clawh *pClawH=NULL;
1509         addr_t   real_TIC_address;
1510         int i,j;
1511         CLAW_DBF_TEXT(4, trace, "init_ccw");
1512
1513         /*  initialize  statistics field */
1514         privptr->active_link_ID=0;
1515         /*  initialize  ccwbk pointers  */
1516         privptr->p_write_free_chain=NULL;   /* pointer to free ccw chain*/
1517         privptr->p_write_active_first=NULL; /* pointer to the first write ccw*/
1518         privptr->p_write_active_last=NULL;  /* pointer to the last write ccw*/
1519         privptr->p_read_active_first=NULL;  /* pointer to the first read ccw*/
1520         privptr->p_read_active_last=NULL;   /* pointer to the last read ccw */
1521         privptr->p_end_ccw=NULL;            /* pointer to ending ccw        */
1522         privptr->p_claw_signal_blk=NULL;    /* pointer to signal block      */
1523         privptr->buffs_alloc = 0;
1524         memset(&privptr->end_ccw, 0x00, sizeof(struct endccw));
1525         memset(&privptr->ctl_bk, 0x00, sizeof(struct clawctl));
1526         /*  initialize  free write ccwbk counter  */
1527         privptr->write_free_count=0;  /* number of free bufs on write chain */
1528         p_last_CCWB = NULL;
1529         p_first_CCWB= NULL;
1530         /*
1531         *  We need 1 CCW block for each read buffer, 1 for each
1532         *  write buffer, plus 1 for ClawSignalBlock
1533         */
1534         ccw_blocks_required =
1535                 privptr->p_env->read_buffers+privptr->p_env->write_buffers+1;
1536         /*
1537         * compute number of CCW blocks that will fit in a page
1538         */
1539         ccw_blocks_perpage= PAGE_SIZE /  CCWBK_SIZE;
1540         ccw_pages_required=
1541                 DIV_ROUND_UP(ccw_blocks_required, ccw_blocks_perpage);
1542
1543         /*
1544          *  read and write sizes are set by 2 constants in claw.h
1545          *  4k and 32k.  Unpacked values other than 4k are not going to
1546          * provide good performance. With packing buffers support 32k
1547          * buffers are used.
1548          */
1549         if (privptr->p_env->read_size < PAGE_SIZE) {
1550                 claw_reads_perpage = PAGE_SIZE / privptr->p_env->read_size;
1551                 claw_read_pages = DIV_ROUND_UP(privptr->p_env->read_buffers,
1552                                                 claw_reads_perpage);
1553          }
1554          else {       /* > or equal  */
1555                 privptr->p_buff_pages_perread =
1556                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
1557                 claw_read_pages = privptr->p_env->read_buffers *
1558                                         privptr->p_buff_pages_perread;
1559          }
1560         if (privptr->p_env->write_size < PAGE_SIZE) {
1561                 claw_writes_perpage =
1562                         PAGE_SIZE / privptr->p_env->write_size;
1563                 claw_write_pages = DIV_ROUND_UP(privptr->p_env->write_buffers,
1564                                                 claw_writes_perpage);
1565
1566         }
1567         else {      /* >  or equal  */
1568                 privptr->p_buff_pages_perwrite =
1569                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
1570                 claw_write_pages = privptr->p_env->write_buffers *
1571                                         privptr->p_buff_pages_perwrite;
1572         }
1573         /*
1574         *               allocate ccw_pages_required
1575         */
1576         if (privptr->p_buff_ccw==NULL) {
1577                 privptr->p_buff_ccw=
1578                         (void *)__get_free_pages(__GFP_DMA,
1579                         (int)pages_to_order_of_mag(ccw_pages_required ));
1580                 if (privptr->p_buff_ccw==NULL) {
1581                         return -ENOMEM;
1582                 }
1583                 privptr->p_buff_ccw_num=ccw_pages_required;
1584         }
1585         memset(privptr->p_buff_ccw, 0x00,
1586                 privptr->p_buff_ccw_num * PAGE_SIZE);
1587
1588         /*
1589         *               obtain ending ccw block address
1590         *
1591         */
1592         privptr->p_end_ccw = (struct endccw *)&privptr->end_ccw;
1593         real_address  = (__u32)__pa(privptr->p_end_ccw);
1594         /*                              Initialize ending CCW block       */
1595         p_endccw=privptr->p_end_ccw;
1596         p_endccw->real=real_address;
1597         p_endccw->write1=0x00;
1598         p_endccw->read1=0x00;
1599
1600         /*      write1_nop1                                     */
1601         p_endccw->write1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1602         p_endccw->write1_nop1.flags       = CCW_FLAG_SLI | CCW_FLAG_CC;
1603         p_endccw->write1_nop1.count       = 1;
1604         p_endccw->write1_nop1.cda         = 0;
1605
1606         /*      write1_nop2                                     */
1607         p_endccw->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1608         p_endccw->write1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1609         p_endccw->write1_nop2.count      = 1;
1610         p_endccw->write1_nop2.cda        = 0;
1611
1612         /*      write2_nop1                                     */
1613         p_endccw->write2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1614         p_endccw->write2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1615         p_endccw->write2_nop1.count        = 1;
1616         p_endccw->write2_nop1.cda          = 0;
1617
1618         /*      write2_nop2                                     */
1619         p_endccw->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1620         p_endccw->write2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1621         p_endccw->write2_nop2.count        = 1;
1622         p_endccw->write2_nop2.cda          = 0;
1623
1624         /*      read1_nop1                                      */
1625         p_endccw->read1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1626         p_endccw->read1_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1627         p_endccw->read1_nop1.count        = 1;
1628         p_endccw->read1_nop1.cda          = 0;
1629
1630         /*      read1_nop2                                      */
1631         p_endccw->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1632         p_endccw->read1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1633         p_endccw->read1_nop2.count        = 1;
1634         p_endccw->read1_nop2.cda          = 0;
1635
1636         /*      read2_nop1                                      */
1637         p_endccw->read2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1638         p_endccw->read2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1639         p_endccw->read2_nop1.count        = 1;
1640         p_endccw->read2_nop1.cda          = 0;
1641
1642         /*      read2_nop2                                      */
1643         p_endccw->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1644         p_endccw->read2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1645         p_endccw->read2_nop2.count        = 1;
1646         p_endccw->read2_nop2.cda          = 0;
1647
1648         /*
1649         *                               Build a chain of CCWs
1650         *
1651         */
1652         p_buff=privptr->p_buff_ccw;
1653
1654         p_free_chain=NULL;
1655         for (i=0 ; i < ccw_pages_required; i++ ) {
1656                 real_address  = (__u32)__pa(p_buff);
1657                 p_buf=p_buff;
1658                 for (j=0 ; j < ccw_blocks_perpage ; j++) {
1659                         p_buf->next  = p_free_chain;
1660                         p_free_chain = p_buf;
1661                         p_buf->real=(__u32)__pa(p_buf);
1662                         ++p_buf;
1663                 }
1664                 p_buff+=PAGE_SIZE;
1665         }
1666         /*
1667         *                               Initialize ClawSignalBlock
1668         *
1669         */
1670         if (privptr->p_claw_signal_blk==NULL) {
1671                 privptr->p_claw_signal_blk=p_free_chain;
1672                 p_free_chain=p_free_chain->next;
1673                 pClawH=(struct clawh *)privptr->p_claw_signal_blk;
1674                 pClawH->length=0xffff;
1675                 pClawH->opcode=0xff;
1676                 pClawH->flag=CLAW_BUSY;
1677         }
1678
1679         /*
1680         *               allocate write_pages_required and add to free chain
1681         */
1682         if (privptr->p_buff_write==NULL) {
1683             if (privptr->p_env->write_size < PAGE_SIZE) {
1684                 privptr->p_buff_write=
1685                         (void *)__get_free_pages(__GFP_DMA,
1686                         (int)pages_to_order_of_mag(claw_write_pages ));
1687                 if (privptr->p_buff_write==NULL) {
1688                         privptr->p_buff_ccw=NULL;
1689                         return -ENOMEM;
1690                 }
1691                 /*
1692                 *                               Build CLAW write free chain
1693                 *
1694                 */
1695
1696                 memset(privptr->p_buff_write, 0x00,
1697                         ccw_pages_required * PAGE_SIZE);
1698                 privptr->p_write_free_chain=NULL;
1699
1700                 p_buff=privptr->p_buff_write;
1701
1702                 for (i=0 ; i< privptr->p_env->write_buffers ; i++) {
1703                         p_buf        = p_free_chain;      /*  get a CCW */
1704                         p_free_chain = p_buf->next;
1705                         p_buf->next  =privptr->p_write_free_chain;
1706                         privptr->p_write_free_chain = p_buf;
1707                         p_buf-> p_buffer        = (struct clawbuf *)p_buff;
1708                         p_buf-> write.cda       = (__u32)__pa(p_buff);
1709                         p_buf-> write.flags     = CCW_FLAG_SLI | CCW_FLAG_CC;
1710                         p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1711                         p_buf-> w_read_FF.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
1712                         p_buf-> w_read_FF.count   = 1;
1713                         p_buf-> w_read_FF.cda     =
1714                                 (__u32)__pa(&p_buf-> header.flag);
1715                         p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1716                         p_buf-> w_TIC_1.flags      = 0;
1717                         p_buf-> w_TIC_1.count      = 0;
1718
1719                         if (((unsigned long)p_buff +
1720                                             privptr->p_env->write_size) >=
1721                            ((unsigned long)(p_buff+2*
1722                             (privptr->p_env->write_size) - 1) & PAGE_MASK)) {
1723                                 p_buff = p_buff+privptr->p_env->write_size;
1724                         }
1725                 }
1726            }
1727            else      /*  Buffers are => PAGE_SIZE. 1 buff per get_free_pages */
1728            {
1729                privptr->p_write_free_chain=NULL;
1730                for (i = 0; i< privptr->p_env->write_buffers ; i++) {
1731                    p_buff=(void *)__get_free_pages(__GFP_DMA,
1732                         (int)pages_to_order_of_mag(
1733                         privptr->p_buff_pages_perwrite) );
1734                    if (p_buff==NULL) {
1735                         free_pages((unsigned long)privptr->p_buff_ccw,
1736                               (int)pages_to_order_of_mag(
1737                                         privptr->p_buff_ccw_num));
1738                         privptr->p_buff_ccw=NULL;
1739                         p_buf=privptr->p_buff_write;
1740                         while (p_buf!=NULL) {
1741                                 free_pages((unsigned long)
1742                                         p_buf->p_buffer,
1743                                         (int)pages_to_order_of_mag(
1744                                         privptr->p_buff_pages_perwrite));
1745                                 p_buf=p_buf->next;
1746                         }
1747                         return -ENOMEM;
1748                    }  /* Error on get_pages   */
1749                    memset(p_buff, 0x00, privptr->p_env->write_size );
1750                    p_buf         = p_free_chain;
1751                    p_free_chain  = p_buf->next;
1752                    p_buf->next   = privptr->p_write_free_chain;
1753                    privptr->p_write_free_chain = p_buf;
1754                    privptr->p_buff_write = p_buf;
1755                    p_buf->p_buffer=(struct clawbuf *)p_buff;
1756                    p_buf-> write.cda     = (__u32)__pa(p_buff);
1757                    p_buf-> write.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
1758                    p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1759                    p_buf-> w_read_FF.flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
1760                    p_buf-> w_read_FF.count    = 1;
1761                    p_buf-> w_read_FF.cda      =
1762                         (__u32)__pa(&p_buf-> header.flag);
1763                    p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1764                    p_buf-> w_TIC_1.flags   = 0;
1765                    p_buf-> w_TIC_1.count   = 0;
1766                }  /* for all write_buffers   */
1767
1768            }    /* else buffers are PAGE_SIZE or bigger */
1769
1770         }
1771         privptr->p_buff_write_num=claw_write_pages;
1772         privptr->write_free_count=privptr->p_env->write_buffers;
1773
1774
1775         /*
1776         *               allocate read_pages_required and chain to free chain
1777         */
1778         if (privptr->p_buff_read==NULL) {
1779             if (privptr->p_env->read_size < PAGE_SIZE)  {
1780                 privptr->p_buff_read=
1781                         (void *)__get_free_pages(__GFP_DMA,
1782                         (int)pages_to_order_of_mag(claw_read_pages) );
1783                 if (privptr->p_buff_read==NULL) {
1784                         free_pages((unsigned long)privptr->p_buff_ccw,
1785                                 (int)pages_to_order_of_mag(
1786                                         privptr->p_buff_ccw_num));
1787                         /* free the write pages size is < page size  */
1788                         free_pages((unsigned long)privptr->p_buff_write,
1789                                 (int)pages_to_order_of_mag(
1790                                 privptr->p_buff_write_num));
1791                         privptr->p_buff_ccw=NULL;
1792                         privptr->p_buff_write=NULL;
1793                         return -ENOMEM;
1794                 }
1795                 memset(privptr->p_buff_read, 0x00, claw_read_pages * PAGE_SIZE);
1796                 privptr->p_buff_read_num=claw_read_pages;
1797                 /*
1798                 *                               Build CLAW read free chain
1799                 *
1800                 */
1801                 p_buff=privptr->p_buff_read;
1802                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
1803                         p_buf        = p_free_chain;
1804                         p_free_chain = p_buf->next;
1805
1806                         if (p_last_CCWB==NULL) {
1807                                 p_buf->next=NULL;
1808                                 real_TIC_address=0;
1809                                 p_last_CCWB=p_buf;
1810                         }
1811                         else {
1812                                 p_buf->next=p_first_CCWB;
1813                                 real_TIC_address=
1814                                 (__u32)__pa(&p_first_CCWB -> read );
1815                         }
1816
1817                         p_first_CCWB=p_buf;
1818
1819                         p_buf->p_buffer=(struct clawbuf *)p_buff;
1820                         /*  initialize read command */
1821                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
1822                         p_buf-> read.cda = (__u32)__pa(p_buff);
1823                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1824                         p_buf-> read.count       = privptr->p_env->read_size;
1825
1826                         /*  initialize read_h command */
1827                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
1828                         p_buf-> read_h.cda =
1829                                 (__u32)__pa(&(p_buf->header));
1830                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1831                         p_buf-> read_h.count      = sizeof(struct clawh);
1832
1833                         /*  initialize Signal command */
1834                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
1835                         p_buf-> signal.cda =
1836                                 (__u32)__pa(&(pClawH->flag));
1837                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1838                         p_buf-> signal.count     = 1;
1839
1840                         /*  initialize r_TIC_1 command */
1841                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1842                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
1843                         p_buf-> r_TIC_1.flags = 0;
1844                         p_buf-> r_TIC_1.count      = 0;
1845
1846                         /*  initialize r_read_FF command */
1847                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1848                         p_buf-> r_read_FF.cda =
1849                                 (__u32)__pa(&(pClawH->flag));
1850                         p_buf-> r_read_FF.flags =
1851                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
1852                         p_buf-> r_read_FF.count    = 1;
1853
1854                         /*    initialize r_TIC_2          */
1855                         memcpy(&p_buf->r_TIC_2,
1856                                 &p_buf->r_TIC_1, sizeof(struct ccw1));
1857
1858                         /*     initialize Header     */
1859                         p_buf->header.length=0xffff;
1860                         p_buf->header.opcode=0xff;
1861                         p_buf->header.flag=CLAW_PENDING;
1862
1863                         if (((unsigned long)p_buff+privptr->p_env->read_size) >=
1864                           ((unsigned long)(p_buff+2*(privptr->p_env->read_size)
1865                                  -1)
1866                            & PAGE_MASK)) {
1867                                 p_buff= p_buff+privptr->p_env->read_size;
1868                         }
1869                         else {
1870                                 p_buff=
1871                                 (void *)((unsigned long)
1872                                         (p_buff+2*(privptr->p_env->read_size)-1)
1873                                          & PAGE_MASK) ;
1874                         }
1875                 }   /* for read_buffers   */
1876           }         /* read_size < PAGE_SIZE  */
1877           else {  /* read Size >= PAGE_SIZE  */
1878                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
1879                         p_buff = (void *)__get_free_pages(__GFP_DMA,
1880                                 (int)pages_to_order_of_mag(
1881                                         privptr->p_buff_pages_perread));
1882                         if (p_buff==NULL) {
1883                                 free_pages((unsigned long)privptr->p_buff_ccw,
1884                                         (int)pages_to_order_of_mag(privptr->
1885                                         p_buff_ccw_num));
1886                                 /* free the write pages  */
1887                                 p_buf=privptr->p_buff_write;
1888                                 while (p_buf!=NULL) {
1889                                         free_pages(
1890                                             (unsigned long)p_buf->p_buffer,
1891                                             (int)pages_to_order_of_mag(
1892                                             privptr->p_buff_pages_perwrite));
1893                                         p_buf=p_buf->next;
1894                                 }
1895                                 /* free any read pages already alloc  */
1896                                 p_buf=privptr->p_buff_read;
1897                                 while (p_buf!=NULL) {
1898                                         free_pages(
1899                                             (unsigned long)p_buf->p_buffer,
1900                                             (int)pages_to_order_of_mag(
1901                                              privptr->p_buff_pages_perread));
1902                                         p_buf=p_buf->next;
1903                                 }
1904                                 privptr->p_buff_ccw=NULL;
1905                                 privptr->p_buff_write=NULL;
1906                                 return -ENOMEM;
1907                         }
1908                         memset(p_buff, 0x00, privptr->p_env->read_size);
1909                         p_buf        = p_free_chain;
1910                         privptr->p_buff_read = p_buf;
1911                         p_free_chain = p_buf->next;
1912
1913                         if (p_last_CCWB==NULL) {
1914                                 p_buf->next=NULL;
1915                                 real_TIC_address=0;
1916                                 p_last_CCWB=p_buf;
1917                         }
1918                         else {
1919                                 p_buf->next=p_first_CCWB;
1920                                 real_TIC_address=
1921                                         (addr_t)__pa(
1922                                                 &p_first_CCWB -> read );
1923                         }
1924
1925                         p_first_CCWB=p_buf;
1926                                 /* save buff address */
1927                         p_buf->p_buffer=(struct clawbuf *)p_buff;
1928                         /*  initialize read command */
1929                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
1930                         p_buf-> read.cda = (__u32)__pa(p_buff);
1931                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1932                         p_buf-> read.count       = privptr->p_env->read_size;
1933
1934                         /*  initialize read_h command */
1935                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
1936                         p_buf-> read_h.cda =
1937                                 (__u32)__pa(&(p_buf->header));
1938                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1939                         p_buf-> read_h.count      = sizeof(struct clawh);
1940
1941                         /*  initialize Signal command */
1942                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
1943                         p_buf-> signal.cda =
1944                                 (__u32)__pa(&(pClawH->flag));
1945                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1946                         p_buf-> signal.count     = 1;
1947
1948                         /*  initialize r_TIC_1 command */
1949                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1950                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
1951                         p_buf-> r_TIC_1.flags = 0;
1952                         p_buf-> r_TIC_1.count      = 0;
1953
1954                         /*  initialize r_read_FF command */
1955                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1956                         p_buf-> r_read_FF.cda =
1957                                 (__u32)__pa(&(pClawH->flag));
1958                         p_buf-> r_read_FF.flags =
1959                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
1960                         p_buf-> r_read_FF.count    = 1;
1961
1962                         /*    initialize r_TIC_2          */
1963                         memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1,
1964                                 sizeof(struct ccw1));
1965
1966                         /*     initialize Header     */
1967                         p_buf->header.length=0xffff;
1968                         p_buf->header.opcode=0xff;
1969                         p_buf->header.flag=CLAW_PENDING;
1970
1971                 }    /* For read_buffers   */
1972           }     /*  read_size >= PAGE_SIZE   */
1973         }       /*  pBuffread = NULL */
1974         add_claw_reads( dev  ,p_first_CCWB , p_last_CCWB);
1975         privptr->buffs_alloc = 1;
1976
1977         return 0;
1978 }    /*    end of init_ccw_bk */
1979
1980 /*-------------------------------------------------------------------*
1981 *                                                                    *
1982 *       probe_error                                                  *
1983 *                                                                    *
1984 *--------------------------------------------------------------------*/
1985
1986 static void
1987 probe_error( struct ccwgroup_device *cgdev)
1988 {
1989         struct claw_privbk *privptr;
1990
1991         CLAW_DBF_TEXT(4, trace, "proberr");
1992         privptr = (struct claw_privbk *) cgdev->dev.driver_data;
1993         if (privptr != NULL) {
1994                 cgdev->dev.driver_data = NULL;
1995                 kfree(privptr->p_env);
1996                 kfree(privptr->p_mtc_envelope);
1997                 kfree(privptr);
1998         }
1999 }    /*    probe_error    */
2000
2001 /*-------------------------------------------------------------------*
2002 *    claw_process_control                                            *
2003 *                                                                    *
2004 *                                                                    *
2005 *--------------------------------------------------------------------*/
2006
2007 static int
2008 claw_process_control( struct net_device *dev, struct ccwbk * p_ccw)
2009 {
2010
2011         struct clawbuf *p_buf;
2012         struct clawctl  ctlbk;
2013         struct clawctl *p_ctlbk;
2014         char    temp_host_name[8];
2015         char    temp_ws_name[8];
2016         struct claw_privbk *privptr;
2017         struct claw_env *p_env;
2018         struct sysval *p_sysval;
2019         struct conncmd *p_connect=NULL;
2020         int rc;
2021         struct chbk *p_ch = NULL;
2022         struct device *tdev;
2023         CLAW_DBF_TEXT(2, setup, "clw_cntl");
2024         udelay(1000);  /* Wait a ms for the control packets to
2025                         *catch up to each other */
2026         privptr = dev->ml_priv;
2027         p_env=privptr->p_env;
2028         tdev = &privptr->channel[READ].cdev->dev;
2029         memcpy( &temp_host_name, p_env->host_name, 8);
2030         memcpy( &temp_ws_name, p_env->adapter_name , 8);
2031         dev_info(tdev, "%s: CLAW device %.8s: "
2032                 "Received Control Packet\n",
2033                 dev->name, temp_ws_name);
2034         if (privptr->release_pend==1) {
2035                 return 0;
2036         }
2037         p_buf=p_ccw->p_buffer;
2038         p_ctlbk=&ctlbk;
2039         if (p_env->packing == DO_PACKED) { /* packing in progress?*/
2040                 memcpy(p_ctlbk, &p_buf->buffer[4], sizeof(struct clawctl));
2041         } else {
2042                 memcpy(p_ctlbk, p_buf, sizeof(struct clawctl));
2043         }
2044         switch (p_ctlbk->command)
2045         {
2046         case SYSTEM_VALIDATE_REQUEST:
2047                 if (p_ctlbk->version != CLAW_VERSION_ID) {
2048                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2049                                 CLAW_RC_WRONG_VERSION);
2050                         dev_warn(tdev, "The communication peer of %s"
2051                                 " uses an incorrect API version %d\n",
2052                                 dev->name, p_ctlbk->version);
2053                 }
2054                 p_sysval = (struct sysval *)&(p_ctlbk->data);
2055                 dev_info(tdev, "%s: Recv Sys Validate Request: "
2056                         "Vers=%d,link_id=%d,Corr=%d,WS name=%.8s,"
2057                         "Host name=%.8s\n",
2058                         dev->name, p_ctlbk->version,
2059                         p_ctlbk->linkid,
2060                         p_ctlbk->correlator,
2061                         p_sysval->WS_name,
2062                         p_sysval->host_name);
2063                 if (memcmp(temp_host_name, p_sysval->host_name, 8)) {
2064                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2065                                 CLAW_RC_NAME_MISMATCH);
2066                         CLAW_DBF_TEXT(2, setup, "HSTBAD");
2067                         CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->host_name);
2068                         CLAW_DBF_TEXT_(2, setup, "%s", temp_host_name);
2069                         dev_warn(tdev,
2070                                 "Host name %s for %s does not match the"
2071                                 " remote adapter name %s\n",
2072                                 p_sysval->host_name,
2073                                 dev->name,
2074                                 temp_host_name);
2075                 }
2076                 if (memcmp(temp_ws_name, p_sysval->WS_name, 8)) {
2077                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2078                                 CLAW_RC_NAME_MISMATCH);
2079                         CLAW_DBF_TEXT(2, setup, "WSNBAD");
2080                         CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->WS_name);
2081                         CLAW_DBF_TEXT_(2, setup, "%s", temp_ws_name);
2082                         dev_warn(tdev, "Adapter name %s for %s does not match"
2083                                 " the remote host name %s\n",
2084                                 p_sysval->WS_name,
2085                                 dev->name,
2086                                 temp_ws_name);
2087                 }
2088                 if ((p_sysval->write_frame_size < p_env->write_size) &&
2089                     (p_env->packing == 0)) {
2090                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2091                                 CLAW_RC_HOST_RCV_TOO_SMALL);
2092                         dev_warn(tdev,
2093                                 "The local write buffer is smaller than the"
2094                                 " remote read buffer\n");
2095                         CLAW_DBF_TEXT(2, setup, "wrtszbad");
2096                 }
2097                 if ((p_sysval->read_frame_size < p_env->read_size) &&
2098                     (p_env->packing == 0)) {
2099                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2100                                 CLAW_RC_HOST_RCV_TOO_SMALL);
2101                         dev_warn(tdev,
2102                                 "The local read buffer is smaller than the"
2103                                 " remote write buffer\n");
2104                         CLAW_DBF_TEXT(2, setup, "rdsizbad");
2105                 }
2106                 claw_snd_sys_validate_rsp(dev, p_ctlbk, 0);
2107                 dev_info(tdev,
2108                         "CLAW device %.8s: System validate"
2109                         " completed.\n", temp_ws_name);
2110                 dev_info(tdev,
2111                         "%s: sys Validate Rsize:%d Wsize:%d\n",
2112                         dev->name, p_sysval->read_frame_size,
2113                         p_sysval->write_frame_size);
2114                 privptr->system_validate_comp = 1;
2115                 if (strncmp(p_env->api_type, WS_APPL_NAME_PACKED, 6) == 0)
2116                         p_env->packing = PACKING_ASK;
2117                 claw_strt_conn_req(dev);
2118                 break;
2119         case SYSTEM_VALIDATE_RESPONSE:
2120                 p_sysval = (struct sysval *)&(p_ctlbk->data);
2121                 dev_info(tdev,
2122                         "Settings for %s validated (version=%d, "
2123                         "remote device=%d, rc=%d, adapter name=%.8s, "
2124                         "host name=%.8s)\n",
2125                         dev->name,
2126                         p_ctlbk->version,
2127                         p_ctlbk->correlator,
2128                         p_ctlbk->rc,
2129                         p_sysval->WS_name,
2130                         p_sysval->host_name);
2131                 switch (p_ctlbk->rc) {
2132                 case 0:
2133                         dev_info(tdev, "%s: CLAW device "
2134                                 "%.8s: System validate completed.\n",
2135                                 dev->name, temp_ws_name);
2136                         if (privptr->system_validate_comp == 0)
2137                                 claw_strt_conn_req(dev);
2138                         privptr->system_validate_comp = 1;
2139                         break;
2140                 case CLAW_RC_NAME_MISMATCH:
2141                         dev_warn(tdev, "Validating %s failed because of"
2142                                 " a host or adapter name mismatch\n",
2143                                 dev->name);
2144                         break;
2145                 case CLAW_RC_WRONG_VERSION:
2146                         dev_warn(tdev, "Validating %s failed because of a"
2147                                 " version conflict\n",
2148                                 dev->name);
2149                         break;
2150                 case CLAW_RC_HOST_RCV_TOO_SMALL:
2151                         dev_warn(tdev, "Validating %s failed because of a"
2152                                 " frame size conflict\n",
2153                                 dev->name);
2154                         break;
2155                 default:
2156                         dev_warn(tdev, "The communication peer of %s rejected"
2157                                 " the connection\n",
2158                                  dev->name);
2159                         break;
2160                 }
2161                 break;
2162
2163         case CONNECTION_REQUEST:
2164                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2165                 dev_info(tdev, "%s: Recv Conn Req: Vers=%d,link_id=%d,"
2166                         "Corr=%d,HOST appl=%.8s,WS appl=%.8s\n",
2167                         dev->name,
2168                         p_ctlbk->version,
2169                         p_ctlbk->linkid,
2170                         p_ctlbk->correlator,
2171                         p_connect->host_name,
2172                         p_connect->WS_name);
2173                 if (privptr->active_link_ID != 0) {
2174                         claw_snd_disc(dev, p_ctlbk);
2175                         dev_info(tdev, "%s rejected a connection request"
2176                                 " because it is already active\n",
2177                                 dev->name);
2178                 }
2179                 if (p_ctlbk->linkid != 1) {
2180                         claw_snd_disc(dev, p_ctlbk);
2181                         dev_info(tdev, "%s rejected a request to open multiple"
2182                                 " connections\n",
2183                                 dev->name);
2184                 }
2185                 rc = find_link(dev, p_connect->host_name, p_connect->WS_name);
2186                 if (rc != 0) {
2187                         claw_snd_disc(dev, p_ctlbk);
2188                         dev_info(tdev, "%s rejected a connection request"
2189                                 " because of a type mismatch\n",
2190                                 dev->name);
2191                 }
2192                 claw_send_control(dev,
2193                         CONNECTION_CONFIRM, p_ctlbk->linkid,
2194                         p_ctlbk->correlator,
2195                         0, p_connect->host_name,
2196                         p_connect->WS_name);
2197                 if (p_env->packing == PACKING_ASK) {
2198                         p_env->packing = PACK_SEND;
2199                         claw_snd_conn_req(dev, 0);
2200                 }
2201                 dev_info(tdev, "%s: CLAW device %.8s: Connection "
2202                         "completed link_id=%d.\n",
2203                         dev->name, temp_ws_name,
2204                         p_ctlbk->linkid);
2205                         privptr->active_link_ID = p_ctlbk->linkid;
2206                         p_ch = &privptr->channel[WRITE];
2207                         wake_up(&p_ch->wait);  /* wake up claw_open ( WRITE) */
2208                 break;
2209         case CONNECTION_RESPONSE:
2210                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2211                 dev_info(tdev, "%s: Recv Conn Resp: Vers=%d,link_id=%d,"
2212                         "Corr=%d,RC=%d,Host appl=%.8s, WS appl=%.8s\n",
2213                         dev->name,
2214                         p_ctlbk->version,
2215                         p_ctlbk->linkid,
2216                         p_ctlbk->correlator,
2217                         p_ctlbk->rc,
2218                         p_connect->host_name,
2219                         p_connect->WS_name);
2220
2221                 if (p_ctlbk->rc != 0) {
2222                         dev_warn(tdev, "The communication peer of %s rejected"
2223                                 " a connection request\n",
2224                                 dev->name);
2225                         return 1;
2226                 }
2227                 rc = find_link(dev,
2228                         p_connect->host_name, p_connect->WS_name);
2229                 if (rc != 0) {
2230                         claw_snd_disc(dev, p_ctlbk);
2231                         dev_warn(tdev, "The communication peer of %s"
2232                                 " rejected a connection "
2233                                 "request because of a type mismatch\n",
2234                                  dev->name);
2235                 }
2236                 /* should be until CONNECTION_CONFIRM */
2237                 privptr->active_link_ID = -(p_ctlbk->linkid);
2238                 break;
2239         case CONNECTION_CONFIRM:
2240                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2241                 dev_info(tdev,
2242                         "%s: Recv Conn Confirm:Vers=%d,link_id=%d,"
2243                         "Corr=%d,Host appl=%.8s,WS appl=%.8s\n",
2244                         dev->name,
2245                         p_ctlbk->version,
2246                         p_ctlbk->linkid,
2247                         p_ctlbk->correlator,
2248                         p_connect->host_name,
2249                         p_connect->WS_name);
2250                 if (p_ctlbk->linkid == -(privptr->active_link_ID)) {
2251                         privptr->active_link_ID = p_ctlbk->linkid;
2252                         if (p_env->packing > PACKING_ASK) {
2253                                 dev_info(tdev,
2254                                 "%s: Confirmed Now packing\n", dev->name);
2255                                 p_env->packing = DO_PACKED;
2256                         }
2257                         p_ch = &privptr->channel[WRITE];
2258                         wake_up(&p_ch->wait);
2259                 } else {
2260                         dev_warn(tdev, "Activating %s failed because of"
2261                                 " an incorrect link ID=%d\n",
2262                                 dev->name, p_ctlbk->linkid);
2263                         claw_snd_disc(dev, p_ctlbk);
2264                 }
2265                 break;
2266         case DISCONNECT:
2267                 dev_info(tdev, "%s: Disconnect: "
2268                         "Vers=%d,link_id=%d,Corr=%d\n",
2269                         dev->name, p_ctlbk->version,
2270                         p_ctlbk->linkid, p_ctlbk->correlator);
2271                 if ((p_ctlbk->linkid == 2) &&
2272                     (p_env->packing == PACK_SEND)) {
2273                         privptr->active_link_ID = 1;
2274                         p_env->packing = DO_PACKED;
2275                 } else
2276                         privptr->active_link_ID = 0;
2277                 break;
2278         case CLAW_ERROR:
2279                 dev_warn(tdev, "The communication peer of %s failed\n",
2280                         dev->name);
2281                 break;
2282         default:
2283                 dev_warn(tdev, "The communication peer of %s sent"
2284                         " an unknown command code\n",
2285                         dev->name);
2286                 break;
2287         }
2288
2289         return 0;
2290 }   /*    end of claw_process_control    */
2291
2292
2293 /*-------------------------------------------------------------------*
2294 *               claw_send_control                                    *
2295 *                                                                    *
2296 *--------------------------------------------------------------------*/
2297
2298 static int
2299 claw_send_control(struct net_device *dev, __u8 type, __u8 link,
2300          __u8 correlator, __u8 rc, char *local_name, char *remote_name)
2301 {
2302         struct claw_privbk              *privptr;
2303         struct clawctl                  *p_ctl;
2304         struct sysval                   *p_sysval;
2305         struct conncmd                  *p_connect;
2306         struct sk_buff                  *skb;
2307
2308         CLAW_DBF_TEXT(2, setup, "sndcntl");
2309         privptr = dev->ml_priv;
2310         p_ctl=(struct clawctl *)&privptr->ctl_bk;
2311
2312         p_ctl->command=type;
2313         p_ctl->version=CLAW_VERSION_ID;
2314         p_ctl->linkid=link;
2315         p_ctl->correlator=correlator;
2316         p_ctl->rc=rc;
2317
2318         p_sysval=(struct sysval *)&p_ctl->data;
2319         p_connect=(struct conncmd *)&p_ctl->data;
2320
2321         switch (p_ctl->command) {
2322                 case SYSTEM_VALIDATE_REQUEST:
2323                 case SYSTEM_VALIDATE_RESPONSE:
2324                         memcpy(&p_sysval->host_name, local_name, 8);
2325                         memcpy(&p_sysval->WS_name, remote_name, 8);
2326                         if (privptr->p_env->packing > 0) {
2327                                 p_sysval->read_frame_size = DEF_PACK_BUFSIZE;
2328                                 p_sysval->write_frame_size = DEF_PACK_BUFSIZE;
2329                         } else {
2330                                 /* how big is the biggest group of packets */
2331                            p_sysval->read_frame_size =
2332                                 privptr->p_env->read_size;
2333                            p_sysval->write_frame_size =
2334                                 privptr->p_env->write_size;
2335                         }
2336                         memset(&p_sysval->reserved, 0x00, 4);
2337                         break;
2338                 case CONNECTION_REQUEST:
2339                 case CONNECTION_RESPONSE:
2340                 case CONNECTION_CONFIRM:
2341                 case DISCONNECT:
2342                         memcpy(&p_sysval->host_name, local_name, 8);
2343                         memcpy(&p_sysval->WS_name, remote_name, 8);
2344                         if (privptr->p_env->packing > 0) {
2345                         /* How big is the biggest packet */
2346                                 p_connect->reserved1[0]=CLAW_FRAME_SIZE;
2347                                 p_connect->reserved1[1]=CLAW_FRAME_SIZE;
2348                         } else {
2349                                 memset(&p_connect->reserved1, 0x00, 4);
2350                                 memset(&p_connect->reserved2, 0x00, 4);
2351                         }
2352                         break;
2353                 default:
2354                         break;
2355         }
2356
2357         /*      write Control Record to the device                   */
2358
2359
2360         skb = dev_alloc_skb(sizeof(struct clawctl));
2361         if (!skb) {
2362                 return -ENOMEM;
2363         }
2364         memcpy(skb_put(skb, sizeof(struct clawctl)),
2365                 p_ctl, sizeof(struct clawctl));
2366         if (privptr->p_env->packing >= PACK_SEND)
2367                 claw_hw_tx(skb, dev, 1);
2368         else
2369                 claw_hw_tx(skb, dev, 0);
2370         return 0;
2371 }  /*   end of claw_send_control  */
2372
2373 /*-------------------------------------------------------------------*
2374 *               claw_snd_conn_req                                    *
2375 *                                                                    *
2376 *--------------------------------------------------------------------*/
2377 static int
2378 claw_snd_conn_req(struct net_device *dev, __u8 link)
2379 {
2380         int                rc;
2381         struct claw_privbk *privptr = dev->ml_priv;
2382         struct clawctl     *p_ctl;
2383
2384         CLAW_DBF_TEXT(2, setup, "snd_conn");
2385         rc = 1;
2386         p_ctl=(struct clawctl *)&privptr->ctl_bk;
2387         p_ctl->linkid = link;
2388         if ( privptr->system_validate_comp==0x00 ) {
2389                 return rc;
2390         }
2391         if (privptr->p_env->packing == PACKING_ASK )
2392                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2393                         WS_APPL_NAME_PACKED, WS_APPL_NAME_PACKED);
2394         if (privptr->p_env->packing == PACK_SEND)  {
2395                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2396                         WS_APPL_NAME_IP_NAME, WS_APPL_NAME_IP_NAME);
2397         }
2398         if (privptr->p_env->packing == 0)
2399                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2400                         HOST_APPL_NAME, privptr->p_env->api_type);
2401         return rc;
2402
2403 }  /*  end of claw_snd_conn_req */
2404
2405
2406 /*-------------------------------------------------------------------*
2407 *               claw_snd_disc                                        *
2408 *                                                                    *
2409 *--------------------------------------------------------------------*/
2410
2411 static int
2412 claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl)
2413 {
2414         int rc;
2415         struct conncmd *  p_connect;
2416
2417         CLAW_DBF_TEXT(2, setup, "snd_dsc");
2418         p_connect=(struct conncmd *)&p_ctl->data;
2419
2420         rc=claw_send_control(dev, DISCONNECT, p_ctl->linkid,
2421                 p_ctl->correlator, 0,
2422                 p_connect->host_name, p_connect->WS_name);
2423         return rc;
2424 }     /*   end of claw_snd_disc    */
2425
2426
2427 /*-------------------------------------------------------------------*
2428 *               claw_snd_sys_validate_rsp                            *
2429 *                                                                    *
2430 *--------------------------------------------------------------------*/
2431
2432 static int
2433 claw_snd_sys_validate_rsp(struct net_device *dev,
2434         struct clawctl *p_ctl, __u32 return_code)
2435 {
2436         struct claw_env *  p_env;
2437         struct claw_privbk *privptr;
2438         int    rc;
2439
2440         CLAW_DBF_TEXT(2, setup, "chkresp");
2441         privptr = dev->ml_priv;
2442         p_env=privptr->p_env;
2443         rc=claw_send_control(dev, SYSTEM_VALIDATE_RESPONSE,
2444                 p_ctl->linkid,
2445                 p_ctl->correlator,
2446                 return_code,
2447                 p_env->host_name,
2448                 p_env->adapter_name  );
2449         return rc;
2450 }     /*    end of claw_snd_sys_validate_rsp    */
2451
2452 /*-------------------------------------------------------------------*
2453 *               claw_strt_conn_req                                   *
2454 *                                                                    *
2455 *--------------------------------------------------------------------*/
2456
2457 static int
2458 claw_strt_conn_req(struct net_device *dev )
2459 {
2460         int rc;
2461
2462         CLAW_DBF_TEXT(2, setup, "conn_req");
2463         rc=claw_snd_conn_req(dev, 1);
2464         return rc;
2465 }    /*   end of claw_strt_conn_req   */
2466
2467
2468
2469 /*-------------------------------------------------------------------*
2470  *   claw_stats                                                      *
2471  *-------------------------------------------------------------------*/
2472
2473 static struct
2474 net_device_stats *claw_stats(struct net_device *dev)
2475 {
2476         struct claw_privbk *privptr;
2477
2478         CLAW_DBF_TEXT(4, trace, "stats");
2479         privptr = dev->ml_priv;
2480         return &privptr->stats;
2481 }     /*   end of claw_stats   */
2482
2483
2484 /*-------------------------------------------------------------------*
2485 *       unpack_read                                                  *
2486 *                                                                    *
2487 *--------------------------------------------------------------------*/
2488 static void
2489 unpack_read(struct net_device *dev )
2490 {
2491         struct sk_buff *skb;
2492         struct claw_privbk *privptr;
2493         struct claw_env    *p_env;
2494         struct ccwbk    *p_this_ccw;
2495         struct ccwbk    *p_first_ccw;
2496         struct ccwbk    *p_last_ccw;
2497         struct clawph   *p_packh;
2498         void            *p_packd;
2499         struct clawctl  *p_ctlrec=NULL;
2500         struct device   *p_dev;
2501
2502         __u32   len_of_data;
2503         __u32   pack_off;
2504         __u8    link_num;
2505         __u8    mtc_this_frm=0;
2506         __u32   bytes_to_mov;
2507         int     i=0;
2508         int     p=0;
2509
2510         CLAW_DBF_TEXT(4, trace, "unpkread");
2511         p_first_ccw=NULL;
2512         p_last_ccw=NULL;
2513         p_packh=NULL;
2514         p_packd=NULL;
2515         privptr = dev->ml_priv;
2516
2517         p_dev = &privptr->channel[READ].cdev->dev;
2518         p_env = privptr->p_env;
2519         p_this_ccw=privptr->p_read_active_first;
2520         i=0;
2521         while (p_this_ccw!=NULL && p_this_ccw->header.flag!=CLAW_PENDING) {
2522                 pack_off = 0;
2523                 p = 0;
2524                 p_this_ccw->header.flag=CLAW_PENDING;
2525                 privptr->p_read_active_first=p_this_ccw->next;
2526                 p_this_ccw->next=NULL;
2527                 p_packh = (struct clawph *)p_this_ccw->p_buffer;
2528                 if ((p_env->packing == PACK_SEND) &&
2529                     (p_packh->len == 32)           &&
2530                     (p_packh->link_num == 0)) {   /* is it a packed ctl rec? */
2531                         p_packh++;  /* peek past pack header */
2532                         p_ctlrec = (struct clawctl *)p_packh;
2533                         p_packh--;  /* un peek */
2534                         if ((p_ctlrec->command == CONNECTION_RESPONSE) ||
2535                             (p_ctlrec->command == CONNECTION_CONFIRM))
2536                                 p_env->packing = DO_PACKED;
2537                 }
2538                 if (p_env->packing == DO_PACKED)
2539                         link_num=p_packh->link_num;
2540                 else
2541                         link_num=p_this_ccw->header.opcode / 8;
2542                 if ((p_this_ccw->header.opcode & MORE_to_COME_FLAG)!=0) {
2543                         mtc_this_frm=1;
2544                         if (p_this_ccw->header.length!=
2545                                 privptr->p_env->read_size ) {
2546                                 dev_warn(p_dev,
2547                                         "The communication peer of %s"
2548                                         " sent a faulty"
2549                                         " frame of length %02x\n",
2550                                         dev->name, p_this_ccw->header.length);
2551                         }
2552                 }
2553
2554                 if (privptr->mtc_skipping) {
2555                         /*
2556                         *   We're in the mode of skipping past a
2557                         *   multi-frame message
2558                         *   that we can't process for some reason or other.
2559                         *   The first frame without the More-To-Come flag is
2560                         *   the last frame of the skipped message.
2561                         */
2562                         /*  in case of More-To-Come not set in this frame */
2563                         if (mtc_this_frm==0) {
2564                                 privptr->mtc_skipping=0; /* Ok, the end */
2565                                 privptr->mtc_logical_link=-1;
2566                         }
2567                         goto NextFrame;
2568                 }
2569
2570                 if (link_num==0) {
2571                         claw_process_control(dev, p_this_ccw);
2572                         CLAW_DBF_TEXT(4, trace, "UnpkCntl");
2573                         goto NextFrame;
2574                 }
2575 unpack_next:
2576                 if (p_env->packing == DO_PACKED) {
2577                         if (pack_off > p_env->read_size)
2578                                 goto NextFrame;
2579                         p_packd = p_this_ccw->p_buffer+pack_off;
2580                         p_packh = (struct clawph *) p_packd;
2581                         if ((p_packh->len == 0) || /* done with this frame? */
2582                             (p_packh->flag != 0))
2583                                 goto NextFrame;
2584                         bytes_to_mov = p_packh->len;
2585                         pack_off += bytes_to_mov+sizeof(struct clawph);
2586                         p++;
2587                 } else {
2588                         bytes_to_mov=p_this_ccw->header.length;
2589                 }
2590                 if (privptr->mtc_logical_link<0) {
2591
2592                 /*
2593                 *  if More-To-Come is set in this frame then we don't know
2594                 *  length of entire message, and hence have to allocate
2595                 *  large buffer   */
2596
2597                 /*      We are starting a new envelope  */
2598                 privptr->mtc_offset=0;
2599                         privptr->mtc_logical_link=link_num;
2600                 }
2601
2602                 if (bytes_to_mov > (MAX_ENVELOPE_SIZE- privptr->mtc_offset) ) {
2603                         /*      error     */
2604                         privptr->stats.rx_frame_errors++;
2605                         goto NextFrame;
2606                 }
2607                 if (p_env->packing == DO_PACKED) {
2608                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
2609                                 p_packd+sizeof(struct clawph), bytes_to_mov);
2610
2611                 } else  {
2612                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
2613                                 p_this_ccw->p_buffer, bytes_to_mov);
2614                 }
2615                 if (mtc_this_frm==0) {
2616                         len_of_data=privptr->mtc_offset+bytes_to_mov;
2617                         skb=dev_alloc_skb(len_of_data);
2618                         if (skb) {
2619                                 memcpy(skb_put(skb,len_of_data),
2620                                         privptr->p_mtc_envelope,
2621                                         len_of_data);
2622                                 skb->dev=dev;
2623                                 skb_reset_mac_header(skb);
2624                                 skb->protocol=htons(ETH_P_IP);
2625                                 skb->ip_summed=CHECKSUM_UNNECESSARY;
2626                                 privptr->stats.rx_packets++;
2627                                 privptr->stats.rx_bytes+=len_of_data;
2628                                 netif_rx(skb);
2629                         }
2630                         else {
2631                                 dev_info(p_dev, "Allocating a buffer for"
2632                                         " incoming data failed\n");
2633                                 privptr->stats.rx_dropped++;
2634                         }
2635                         privptr->mtc_offset=0;
2636                         privptr->mtc_logical_link=-1;
2637                 }
2638                 else {
2639                         privptr->mtc_offset+=bytes_to_mov;
2640                 }
2641                 if (p_env->packing == DO_PACKED)
2642                         goto unpack_next;
2643 NextFrame:
2644                 /*
2645                 *   Remove ThisCCWblock from active read queue, and add it
2646                 *   to queue of free blocks to be reused.
2647                 */
2648                 i++;
2649                 p_this_ccw->header.length=0xffff;
2650                 p_this_ccw->header.opcode=0xff;
2651                 /*
2652                 *       add this one to the free queue for later reuse
2653                 */
2654                 if (p_first_ccw==NULL) {
2655                         p_first_ccw = p_this_ccw;
2656                 }
2657                 else {
2658                         p_last_ccw->next = p_this_ccw;
2659                 }
2660                 p_last_ccw = p_this_ccw;
2661                 /*
2662                 *       chain to next block on active read queue
2663                 */
2664                 p_this_ccw = privptr->p_read_active_first;
2665                 CLAW_DBF_TEXT_(4, trace, "rxpkt %d", p);
2666         } /* end of while */
2667
2668         /*      check validity                  */
2669
2670         CLAW_DBF_TEXT_(4, trace, "rxfrm %d", i);
2671         add_claw_reads(dev, p_first_ccw, p_last_ccw);
2672         claw_strt_read(dev, LOCK_YES);
2673         return;
2674 }     /*  end of unpack_read   */
2675
2676 /*-------------------------------------------------------------------*
2677 *       claw_strt_read                                               *
2678 *                                                                    *
2679 *--------------------------------------------------------------------*/
2680 static void
2681 claw_strt_read (struct net_device *dev, int lock )
2682 {
2683         int        rc = 0;
2684         __u32      parm;
2685         unsigned long  saveflags = 0;
2686         struct claw_privbk *privptr = dev->ml_priv;
2687         struct ccwbk*p_ccwbk;
2688         struct chbk *p_ch;
2689         struct clawh *p_clawh;
2690         p_ch=&privptr->channel[READ];
2691
2692         CLAW_DBF_TEXT(4, trace, "StRdNter");
2693         p_clawh=(struct clawh *)privptr->p_claw_signal_blk;
2694         p_clawh->flag=CLAW_IDLE;    /* 0x00 */
2695
2696         if ((privptr->p_write_active_first!=NULL &&
2697              privptr->p_write_active_first->header.flag!=CLAW_PENDING) ||
2698             (privptr->p_read_active_first!=NULL &&
2699              privptr->p_read_active_first->header.flag!=CLAW_PENDING )) {
2700                 p_clawh->flag=CLAW_BUSY;    /* 0xff */
2701         }
2702         if (lock==LOCK_YES) {
2703                 spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
2704         }
2705         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
2706                 CLAW_DBF_TEXT(4, trace, "HotRead");
2707                 p_ccwbk=privptr->p_read_active_first;
2708                 parm = (unsigned long) p_ch;
2709                 rc = ccw_device_start (p_ch->cdev, &p_ccwbk->read, parm,
2710                                        0xff, 0);
2711                 if (rc != 0) {
2712                         ccw_check_return_code(p_ch->cdev, rc);
2713                 }
2714         }
2715         else {
2716                 CLAW_DBF_TEXT(2, trace, "ReadAct");
2717         }
2718
2719         if (lock==LOCK_YES) {
2720                 spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
2721         }
2722         CLAW_DBF_TEXT(4, trace, "StRdExit");
2723         return;
2724 }       /*    end of claw_strt_read    */
2725
2726 /*-------------------------------------------------------------------*
2727 *       claw_strt_out_IO                                             *
2728 *                                                                    *
2729 *--------------------------------------------------------------------*/
2730
2731 static void
2732 claw_strt_out_IO( struct net_device *dev )
2733 {
2734         int                     rc = 0;
2735         unsigned long           parm;
2736         struct claw_privbk      *privptr;
2737         struct chbk             *p_ch;
2738         struct ccwbk    *p_first_ccw;
2739
2740         if (!dev) {
2741                 return;
2742         }
2743         privptr = (struct claw_privbk *)dev->ml_priv;
2744         p_ch=&privptr->channel[WRITE];
2745
2746         CLAW_DBF_TEXT(4, trace, "strt_io");
2747         p_first_ccw=privptr->p_write_active_first;
2748
2749         if (p_ch->claw_state == CLAW_STOP)
2750                 return;
2751         if (p_first_ccw == NULL) {
2752                 return;
2753         }
2754         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
2755                 parm = (unsigned long) p_ch;
2756                 CLAW_DBF_TEXT(2, trace, "StWrtIO");
2757                 rc = ccw_device_start(p_ch->cdev, &p_first_ccw->write, parm,
2758                                       0xff, 0);
2759                 if (rc != 0) {
2760                         ccw_check_return_code(p_ch->cdev, rc);
2761                 }
2762         }
2763         dev->trans_start = jiffies;
2764         return;
2765 }       /*    end of claw_strt_out_IO    */
2766
2767 /*-------------------------------------------------------------------*
2768 *       Free write buffers                                           *
2769 *                                                                    *
2770 *--------------------------------------------------------------------*/
2771
2772 static void
2773 claw_free_wrt_buf( struct net_device *dev )
2774 {
2775
2776         struct claw_privbk *privptr = (struct claw_privbk *)dev->ml_priv;
2777         struct ccwbk*p_first_ccw;
2778         struct ccwbk*p_last_ccw;
2779         struct ccwbk*p_this_ccw;
2780         struct ccwbk*p_next_ccw;
2781
2782         CLAW_DBF_TEXT(4, trace, "freewrtb");
2783         /*  scan the write queue to free any completed write packets   */
2784         p_first_ccw=NULL;
2785         p_last_ccw=NULL;
2786         p_this_ccw=privptr->p_write_active_first;
2787         while ( (p_this_ccw!=NULL) && (p_this_ccw->header.flag!=CLAW_PENDING))
2788         {
2789                 p_next_ccw = p_this_ccw->next;
2790                 if (((p_next_ccw!=NULL) &&
2791                      (p_next_ccw->header.flag!=CLAW_PENDING)) ||
2792                     ((p_this_ccw == privptr->p_write_active_last) &&
2793                      (p_this_ccw->header.flag!=CLAW_PENDING))) {
2794                         /* The next CCW is OK or this is  */
2795                         /* the last CCW...free it   @A1A  */
2796                         privptr->p_write_active_first=p_this_ccw->next;
2797                         p_this_ccw->header.flag=CLAW_PENDING;
2798                         p_this_ccw->next=privptr->p_write_free_chain;
2799                         privptr->p_write_free_chain=p_this_ccw;
2800                         ++privptr->write_free_count;
2801                         privptr->stats.tx_bytes+= p_this_ccw->write.count;
2802                         p_this_ccw=privptr->p_write_active_first;
2803                         privptr->stats.tx_packets++;
2804                 }
2805                 else {
2806                         break;
2807                 }
2808         }
2809         if (privptr->write_free_count!=0) {
2810                 claw_clearbit_busy(TB_NOBUFFER,dev);
2811         }
2812         /*   whole chain removed?   */
2813         if (privptr->p_write_active_first==NULL) {
2814                 privptr->p_write_active_last=NULL;
2815         }
2816         CLAW_DBF_TEXT_(4, trace, "FWC=%d", privptr->write_free_count);
2817         return;
2818 }
2819
2820 /*-------------------------------------------------------------------*
2821 *       claw free netdevice                                          *
2822 *                                                                    *
2823 *--------------------------------------------------------------------*/
2824 static void
2825 claw_free_netdevice(struct net_device * dev, int free_dev)
2826 {
2827         struct claw_privbk *privptr;
2828
2829         CLAW_DBF_TEXT(2, setup, "free_dev");
2830         if (!dev)
2831                 return;
2832         CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
2833         privptr = dev->ml_priv;
2834         if (dev->flags & IFF_RUNNING)
2835                 claw_release(dev);
2836         if (privptr) {
2837                 privptr->channel[READ].ndev = NULL;  /* say it's free */
2838         }
2839         dev->ml_priv = NULL;
2840 #ifdef MODULE
2841         if (free_dev) {
2842                 free_netdev(dev);
2843         }
2844 #endif
2845         CLAW_DBF_TEXT(2, setup, "free_ok");
2846 }
2847
2848 /**
2849  * Claw init netdevice
2850  * Initialize everything of the net device except the name and the
2851  * channel structs.
2852  */
2853 static const struct net_device_ops claw_netdev_ops = {
2854         .ndo_open               = claw_open,
2855         .ndo_stop               = claw_release,
2856         .ndo_get_stats          = claw_stats,
2857         .ndo_start_xmit         = claw_tx,
2858         .ndo_change_mtu         = claw_change_mtu,
2859 };
2860
2861 static void
2862 claw_init_netdevice(struct net_device * dev)
2863 {
2864         CLAW_DBF_TEXT(2, setup, "init_dev");
2865         CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
2866         dev->mtu = CLAW_DEFAULT_MTU_SIZE;
2867         dev->hard_header_len = 0;
2868         dev->addr_len = 0;
2869         dev->type = ARPHRD_SLIP;
2870         dev->tx_queue_len = 1300;
2871         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2872         dev->netdev_ops = &claw_netdev_ops;
2873         CLAW_DBF_TEXT(2, setup, "initok");
2874         return;
2875 }
2876
2877 /**
2878  * Init a new channel in the privptr->channel[i].
2879  *
2880  * @param cdev  The ccw_device to be added.
2881  *
2882  * @return 0 on success, !0 on error.
2883  */
2884 static int
2885 add_channel(struct ccw_device *cdev,int i,struct claw_privbk *privptr)
2886 {
2887         struct chbk *p_ch;
2888         struct ccw_dev_id dev_id;
2889
2890         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cdev->dev));
2891         privptr->channel[i].flag  = i+1;   /* Read is 1 Write is 2 */
2892         p_ch = &privptr->channel[i];
2893         p_ch->cdev = cdev;
2894         snprintf(p_ch->id, CLAW_ID_SIZE, "cl-%s", dev_name(&cdev->dev));
2895         ccw_device_get_id(cdev, &dev_id);
2896         p_ch->devno = dev_id.devno;
2897         if ((p_ch->irb = kzalloc(sizeof (struct irb),GFP_KERNEL)) == NULL) {
2898                 return -ENOMEM;
2899         }
2900         return 0;
2901 }
2902
2903
2904 /**
2905  *
2906  * Setup an interface.
2907  *
2908  * @param cgdev  Device to be setup.
2909  *
2910  * @returns 0 on success, !0 on failure.
2911  */
2912 static int
2913 claw_new_device(struct ccwgroup_device *cgdev)
2914 {
2915         struct claw_privbk *privptr;
2916         struct claw_env *p_env;
2917         struct net_device *dev;
2918         int ret;
2919         struct ccw_dev_id dev_id;
2920
2921         dev_info(&cgdev->dev, "add for %s\n",
2922                  dev_name(&cgdev->cdev[READ]->dev));
2923         CLAW_DBF_TEXT(2, setup, "new_dev");
2924         privptr = cgdev->dev.driver_data;
2925         cgdev->cdev[READ]->dev.driver_data = privptr;
2926         cgdev->cdev[WRITE]->dev.driver_data = privptr;
2927         if (!privptr)
2928                 return -ENODEV;
2929         p_env = privptr->p_env;
2930         ccw_device_get_id(cgdev->cdev[READ], &dev_id);
2931         p_env->devno[READ] = dev_id.devno;
2932         ccw_device_get_id(cgdev->cdev[WRITE], &dev_id);
2933         p_env->devno[WRITE] = dev_id.devno;
2934         ret = add_channel(cgdev->cdev[0],0,privptr);
2935         if (ret == 0)
2936                 ret = add_channel(cgdev->cdev[1],1,privptr);
2937         if (ret != 0) {
2938                 dev_warn(&cgdev->dev, "Creating a CLAW group device"
2939                         " failed with error code %d\n", ret);
2940                 goto out;
2941         }
2942         ret = ccw_device_set_online(cgdev->cdev[READ]);
2943         if (ret != 0) {
2944                 dev_warn(&cgdev->dev,
2945                         "Setting the read subchannel online"
2946                         " failed with error code %d\n", ret);
2947                 goto out;
2948         }
2949         ret = ccw_device_set_online(cgdev->cdev[WRITE]);
2950         if (ret != 0) {
2951                 dev_warn(&cgdev->dev,
2952                         "Setting the write subchannel online "
2953                         "failed with error code %d\n", ret);
2954                 goto out;
2955         }
2956         dev = alloc_netdev(0,"claw%d",claw_init_netdevice);
2957         if (!dev) {
2958                 dev_warn(&cgdev->dev,
2959                         "Activating the CLAW device failed\n");
2960                 goto out;
2961         }
2962         dev->ml_priv = privptr;
2963         cgdev->dev.driver_data = privptr;
2964         cgdev->cdev[READ]->dev.driver_data = privptr;
2965         cgdev->cdev[WRITE]->dev.driver_data = privptr;
2966         /* sysfs magic */
2967         SET_NETDEV_DEV(dev, &cgdev->dev);
2968         if (register_netdev(dev) != 0) {
2969                 claw_free_netdevice(dev, 1);
2970                 CLAW_DBF_TEXT(2, trace, "regfail");
2971                 goto out;
2972         }
2973         dev->flags &=~IFF_RUNNING;
2974         if (privptr->buffs_alloc == 0) {
2975                 ret=init_ccw_bk(dev);
2976                 if (ret !=0) {
2977                         unregister_netdev(dev);
2978                         claw_free_netdevice(dev,1);
2979                         CLAW_DBF_TEXT(2, trace, "ccwmem");
2980                         goto out;
2981                 }
2982         }
2983         privptr->channel[READ].ndev = dev;
2984         privptr->channel[WRITE].ndev = dev;
2985         privptr->p_env->ndev = dev;
2986
2987         dev_info(&cgdev->dev, "%s:readsize=%d  writesize=%d "
2988                 "readbuffer=%d writebuffer=%d read=0x%04x write=0x%04x\n",
2989                 dev->name, p_env->read_size,
2990                 p_env->write_size, p_env->read_buffers,
2991                 p_env->write_buffers, p_env->devno[READ],
2992                 p_env->devno[WRITE]);
2993         dev_info(&cgdev->dev, "%s:host_name:%.8s, adapter_name "
2994                 ":%.8s api_type: %.8s\n",
2995                 dev->name, p_env->host_name,
2996                 p_env->adapter_name , p_env->api_type);
2997         return 0;
2998 out:
2999         ccw_device_set_offline(cgdev->cdev[1]);
3000         ccw_device_set_offline(cgdev->cdev[0]);
3001         return -ENODEV;
3002 }
3003
3004 static void
3005 claw_purge_skb_queue(struct sk_buff_head *q)
3006 {
3007         struct sk_buff *skb;
3008
3009         CLAW_DBF_TEXT(4, trace, "purgque");
3010         while ((skb = skb_dequeue(q))) {
3011                 atomic_dec(&skb->users);
3012                 dev_kfree_skb_any(skb);
3013         }
3014 }
3015
3016 /**
3017  * Shutdown an interface.
3018  *
3019  * @param cgdev  Device to be shut down.
3020  *
3021  * @returns 0 on success, !0 on failure.
3022  */
3023 static int
3024 claw_shutdown_device(struct ccwgroup_device *cgdev)
3025 {
3026         struct claw_privbk *priv;
3027         struct net_device *ndev;
3028         int     ret;
3029
3030         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3031         priv = cgdev->dev.driver_data;
3032         if (!priv)
3033                 return -ENODEV;
3034         ndev = priv->channel[READ].ndev;
3035         if (ndev) {
3036                 /* Close the device */
3037                 dev_info(&cgdev->dev, "%s: shutting down \n",
3038                         ndev->name);
3039                 if (ndev->flags & IFF_RUNNING)
3040                         ret = claw_release(ndev);
3041                 ndev->flags &=~IFF_RUNNING;
3042                 unregister_netdev(ndev);
3043                 ndev->ml_priv = NULL;  /* cgdev data, not ndev's to free */
3044                 claw_free_netdevice(ndev, 1);
3045                 priv->channel[READ].ndev = NULL;
3046                 priv->channel[WRITE].ndev = NULL;
3047                 priv->p_env->ndev = NULL;
3048         }
3049         ccw_device_set_offline(cgdev->cdev[1]);
3050         ccw_device_set_offline(cgdev->cdev[0]);
3051         return 0;
3052 }
3053
3054 static void
3055 claw_remove_device(struct ccwgroup_device *cgdev)
3056 {
3057         struct claw_privbk *priv;
3058
3059         BUG_ON(!cgdev);
3060         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3061         priv = cgdev->dev.driver_data;
3062         BUG_ON(!priv);
3063         dev_info(&cgdev->dev, " will be removed.\n");
3064         if (cgdev->state == CCWGROUP_ONLINE)
3065                 claw_shutdown_device(cgdev);
3066         claw_remove_files(&cgdev->dev);
3067         kfree(priv->p_mtc_envelope);
3068         priv->p_mtc_envelope=NULL;
3069         kfree(priv->p_env);
3070         priv->p_env=NULL;
3071         kfree(priv->channel[0].irb);
3072         priv->channel[0].irb=NULL;
3073         kfree(priv->channel[1].irb);
3074         priv->channel[1].irb=NULL;
3075         kfree(priv);
3076         cgdev->dev.driver_data=NULL;
3077         cgdev->cdev[READ]->dev.driver_data = NULL;
3078         cgdev->cdev[WRITE]->dev.driver_data = NULL;
3079         put_device(&cgdev->dev);
3080
3081         return;
3082 }
3083
3084
3085 /*
3086  * sysfs attributes
3087  */
3088 static ssize_t
3089 claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf)
3090 {
3091         struct claw_privbk *priv;
3092         struct claw_env *  p_env;
3093
3094         priv = dev->driver_data;
3095         if (!priv)
3096                 return -ENODEV;
3097         p_env = priv->p_env;
3098         return sprintf(buf, "%s\n",p_env->host_name);
3099 }
3100
3101 static ssize_t
3102 claw_hname_write(struct device *dev, struct device_attribute *attr,
3103          const char *buf, size_t count)
3104 {
3105         struct claw_privbk *priv;
3106         struct claw_env *  p_env;
3107
3108         priv = dev->driver_data;
3109         if (!priv)
3110                 return -ENODEV;
3111         p_env = priv->p_env;
3112         if (count > MAX_NAME_LEN+1)
3113                 return -EINVAL;
3114         memset(p_env->host_name, 0x20, MAX_NAME_LEN);
3115         strncpy(p_env->host_name,buf, count);
3116         p_env->host_name[count-1] = 0x20;  /* clear extra 0x0a */
3117         p_env->host_name[MAX_NAME_LEN] = 0x00;
3118         CLAW_DBF_TEXT(2, setup, "HstnSet");
3119         CLAW_DBF_TEXT_(2, setup, "%s", p_env->host_name);
3120
3121         return count;
3122 }
3123
3124 static DEVICE_ATTR(host_name, 0644, claw_hname_show, claw_hname_write);
3125
3126 static ssize_t
3127 claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf)
3128 {
3129         struct claw_privbk *priv;
3130         struct claw_env *  p_env;
3131
3132         priv = dev->driver_data;
3133         if (!priv)
3134                 return -ENODEV;
3135         p_env = priv->p_env;
3136         return sprintf(buf, "%s\n", p_env->adapter_name);
3137 }
3138
3139 static ssize_t
3140 claw_adname_write(struct device *dev, struct device_attribute *attr,
3141          const char *buf, size_t count)
3142 {
3143         struct claw_privbk *priv;
3144         struct claw_env *  p_env;
3145
3146         priv = dev->driver_data;
3147         if (!priv)
3148                 return -ENODEV;
3149         p_env = priv->p_env;
3150         if (count > MAX_NAME_LEN+1)
3151                 return -EINVAL;
3152         memset(p_env->adapter_name, 0x20, MAX_NAME_LEN);
3153         strncpy(p_env->adapter_name,buf, count);
3154         p_env->adapter_name[count-1] = 0x20; /* clear extra 0x0a */
3155         p_env->adapter_name[MAX_NAME_LEN] = 0x00;
3156         CLAW_DBF_TEXT(2, setup, "AdnSet");
3157         CLAW_DBF_TEXT_(2, setup, "%s", p_env->adapter_name);
3158
3159         return count;
3160 }
3161
3162 static DEVICE_ATTR(adapter_name, 0644, claw_adname_show, claw_adname_write);
3163
3164 static ssize_t
3165 claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf)
3166 {
3167         struct claw_privbk *priv;
3168         struct claw_env *  p_env;
3169
3170         priv = dev->driver_data;
3171         if (!priv)
3172                 return -ENODEV;
3173         p_env = priv->p_env;
3174         return sprintf(buf, "%s\n",
3175                        p_env->api_type);
3176 }
3177
3178 static ssize_t
3179 claw_apname_write(struct device *dev, struct device_attribute *attr,
3180         const char *buf, size_t count)
3181 {
3182         struct claw_privbk *priv;
3183         struct claw_env *  p_env;
3184
3185         priv = dev->driver_data;
3186         if (!priv)
3187                 return -ENODEV;
3188         p_env = priv->p_env;
3189         if (count > MAX_NAME_LEN+1)
3190                 return -EINVAL;
3191         memset(p_env->api_type, 0x20, MAX_NAME_LEN);
3192         strncpy(p_env->api_type,buf, count);
3193         p_env->api_type[count-1] = 0x20;  /* we get a loose 0x0a */
3194         p_env->api_type[MAX_NAME_LEN] = 0x00;
3195         if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
3196                 p_env->read_size=DEF_PACK_BUFSIZE;
3197                 p_env->write_size=DEF_PACK_BUFSIZE;
3198                 p_env->packing=PACKING_ASK;
3199                 CLAW_DBF_TEXT(2, setup, "PACKING");
3200         }
3201         else {
3202                 p_env->packing=0;
3203                 p_env->read_size=CLAW_FRAME_SIZE;
3204                 p_env->write_size=CLAW_FRAME_SIZE;
3205                 CLAW_DBF_TEXT(2, setup, "ApiSet");
3206         }
3207         CLAW_DBF_TEXT_(2, setup, "%s", p_env->api_type);
3208         return count;
3209 }
3210
3211 static DEVICE_ATTR(api_type, 0644, claw_apname_show, claw_apname_write);
3212
3213 static ssize_t
3214 claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
3215 {
3216         struct claw_privbk *priv;
3217         struct claw_env * p_env;
3218
3219         priv = dev->driver_data;
3220         if (!priv)
3221                 return -ENODEV;
3222         p_env = priv->p_env;
3223         return sprintf(buf, "%d\n", p_env->write_buffers);
3224 }
3225
3226 static ssize_t
3227 claw_wbuff_write(struct device *dev, struct device_attribute *attr,
3228         const char *buf, size_t count)
3229 {
3230         struct claw_privbk *priv;
3231         struct claw_env *  p_env;
3232         int nnn,max;
3233
3234         priv = dev->driver_data;
3235         if (!priv)
3236                 return -ENODEV;
3237         p_env = priv->p_env;
3238         sscanf(buf, "%i", &nnn);
3239         if (p_env->packing) {
3240                 max = 64;
3241         }
3242         else {
3243                 max = 512;
3244         }
3245         if ((nnn > max ) || (nnn < 2))
3246                 return -EINVAL;
3247         p_env->write_buffers = nnn;
3248         CLAW_DBF_TEXT(2, setup, "Wbufset");
3249         CLAW_DBF_TEXT_(2, setup, "WB=%d", p_env->write_buffers);
3250         return count;
3251 }
3252
3253 static DEVICE_ATTR(write_buffer, 0644, claw_wbuff_show, claw_wbuff_write);
3254
3255 static ssize_t
3256 claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
3257 {
3258         struct claw_privbk *priv;
3259         struct claw_env *  p_env;
3260
3261         priv = dev->driver_data;
3262         if (!priv)
3263                 return -ENODEV;
3264         p_env = priv->p_env;
3265         return sprintf(buf, "%d\n", p_env->read_buffers);
3266 }
3267
3268 static ssize_t
3269 claw_rbuff_write(struct device *dev, struct device_attribute *attr,
3270         const char *buf, size_t count)
3271 {
3272         struct claw_privbk *priv;
3273         struct claw_env *p_env;
3274         int nnn,max;
3275
3276         priv = dev->driver_data;
3277         if (!priv)
3278                 return -ENODEV;
3279         p_env = priv->p_env;
3280         sscanf(buf, "%i", &nnn);
3281         if (p_env->packing) {
3282                 max = 64;
3283         }
3284         else {
3285                 max = 512;
3286         }
3287         if ((nnn > max ) || (nnn < 2))
3288                 return -EINVAL;
3289         p_env->read_buffers = nnn;
3290         CLAW_DBF_TEXT(2, setup, "Rbufset");
3291         CLAW_DBF_TEXT_(2, setup, "RB=%d", p_env->read_buffers);
3292         return count;
3293 }
3294
3295 static DEVICE_ATTR(read_buffer, 0644, claw_rbuff_show, claw_rbuff_write);
3296
3297 static struct attribute *claw_attr[] = {
3298         &dev_attr_read_buffer.attr,
3299         &dev_attr_write_buffer.attr,
3300         &dev_attr_adapter_name.attr,
3301         &dev_attr_api_type.attr,
3302         &dev_attr_host_name.attr,
3303         NULL,
3304 };
3305
3306 static struct attribute_group claw_attr_group = {
3307         .attrs = claw_attr,
3308 };
3309
3310 static int
3311 claw_add_files(struct device *dev)
3312 {
3313         CLAW_DBF_TEXT(2, setup, "add_file");
3314         return sysfs_create_group(&dev->kobj, &claw_attr_group);
3315 }
3316
3317 static void
3318 claw_remove_files(struct device *dev)
3319 {
3320         CLAW_DBF_TEXT(2, setup, "rem_file");
3321         sysfs_remove_group(&dev->kobj, &claw_attr_group);
3322 }
3323
3324 /*--------------------------------------------------------------------*
3325 *    claw_init  and cleanup                                           *
3326 *---------------------------------------------------------------------*/
3327
3328 static void __exit
3329 claw_cleanup(void)
3330 {
3331         unregister_cu3088_discipline(&claw_group_driver);
3332         claw_unregister_debug_facility();
3333         pr_info("Driver unloaded\n");
3334
3335 }
3336
3337 /**
3338  * Initialize module.
3339  * This is called just after the module is loaded.
3340  *
3341  * @return 0 on success, !0 on error.
3342  */
3343 static int __init
3344 claw_init(void)
3345 {
3346         int ret = 0;
3347
3348         pr_info("Loading %s\n", version);
3349         ret = claw_register_debug_facility();
3350         if (ret) {
3351                 pr_err("Registering with the S/390 debug feature"
3352                         " failed with error code %d\n", ret);
3353                 return ret;
3354         }
3355         CLAW_DBF_TEXT(2, setup, "init_mod");
3356         ret = register_cu3088_discipline(&claw_group_driver);
3357         if (ret) {
3358                 CLAW_DBF_TEXT(2, setup, "init_bad");
3359                 claw_unregister_debug_facility();
3360                 pr_err("Registering with the cu3088 device driver failed "
3361                            "with error code %d\n", ret);
3362         }
3363         return ret;
3364 }
3365
3366 module_init(claw_init);
3367 module_exit(claw_cleanup);
3368
3369 MODULE_AUTHOR("Andy Richter <richtera@us.ibm.com>");
3370 MODULE_DESCRIPTION("Linux for System z CLAW Driver\n" \
3371                         "Copyright 2000,2008 IBM Corporation\n");
3372 MODULE_LICENSE("GPL");