Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6
[safe/jmp/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 IBM Corp. 2002, 2009
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 static int claw_pm_prepare(struct ccwgroup_device *gdev)
257 {
258         return -EPERM;
259 }
260
261 /* ccwgroup table  */
262
263 static struct ccwgroup_driver claw_group_driver = {
264         .owner       = THIS_MODULE,
265         .name        = "claw",
266         .max_slaves  = 2,
267         .driver_id   = 0xC3D3C1E6,
268         .probe       = claw_probe,
269         .remove      = claw_remove_device,
270         .set_online  = claw_new_device,
271         .set_offline = claw_shutdown_device,
272         .prepare     = claw_pm_prepare,
273 };
274
275 /*
276 *       Key functions
277 */
278
279 /*----------------------------------------------------------------*
280  *   claw_probe                                                   *
281  *      this function is called for each CLAW device.             *
282  *----------------------------------------------------------------*/
283 static int
284 claw_probe(struct ccwgroup_device *cgdev)
285 {
286         int             rc;
287         struct claw_privbk *privptr=NULL;
288
289         CLAW_DBF_TEXT(2, setup, "probe");
290         if (!get_device(&cgdev->dev))
291                 return -ENODEV;
292         privptr = kzalloc(sizeof(struct claw_privbk), GFP_KERNEL);
293         dev_set_drvdata(&cgdev->dev, privptr);
294         if (privptr == NULL) {
295                 probe_error(cgdev);
296                 put_device(&cgdev->dev);
297                 CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
298                 return -ENOMEM;
299         }
300         privptr->p_mtc_envelope= kzalloc( MAX_ENVELOPE_SIZE, GFP_KERNEL);
301         privptr->p_env = kzalloc(sizeof(struct claw_env), GFP_KERNEL);
302         if ((privptr->p_mtc_envelope==NULL) || (privptr->p_env==NULL)) {
303                 probe_error(cgdev);
304                 put_device(&cgdev->dev);
305                 CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
306                 return -ENOMEM;
307         }
308         memcpy(privptr->p_env->adapter_name,WS_NAME_NOT_DEF,8);
309         memcpy(privptr->p_env->host_name,WS_NAME_NOT_DEF,8);
310         memcpy(privptr->p_env->api_type,WS_NAME_NOT_DEF,8);
311         privptr->p_env->packing = 0;
312         privptr->p_env->write_buffers = 5;
313         privptr->p_env->read_buffers = 5;
314         privptr->p_env->read_size = CLAW_FRAME_SIZE;
315         privptr->p_env->write_size = CLAW_FRAME_SIZE;
316         rc = claw_add_files(&cgdev->dev);
317         if (rc) {
318                 probe_error(cgdev);
319                 put_device(&cgdev->dev);
320                 dev_err(&cgdev->dev, "Creating the /proc files for a new"
321                 " CLAW device failed\n");
322                 CLAW_DBF_TEXT_(2, setup, "probex%d", rc);
323                 return rc;
324         }
325         privptr->p_env->p_priv = privptr;
326         cgdev->cdev[0]->handler = claw_irq_handler;
327         cgdev->cdev[1]->handler = claw_irq_handler;
328         CLAW_DBF_TEXT(2, setup, "prbext 0");
329
330         return 0;
331 }  /*  end of claw_probe       */
332
333 /*-------------------------------------------------------------------*
334  *   claw_tx                                                         *
335  *-------------------------------------------------------------------*/
336
337 static int
338 claw_tx(struct sk_buff *skb, struct net_device *dev)
339 {
340         int             rc;
341         struct claw_privbk *privptr = dev->ml_priv;
342         unsigned long saveflags;
343         struct chbk *p_ch;
344
345         CLAW_DBF_TEXT(4, trace, "claw_tx");
346         p_ch=&privptr->channel[WRITE];
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         privptr = dev_get_drvdata(&cdev->dev);
601         if (!privptr) {
602                 dev_warn(&cdev->dev, "An uninitialized CLAW device received an"
603                         " IRQ, c-%02x d-%02x\n",
604                         irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
605                 CLAW_DBF_TEXT(2, trace, "badirq");
606                 return;
607         }
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;
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         } else if (sense & 0x20) {
1197                 if (sense & 0x04) {
1198                         dev_warn(dev, "A data streaming timeout occurred"
1199                                 " for %s\n",
1200                                 ndev->name);
1201                 } else if (sense & 0x10) {
1202                         dev_warn(dev, "The remote channel adapter for %s"
1203                                 " is faulty\n",
1204                                 ndev->name);
1205                 } else {
1206                         dev_warn(dev, "A data transfer parity error occurred"
1207                                 " for %s\n",
1208                                 ndev->name);
1209                 }
1210         } else if (sense & 0x10) {
1211                 dev_warn(dev, "A read data parity error occurred"
1212                         " for %s\n",
1213                         ndev->name);
1214         }
1215
1216 }   /*    end of ccw_check_unit_check    */
1217
1218 /*-------------------------------------------------------------------*
1219 *               find_link                                            *
1220 *--------------------------------------------------------------------*/
1221 static int
1222 find_link(struct net_device *dev, char *host_name, char *ws_name )
1223 {
1224         struct claw_privbk *privptr;
1225         struct claw_env *p_env;
1226         int    rc=0;
1227
1228         CLAW_DBF_TEXT(2, setup, "findlink");
1229         privptr = dev->ml_priv;
1230         p_env=privptr->p_env;
1231         switch (p_env->packing)
1232         {
1233                 case  PACKING_ASK:
1234                         if ((memcmp(WS_APPL_NAME_PACKED, host_name, 8)!=0) ||
1235                             (memcmp(WS_APPL_NAME_PACKED, ws_name, 8)!=0 ))
1236                              rc = EINVAL;
1237                         break;
1238                 case  DO_PACKED:
1239                 case  PACK_SEND:
1240                         if ((memcmp(WS_APPL_NAME_IP_NAME, host_name, 8)!=0) ||
1241                             (memcmp(WS_APPL_NAME_IP_NAME, ws_name, 8)!=0 ))
1242                                 rc = EINVAL;
1243                         break;
1244                 default:
1245                         if ((memcmp(HOST_APPL_NAME, host_name, 8)!=0) ||
1246                             (memcmp(p_env->api_type , ws_name, 8)!=0))
1247                                 rc = EINVAL;
1248                         break;
1249         }
1250
1251         return rc;
1252 }    /*    end of find_link    */
1253
1254 /*-------------------------------------------------------------------*
1255  *   claw_hw_tx                                                      *
1256  *                                                                   *
1257  *                                                                   *
1258  *-------------------------------------------------------------------*/
1259
1260 static int
1261 claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid)
1262 {
1263         int                             rc=0;
1264         struct claw_privbk              *privptr;
1265         struct ccwbk           *p_this_ccw;
1266         struct ccwbk           *p_first_ccw;
1267         struct ccwbk           *p_last_ccw;
1268         __u32                           numBuffers;
1269         signed long                     len_of_data;
1270         unsigned long                   bytesInThisBuffer;
1271         unsigned char                   *pDataAddress;
1272         struct endccw                   *pEnd;
1273         struct ccw1                     tempCCW;
1274         struct chbk                     *p_ch;
1275         struct claw_env                 *p_env;
1276         int                             lock;
1277         struct clawph                   *pk_head;
1278         struct chbk                     *ch;
1279
1280         CLAW_DBF_TEXT(4, trace, "hw_tx");
1281         privptr = (struct claw_privbk *)(dev->ml_priv);
1282         p_ch=(struct chbk *)&privptr->channel[WRITE];
1283         p_env =privptr->p_env;
1284         claw_free_wrt_buf(dev); /* Clean up free chain if posible */
1285         /*  scan the write queue to free any completed write packets   */
1286         p_first_ccw=NULL;
1287         p_last_ccw=NULL;
1288         if ((p_env->packing >= PACK_SEND) &&
1289             (skb->cb[1] != 'P')) {
1290                 skb_push(skb,sizeof(struct clawph));
1291                 pk_head=(struct clawph *)skb->data;
1292                 pk_head->len=skb->len-sizeof(struct clawph);
1293                 if (pk_head->len%4)  {
1294                         pk_head->len+= 4-(pk_head->len%4);
1295                         skb_pad(skb,4-(pk_head->len%4));
1296                         skb_put(skb,4-(pk_head->len%4));
1297                 }
1298                 if (p_env->packing == DO_PACKED)
1299                         pk_head->link_num = linkid;
1300                 else
1301                         pk_head->link_num = 0;
1302                 pk_head->flag = 0x00;
1303                 skb_pad(skb,4);
1304                 skb->cb[1] = 'P';
1305         }
1306         if (linkid == 0) {
1307                 if (claw_check_busy(dev)) {
1308                         if (privptr->write_free_count!=0) {
1309                                 claw_clear_busy(dev);
1310                         }
1311                         else {
1312                                 claw_strt_out_IO(dev );
1313                                 claw_free_wrt_buf( dev );
1314                                 if (privptr->write_free_count==0) {
1315                                         ch = &privptr->channel[WRITE];
1316                                         atomic_inc(&skb->users);
1317                                         skb_queue_tail(&ch->collect_queue, skb);
1318                                         goto Done;
1319                                 }
1320                                 else {
1321                                         claw_clear_busy(dev);
1322                                 }
1323                         }
1324                 }
1325                 /*  tx lock  */
1326                 if (claw_test_and_setbit_busy(TB_TX,dev)) { /* set to busy */
1327                         ch = &privptr->channel[WRITE];
1328                         atomic_inc(&skb->users);
1329                         skb_queue_tail(&ch->collect_queue, skb);
1330                         claw_strt_out_IO(dev );
1331                         rc=-EBUSY;
1332                         goto Done2;
1333                 }
1334         }
1335         /*      See how many write buffers are required to hold this data */
1336         numBuffers = DIV_ROUND_UP(skb->len, privptr->p_env->write_size);
1337
1338         /*      If that number of buffers isn't available, give up for now */
1339         if (privptr->write_free_count < numBuffers ||
1340             privptr->p_write_free_chain == NULL ) {
1341
1342                 claw_setbit_busy(TB_NOBUFFER,dev);
1343                 ch = &privptr->channel[WRITE];
1344                 atomic_inc(&skb->users);
1345                 skb_queue_tail(&ch->collect_queue, skb);
1346                 CLAW_DBF_TEXT(2, trace, "clawbusy");
1347                 goto Done2;
1348         }
1349         pDataAddress=skb->data;
1350         len_of_data=skb->len;
1351
1352         while (len_of_data > 0) {
1353                 p_this_ccw=privptr->p_write_free_chain;  /* get a block */
1354                 if (p_this_ccw == NULL) { /* lost the race */
1355                         ch = &privptr->channel[WRITE];
1356                         atomic_inc(&skb->users);
1357                         skb_queue_tail(&ch->collect_queue, skb);
1358                         goto Done2;
1359                 }
1360                 privptr->p_write_free_chain=p_this_ccw->next;
1361                 p_this_ccw->next=NULL;
1362                 --privptr->write_free_count; /* -1 */
1363                 if (len_of_data >= privptr->p_env->write_size)
1364                         bytesInThisBuffer = privptr->p_env->write_size;
1365                 else
1366                         bytesInThisBuffer = len_of_data;
1367                 memcpy( p_this_ccw->p_buffer,pDataAddress, bytesInThisBuffer);
1368                 len_of_data-=bytesInThisBuffer;
1369                 pDataAddress+=(unsigned long)bytesInThisBuffer;
1370                 /*      setup write CCW         */
1371                 p_this_ccw->write.cmd_code = (linkid * 8) +1;
1372                 if (len_of_data>0) {
1373                         p_this_ccw->write.cmd_code+=MORE_to_COME_FLAG;
1374                 }
1375                 p_this_ccw->write.count=bytesInThisBuffer;
1376                 /*      now add to end of this chain    */
1377                 if (p_first_ccw==NULL)    {
1378                         p_first_ccw=p_this_ccw;
1379                 }
1380                 if (p_last_ccw!=NULL) {
1381                         p_last_ccw->next=p_this_ccw;
1382                         /*      set up TIC ccws         */
1383                         p_last_ccw->w_TIC_1.cda=
1384                                 (__u32)__pa(&p_this_ccw->write);
1385                 }
1386                 p_last_ccw=p_this_ccw;      /* save new last block */
1387         }
1388
1389         /*      FirstCCW and LastCCW now contain a new set of write channel
1390         *       programs to append to the running channel program
1391         */
1392
1393         if (p_first_ccw!=NULL) {
1394                 /*      setup ending ccw sequence for this segment           */
1395                 pEnd=privptr->p_end_ccw;
1396                 if (pEnd->write1) {
1397                         pEnd->write1=0x00;   /* second end ccw is now active */
1398                         /*      set up Tic CCWs         */
1399                         p_last_ccw->w_TIC_1.cda=
1400                                 (__u32)__pa(&pEnd->write2_nop1);
1401                         pEnd->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1402                         pEnd->write2_nop2.flags    =
1403                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1404                         pEnd->write2_nop2.cda=0;
1405                         pEnd->write2_nop2.count=1;
1406                 }
1407                 else {  /*  end of if (pEnd->write1)*/
1408                         pEnd->write1=0x01;   /* first end ccw is now active */
1409                         /*      set up Tic CCWs         */
1410                         p_last_ccw->w_TIC_1.cda=
1411                                 (__u32)__pa(&pEnd->write1_nop1);
1412                         pEnd->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1413                         pEnd->write1_nop2.flags    =
1414                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1415                         pEnd->write1_nop2.cda=0;
1416                         pEnd->write1_nop2.count=1;
1417                 }  /* end if if (pEnd->write1) */
1418
1419                 if (privptr->p_write_active_first==NULL ) {
1420                         privptr->p_write_active_first=p_first_ccw;
1421                         privptr->p_write_active_last=p_last_ccw;
1422                 }
1423                 else {
1424                         /*      set up Tic CCWs         */
1425
1426                         tempCCW.cda=(__u32)__pa(&p_first_ccw->write);
1427                         tempCCW.count=0;
1428                         tempCCW.flags=0;
1429                         tempCCW.cmd_code=CCW_CLAW_CMD_TIC;
1430
1431                         if (pEnd->write1) {
1432
1433                  /*
1434                  * first set of ending CCW's is chained to the new write
1435                  * chain, so the second set is chained to the active chain
1436                  * Therefore modify the second set to point the new write chain.
1437                  * make sure we update the CCW atomically
1438                  * so channel does not fetch it when it's only half done
1439                  */
1440                                 memcpy( &pEnd->write2_nop2, &tempCCW ,
1441                                         sizeof(struct ccw1));
1442                                 privptr->p_write_active_last->w_TIC_1.cda=
1443                                         (__u32)__pa(&p_first_ccw->write);
1444                         }
1445                         else {
1446
1447                         /*make sure we update the CCW atomically
1448                          *so channel does not fetch it when it's only half done
1449                          */
1450                                 memcpy(&pEnd->write1_nop2, &tempCCW ,
1451                                         sizeof(struct ccw1));
1452                                 privptr->p_write_active_last->w_TIC_1.cda=
1453                                         (__u32)__pa(&p_first_ccw->write);
1454
1455                         } /* end if if (pEnd->write1) */
1456
1457                         privptr->p_write_active_last->next=p_first_ccw;
1458                         privptr->p_write_active_last=p_last_ccw;
1459                 }
1460
1461         } /* endif (p_first_ccw!=NULL)  */
1462         dev_kfree_skb_any(skb);
1463         if (linkid==0) {
1464                 lock=LOCK_NO;
1465         }
1466         else  {
1467                 lock=LOCK_YES;
1468         }
1469         claw_strt_out_IO(dev );
1470         /*      if write free count is zero , set NOBUFFER       */
1471         if (privptr->write_free_count==0) {
1472                 claw_setbit_busy(TB_NOBUFFER,dev);
1473         }
1474 Done2:
1475         claw_clearbit_busy(TB_TX,dev);
1476 Done:
1477         return(rc);
1478 }    /*    end of claw_hw_tx    */
1479
1480 /*-------------------------------------------------------------------*
1481 *                                                                    *
1482 *     init_ccw_bk                                                    *
1483 *                                                                    *
1484 *--------------------------------------------------------------------*/
1485
1486 static int
1487 init_ccw_bk(struct net_device *dev)
1488 {
1489
1490         __u32   ccw_blocks_required;
1491         __u32   ccw_blocks_perpage;
1492         __u32   ccw_pages_required;
1493         __u32   claw_reads_perpage=1;
1494         __u32   claw_read_pages;
1495         __u32   claw_writes_perpage=1;
1496         __u32   claw_write_pages;
1497         void    *p_buff=NULL;
1498         struct ccwbk*p_free_chain;
1499         struct ccwbk*p_buf;
1500         struct ccwbk*p_last_CCWB;
1501         struct ccwbk*p_first_CCWB;
1502         struct endccw *p_endccw=NULL;
1503         addr_t  real_address;
1504         struct claw_privbk *privptr = dev->ml_priv;
1505         struct clawh *pClawH=NULL;
1506         addr_t   real_TIC_address;
1507         int i,j;
1508         CLAW_DBF_TEXT(4, trace, "init_ccw");
1509
1510         /*  initialize  statistics field */
1511         privptr->active_link_ID=0;
1512         /*  initialize  ccwbk pointers  */
1513         privptr->p_write_free_chain=NULL;   /* pointer to free ccw chain*/
1514         privptr->p_write_active_first=NULL; /* pointer to the first write ccw*/
1515         privptr->p_write_active_last=NULL;  /* pointer to the last write ccw*/
1516         privptr->p_read_active_first=NULL;  /* pointer to the first read ccw*/
1517         privptr->p_read_active_last=NULL;   /* pointer to the last read ccw */
1518         privptr->p_end_ccw=NULL;            /* pointer to ending ccw        */
1519         privptr->p_claw_signal_blk=NULL;    /* pointer to signal block      */
1520         privptr->buffs_alloc = 0;
1521         memset(&privptr->end_ccw, 0x00, sizeof(struct endccw));
1522         memset(&privptr->ctl_bk, 0x00, sizeof(struct clawctl));
1523         /*  initialize  free write ccwbk counter  */
1524         privptr->write_free_count=0;  /* number of free bufs on write chain */
1525         p_last_CCWB = NULL;
1526         p_first_CCWB= NULL;
1527         /*
1528         *  We need 1 CCW block for each read buffer, 1 for each
1529         *  write buffer, plus 1 for ClawSignalBlock
1530         */
1531         ccw_blocks_required =
1532                 privptr->p_env->read_buffers+privptr->p_env->write_buffers+1;
1533         /*
1534         * compute number of CCW blocks that will fit in a page
1535         */
1536         ccw_blocks_perpage= PAGE_SIZE /  CCWBK_SIZE;
1537         ccw_pages_required=
1538                 DIV_ROUND_UP(ccw_blocks_required, ccw_blocks_perpage);
1539
1540         /*
1541          *  read and write sizes are set by 2 constants in claw.h
1542          *  4k and 32k.  Unpacked values other than 4k are not going to
1543          * provide good performance. With packing buffers support 32k
1544          * buffers are used.
1545          */
1546         if (privptr->p_env->read_size < PAGE_SIZE) {
1547                 claw_reads_perpage = PAGE_SIZE / privptr->p_env->read_size;
1548                 claw_read_pages = DIV_ROUND_UP(privptr->p_env->read_buffers,
1549                                                 claw_reads_perpage);
1550          }
1551          else {       /* > or equal  */
1552                 privptr->p_buff_pages_perread =
1553                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
1554                 claw_read_pages = privptr->p_env->read_buffers *
1555                                         privptr->p_buff_pages_perread;
1556          }
1557         if (privptr->p_env->write_size < PAGE_SIZE) {
1558                 claw_writes_perpage =
1559                         PAGE_SIZE / privptr->p_env->write_size;
1560                 claw_write_pages = DIV_ROUND_UP(privptr->p_env->write_buffers,
1561                                                 claw_writes_perpage);
1562
1563         }
1564         else {      /* >  or equal  */
1565                 privptr->p_buff_pages_perwrite =
1566                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
1567                 claw_write_pages = privptr->p_env->write_buffers *
1568                                         privptr->p_buff_pages_perwrite;
1569         }
1570         /*
1571         *               allocate ccw_pages_required
1572         */
1573         if (privptr->p_buff_ccw==NULL) {
1574                 privptr->p_buff_ccw=
1575                         (void *)__get_free_pages(__GFP_DMA,
1576                         (int)pages_to_order_of_mag(ccw_pages_required ));
1577                 if (privptr->p_buff_ccw==NULL) {
1578                         return -ENOMEM;
1579                 }
1580                 privptr->p_buff_ccw_num=ccw_pages_required;
1581         }
1582         memset(privptr->p_buff_ccw, 0x00,
1583                 privptr->p_buff_ccw_num * PAGE_SIZE);
1584
1585         /*
1586         *               obtain ending ccw block address
1587         *
1588         */
1589         privptr->p_end_ccw = (struct endccw *)&privptr->end_ccw;
1590         real_address  = (__u32)__pa(privptr->p_end_ccw);
1591         /*                              Initialize ending CCW block       */
1592         p_endccw=privptr->p_end_ccw;
1593         p_endccw->real=real_address;
1594         p_endccw->write1=0x00;
1595         p_endccw->read1=0x00;
1596
1597         /*      write1_nop1                                     */
1598         p_endccw->write1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1599         p_endccw->write1_nop1.flags       = CCW_FLAG_SLI | CCW_FLAG_CC;
1600         p_endccw->write1_nop1.count       = 1;
1601         p_endccw->write1_nop1.cda         = 0;
1602
1603         /*      write1_nop2                                     */
1604         p_endccw->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1605         p_endccw->write1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1606         p_endccw->write1_nop2.count      = 1;
1607         p_endccw->write1_nop2.cda        = 0;
1608
1609         /*      write2_nop1                                     */
1610         p_endccw->write2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1611         p_endccw->write2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1612         p_endccw->write2_nop1.count        = 1;
1613         p_endccw->write2_nop1.cda          = 0;
1614
1615         /*      write2_nop2                                     */
1616         p_endccw->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1617         p_endccw->write2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1618         p_endccw->write2_nop2.count        = 1;
1619         p_endccw->write2_nop2.cda          = 0;
1620
1621         /*      read1_nop1                                      */
1622         p_endccw->read1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1623         p_endccw->read1_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1624         p_endccw->read1_nop1.count        = 1;
1625         p_endccw->read1_nop1.cda          = 0;
1626
1627         /*      read1_nop2                                      */
1628         p_endccw->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1629         p_endccw->read1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1630         p_endccw->read1_nop2.count        = 1;
1631         p_endccw->read1_nop2.cda          = 0;
1632
1633         /*      read2_nop1                                      */
1634         p_endccw->read2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
1635         p_endccw->read2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
1636         p_endccw->read2_nop1.count        = 1;
1637         p_endccw->read2_nop1.cda          = 0;
1638
1639         /*      read2_nop2                                      */
1640         p_endccw->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1641         p_endccw->read2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1642         p_endccw->read2_nop2.count        = 1;
1643         p_endccw->read2_nop2.cda          = 0;
1644
1645         /*
1646         *                               Build a chain of CCWs
1647         *
1648         */
1649         p_buff=privptr->p_buff_ccw;
1650
1651         p_free_chain=NULL;
1652         for (i=0 ; i < ccw_pages_required; i++ ) {
1653                 real_address  = (__u32)__pa(p_buff);
1654                 p_buf=p_buff;
1655                 for (j=0 ; j < ccw_blocks_perpage ; j++) {
1656                         p_buf->next  = p_free_chain;
1657                         p_free_chain = p_buf;
1658                         p_buf->real=(__u32)__pa(p_buf);
1659                         ++p_buf;
1660                 }
1661                 p_buff+=PAGE_SIZE;
1662         }
1663         /*
1664         *                               Initialize ClawSignalBlock
1665         *
1666         */
1667         if (privptr->p_claw_signal_blk==NULL) {
1668                 privptr->p_claw_signal_blk=p_free_chain;
1669                 p_free_chain=p_free_chain->next;
1670                 pClawH=(struct clawh *)privptr->p_claw_signal_blk;
1671                 pClawH->length=0xffff;
1672                 pClawH->opcode=0xff;
1673                 pClawH->flag=CLAW_BUSY;
1674         }
1675
1676         /*
1677         *               allocate write_pages_required and add to free chain
1678         */
1679         if (privptr->p_buff_write==NULL) {
1680             if (privptr->p_env->write_size < PAGE_SIZE) {
1681                 privptr->p_buff_write=
1682                         (void *)__get_free_pages(__GFP_DMA,
1683                         (int)pages_to_order_of_mag(claw_write_pages ));
1684                 if (privptr->p_buff_write==NULL) {
1685                         privptr->p_buff_ccw=NULL;
1686                         return -ENOMEM;
1687                 }
1688                 /*
1689                 *                               Build CLAW write free chain
1690                 *
1691                 */
1692
1693                 memset(privptr->p_buff_write, 0x00,
1694                         ccw_pages_required * PAGE_SIZE);
1695                 privptr->p_write_free_chain=NULL;
1696
1697                 p_buff=privptr->p_buff_write;
1698
1699                 for (i=0 ; i< privptr->p_env->write_buffers ; i++) {
1700                         p_buf        = p_free_chain;      /*  get a CCW */
1701                         p_free_chain = p_buf->next;
1702                         p_buf->next  =privptr->p_write_free_chain;
1703                         privptr->p_write_free_chain = p_buf;
1704                         p_buf-> p_buffer        = (struct clawbuf *)p_buff;
1705                         p_buf-> write.cda       = (__u32)__pa(p_buff);
1706                         p_buf-> write.flags     = CCW_FLAG_SLI | CCW_FLAG_CC;
1707                         p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1708                         p_buf-> w_read_FF.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
1709                         p_buf-> w_read_FF.count   = 1;
1710                         p_buf-> w_read_FF.cda     =
1711                                 (__u32)__pa(&p_buf-> header.flag);
1712                         p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1713                         p_buf-> w_TIC_1.flags      = 0;
1714                         p_buf-> w_TIC_1.count      = 0;
1715
1716                         if (((unsigned long)p_buff +
1717                                             privptr->p_env->write_size) >=
1718                            ((unsigned long)(p_buff+2*
1719                             (privptr->p_env->write_size) - 1) & PAGE_MASK)) {
1720                                 p_buff = p_buff+privptr->p_env->write_size;
1721                         }
1722                 }
1723            }
1724            else      /*  Buffers are => PAGE_SIZE. 1 buff per get_free_pages */
1725            {
1726                privptr->p_write_free_chain=NULL;
1727                for (i = 0; i< privptr->p_env->write_buffers ; i++) {
1728                    p_buff=(void *)__get_free_pages(__GFP_DMA,
1729                         (int)pages_to_order_of_mag(
1730                         privptr->p_buff_pages_perwrite) );
1731                    if (p_buff==NULL) {
1732                         free_pages((unsigned long)privptr->p_buff_ccw,
1733                               (int)pages_to_order_of_mag(
1734                                         privptr->p_buff_ccw_num));
1735                         privptr->p_buff_ccw=NULL;
1736                         p_buf=privptr->p_buff_write;
1737                         while (p_buf!=NULL) {
1738                                 free_pages((unsigned long)
1739                                         p_buf->p_buffer,
1740                                         (int)pages_to_order_of_mag(
1741                                         privptr->p_buff_pages_perwrite));
1742                                 p_buf=p_buf->next;
1743                         }
1744                         return -ENOMEM;
1745                    }  /* Error on get_pages   */
1746                    memset(p_buff, 0x00, privptr->p_env->write_size );
1747                    p_buf         = p_free_chain;
1748                    p_free_chain  = p_buf->next;
1749                    p_buf->next   = privptr->p_write_free_chain;
1750                    privptr->p_write_free_chain = p_buf;
1751                    privptr->p_buff_write = p_buf;
1752                    p_buf->p_buffer=(struct clawbuf *)p_buff;
1753                    p_buf-> write.cda     = (__u32)__pa(p_buff);
1754                    p_buf-> write.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
1755                    p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1756                    p_buf-> w_read_FF.flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
1757                    p_buf-> w_read_FF.count    = 1;
1758                    p_buf-> w_read_FF.cda      =
1759                         (__u32)__pa(&p_buf-> header.flag);
1760                    p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1761                    p_buf-> w_TIC_1.flags   = 0;
1762                    p_buf-> w_TIC_1.count   = 0;
1763                }  /* for all write_buffers   */
1764
1765            }    /* else buffers are PAGE_SIZE or bigger */
1766
1767         }
1768         privptr->p_buff_write_num=claw_write_pages;
1769         privptr->write_free_count=privptr->p_env->write_buffers;
1770
1771
1772         /*
1773         *               allocate read_pages_required and chain to free chain
1774         */
1775         if (privptr->p_buff_read==NULL) {
1776             if (privptr->p_env->read_size < PAGE_SIZE)  {
1777                 privptr->p_buff_read=
1778                         (void *)__get_free_pages(__GFP_DMA,
1779                         (int)pages_to_order_of_mag(claw_read_pages) );
1780                 if (privptr->p_buff_read==NULL) {
1781                         free_pages((unsigned long)privptr->p_buff_ccw,
1782                                 (int)pages_to_order_of_mag(
1783                                         privptr->p_buff_ccw_num));
1784                         /* free the write pages size is < page size  */
1785                         free_pages((unsigned long)privptr->p_buff_write,
1786                                 (int)pages_to_order_of_mag(
1787                                 privptr->p_buff_write_num));
1788                         privptr->p_buff_ccw=NULL;
1789                         privptr->p_buff_write=NULL;
1790                         return -ENOMEM;
1791                 }
1792                 memset(privptr->p_buff_read, 0x00, claw_read_pages * PAGE_SIZE);
1793                 privptr->p_buff_read_num=claw_read_pages;
1794                 /*
1795                 *                               Build CLAW read free chain
1796                 *
1797                 */
1798                 p_buff=privptr->p_buff_read;
1799                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
1800                         p_buf        = p_free_chain;
1801                         p_free_chain = p_buf->next;
1802
1803                         if (p_last_CCWB==NULL) {
1804                                 p_buf->next=NULL;
1805                                 real_TIC_address=0;
1806                                 p_last_CCWB=p_buf;
1807                         }
1808                         else {
1809                                 p_buf->next=p_first_CCWB;
1810                                 real_TIC_address=
1811                                 (__u32)__pa(&p_first_CCWB -> read );
1812                         }
1813
1814                         p_first_CCWB=p_buf;
1815
1816                         p_buf->p_buffer=(struct clawbuf *)p_buff;
1817                         /*  initialize read command */
1818                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
1819                         p_buf-> read.cda = (__u32)__pa(p_buff);
1820                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1821                         p_buf-> read.count       = privptr->p_env->read_size;
1822
1823                         /*  initialize read_h command */
1824                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
1825                         p_buf-> read_h.cda =
1826                                 (__u32)__pa(&(p_buf->header));
1827                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1828                         p_buf-> read_h.count      = sizeof(struct clawh);
1829
1830                         /*  initialize Signal command */
1831                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
1832                         p_buf-> signal.cda =
1833                                 (__u32)__pa(&(pClawH->flag));
1834                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1835                         p_buf-> signal.count     = 1;
1836
1837                         /*  initialize r_TIC_1 command */
1838                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1839                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
1840                         p_buf-> r_TIC_1.flags = 0;
1841                         p_buf-> r_TIC_1.count      = 0;
1842
1843                         /*  initialize r_read_FF command */
1844                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1845                         p_buf-> r_read_FF.cda =
1846                                 (__u32)__pa(&(pClawH->flag));
1847                         p_buf-> r_read_FF.flags =
1848                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
1849                         p_buf-> r_read_FF.count    = 1;
1850
1851                         /*    initialize r_TIC_2          */
1852                         memcpy(&p_buf->r_TIC_2,
1853                                 &p_buf->r_TIC_1, sizeof(struct ccw1));
1854
1855                         /*     initialize Header     */
1856                         p_buf->header.length=0xffff;
1857                         p_buf->header.opcode=0xff;
1858                         p_buf->header.flag=CLAW_PENDING;
1859
1860                         if (((unsigned long)p_buff+privptr->p_env->read_size) >=
1861                           ((unsigned long)(p_buff+2*(privptr->p_env->read_size)
1862                                  -1)
1863                            & PAGE_MASK)) {
1864                                 p_buff= p_buff+privptr->p_env->read_size;
1865                         }
1866                         else {
1867                                 p_buff=
1868                                 (void *)((unsigned long)
1869                                         (p_buff+2*(privptr->p_env->read_size)-1)
1870                                          & PAGE_MASK) ;
1871                         }
1872                 }   /* for read_buffers   */
1873           }         /* read_size < PAGE_SIZE  */
1874           else {  /* read Size >= PAGE_SIZE  */
1875                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
1876                         p_buff = (void *)__get_free_pages(__GFP_DMA,
1877                                 (int)pages_to_order_of_mag(
1878                                         privptr->p_buff_pages_perread));
1879                         if (p_buff==NULL) {
1880                                 free_pages((unsigned long)privptr->p_buff_ccw,
1881                                         (int)pages_to_order_of_mag(privptr->
1882                                         p_buff_ccw_num));
1883                                 /* free the write pages  */
1884                                 p_buf=privptr->p_buff_write;
1885                                 while (p_buf!=NULL) {
1886                                         free_pages(
1887                                             (unsigned long)p_buf->p_buffer,
1888                                             (int)pages_to_order_of_mag(
1889                                             privptr->p_buff_pages_perwrite));
1890                                         p_buf=p_buf->next;
1891                                 }
1892                                 /* free any read pages already alloc  */
1893                                 p_buf=privptr->p_buff_read;
1894                                 while (p_buf!=NULL) {
1895                                         free_pages(
1896                                             (unsigned long)p_buf->p_buffer,
1897                                             (int)pages_to_order_of_mag(
1898                                              privptr->p_buff_pages_perread));
1899                                         p_buf=p_buf->next;
1900                                 }
1901                                 privptr->p_buff_ccw=NULL;
1902                                 privptr->p_buff_write=NULL;
1903                                 return -ENOMEM;
1904                         }
1905                         memset(p_buff, 0x00, privptr->p_env->read_size);
1906                         p_buf        = p_free_chain;
1907                         privptr->p_buff_read = p_buf;
1908                         p_free_chain = p_buf->next;
1909
1910                         if (p_last_CCWB==NULL) {
1911                                 p_buf->next=NULL;
1912                                 real_TIC_address=0;
1913                                 p_last_CCWB=p_buf;
1914                         }
1915                         else {
1916                                 p_buf->next=p_first_CCWB;
1917                                 real_TIC_address=
1918                                         (addr_t)__pa(
1919                                                 &p_first_CCWB -> read );
1920                         }
1921
1922                         p_first_CCWB=p_buf;
1923                                 /* save buff address */
1924                         p_buf->p_buffer=(struct clawbuf *)p_buff;
1925                         /*  initialize read command */
1926                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
1927                         p_buf-> read.cda = (__u32)__pa(p_buff);
1928                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1929                         p_buf-> read.count       = privptr->p_env->read_size;
1930
1931                         /*  initialize read_h command */
1932                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
1933                         p_buf-> read_h.cda =
1934                                 (__u32)__pa(&(p_buf->header));
1935                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1936                         p_buf-> read_h.count      = sizeof(struct clawh);
1937
1938                         /*  initialize Signal command */
1939                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
1940                         p_buf-> signal.cda =
1941                                 (__u32)__pa(&(pClawH->flag));
1942                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
1943                         p_buf-> signal.count     = 1;
1944
1945                         /*  initialize r_TIC_1 command */
1946                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
1947                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
1948                         p_buf-> r_TIC_1.flags = 0;
1949                         p_buf-> r_TIC_1.count      = 0;
1950
1951                         /*  initialize r_read_FF command */
1952                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
1953                         p_buf-> r_read_FF.cda =
1954                                 (__u32)__pa(&(pClawH->flag));
1955                         p_buf-> r_read_FF.flags =
1956                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
1957                         p_buf-> r_read_FF.count    = 1;
1958
1959                         /*    initialize r_TIC_2          */
1960                         memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1,
1961                                 sizeof(struct ccw1));
1962
1963                         /*     initialize Header     */
1964                         p_buf->header.length=0xffff;
1965                         p_buf->header.opcode=0xff;
1966                         p_buf->header.flag=CLAW_PENDING;
1967
1968                 }    /* For read_buffers   */
1969           }     /*  read_size >= PAGE_SIZE   */
1970         }       /*  pBuffread = NULL */
1971         add_claw_reads( dev  ,p_first_CCWB , p_last_CCWB);
1972         privptr->buffs_alloc = 1;
1973
1974         return 0;
1975 }    /*    end of init_ccw_bk */
1976
1977 /*-------------------------------------------------------------------*
1978 *                                                                    *
1979 *       probe_error                                                  *
1980 *                                                                    *
1981 *--------------------------------------------------------------------*/
1982
1983 static void
1984 probe_error( struct ccwgroup_device *cgdev)
1985 {
1986         struct claw_privbk *privptr;
1987
1988         CLAW_DBF_TEXT(4, trace, "proberr");
1989         privptr = dev_get_drvdata(&cgdev->dev);
1990         if (privptr != NULL) {
1991                 dev_set_drvdata(&cgdev->dev, NULL);
1992                 kfree(privptr->p_env);
1993                 kfree(privptr->p_mtc_envelope);
1994                 kfree(privptr);
1995         }
1996 }    /*    probe_error    */
1997
1998 /*-------------------------------------------------------------------*
1999 *    claw_process_control                                            *
2000 *                                                                    *
2001 *                                                                    *
2002 *--------------------------------------------------------------------*/
2003
2004 static int
2005 claw_process_control( struct net_device *dev, struct ccwbk * p_ccw)
2006 {
2007
2008         struct clawbuf *p_buf;
2009         struct clawctl  ctlbk;
2010         struct clawctl *p_ctlbk;
2011         char    temp_host_name[8];
2012         char    temp_ws_name[8];
2013         struct claw_privbk *privptr;
2014         struct claw_env *p_env;
2015         struct sysval *p_sysval;
2016         struct conncmd *p_connect=NULL;
2017         int rc;
2018         struct chbk *p_ch = NULL;
2019         struct device *tdev;
2020         CLAW_DBF_TEXT(2, setup, "clw_cntl");
2021         udelay(1000);  /* Wait a ms for the control packets to
2022                         *catch up to each other */
2023         privptr = dev->ml_priv;
2024         p_env=privptr->p_env;
2025         tdev = &privptr->channel[READ].cdev->dev;
2026         memcpy( &temp_host_name, p_env->host_name, 8);
2027         memcpy( &temp_ws_name, p_env->adapter_name , 8);
2028         dev_info(tdev, "%s: CLAW device %.8s: "
2029                 "Received Control Packet\n",
2030                 dev->name, temp_ws_name);
2031         if (privptr->release_pend==1) {
2032                 return 0;
2033         }
2034         p_buf=p_ccw->p_buffer;
2035         p_ctlbk=&ctlbk;
2036         if (p_env->packing == DO_PACKED) { /* packing in progress?*/
2037                 memcpy(p_ctlbk, &p_buf->buffer[4], sizeof(struct clawctl));
2038         } else {
2039                 memcpy(p_ctlbk, p_buf, sizeof(struct clawctl));
2040         }
2041         switch (p_ctlbk->command)
2042         {
2043         case SYSTEM_VALIDATE_REQUEST:
2044                 if (p_ctlbk->version != CLAW_VERSION_ID) {
2045                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2046                                 CLAW_RC_WRONG_VERSION);
2047                         dev_warn(tdev, "The communication peer of %s"
2048                                 " uses an incorrect API version %d\n",
2049                                 dev->name, p_ctlbk->version);
2050                 }
2051                 p_sysval = (struct sysval *)&(p_ctlbk->data);
2052                 dev_info(tdev, "%s: Recv Sys Validate Request: "
2053                         "Vers=%d,link_id=%d,Corr=%d,WS name=%.8s,"
2054                         "Host name=%.8s\n",
2055                         dev->name, p_ctlbk->version,
2056                         p_ctlbk->linkid,
2057                         p_ctlbk->correlator,
2058                         p_sysval->WS_name,
2059                         p_sysval->host_name);
2060                 if (memcmp(temp_host_name, p_sysval->host_name, 8)) {
2061                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2062                                 CLAW_RC_NAME_MISMATCH);
2063                         CLAW_DBF_TEXT(2, setup, "HSTBAD");
2064                         CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->host_name);
2065                         CLAW_DBF_TEXT_(2, setup, "%s", temp_host_name);
2066                         dev_warn(tdev,
2067                                 "Host name %s for %s does not match the"
2068                                 " remote adapter name %s\n",
2069                                 p_sysval->host_name,
2070                                 dev->name,
2071                                 temp_host_name);
2072                 }
2073                 if (memcmp(temp_ws_name, p_sysval->WS_name, 8)) {
2074                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2075                                 CLAW_RC_NAME_MISMATCH);
2076                         CLAW_DBF_TEXT(2, setup, "WSNBAD");
2077                         CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->WS_name);
2078                         CLAW_DBF_TEXT_(2, setup, "%s", temp_ws_name);
2079                         dev_warn(tdev, "Adapter name %s for %s does not match"
2080                                 " the remote host name %s\n",
2081                                 p_sysval->WS_name,
2082                                 dev->name,
2083                                 temp_ws_name);
2084                 }
2085                 if ((p_sysval->write_frame_size < p_env->write_size) &&
2086                     (p_env->packing == 0)) {
2087                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2088                                 CLAW_RC_HOST_RCV_TOO_SMALL);
2089                         dev_warn(tdev,
2090                                 "The local write buffer is smaller than the"
2091                                 " remote read buffer\n");
2092                         CLAW_DBF_TEXT(2, setup, "wrtszbad");
2093                 }
2094                 if ((p_sysval->read_frame_size < p_env->read_size) &&
2095                     (p_env->packing == 0)) {
2096                         claw_snd_sys_validate_rsp(dev, p_ctlbk,
2097                                 CLAW_RC_HOST_RCV_TOO_SMALL);
2098                         dev_warn(tdev,
2099                                 "The local read buffer is smaller than the"
2100                                 " remote write buffer\n");
2101                         CLAW_DBF_TEXT(2, setup, "rdsizbad");
2102                 }
2103                 claw_snd_sys_validate_rsp(dev, p_ctlbk, 0);
2104                 dev_info(tdev,
2105                         "CLAW device %.8s: System validate"
2106                         " completed.\n", temp_ws_name);
2107                 dev_info(tdev,
2108                         "%s: sys Validate Rsize:%d Wsize:%d\n",
2109                         dev->name, p_sysval->read_frame_size,
2110                         p_sysval->write_frame_size);
2111                 privptr->system_validate_comp = 1;
2112                 if (strncmp(p_env->api_type, WS_APPL_NAME_PACKED, 6) == 0)
2113                         p_env->packing = PACKING_ASK;
2114                 claw_strt_conn_req(dev);
2115                 break;
2116         case SYSTEM_VALIDATE_RESPONSE:
2117                 p_sysval = (struct sysval *)&(p_ctlbk->data);
2118                 dev_info(tdev,
2119                         "Settings for %s validated (version=%d, "
2120                         "remote device=%d, rc=%d, adapter name=%.8s, "
2121                         "host name=%.8s)\n",
2122                         dev->name,
2123                         p_ctlbk->version,
2124                         p_ctlbk->correlator,
2125                         p_ctlbk->rc,
2126                         p_sysval->WS_name,
2127                         p_sysval->host_name);
2128                 switch (p_ctlbk->rc) {
2129                 case 0:
2130                         dev_info(tdev, "%s: CLAW device "
2131                                 "%.8s: System validate completed.\n",
2132                                 dev->name, temp_ws_name);
2133                         if (privptr->system_validate_comp == 0)
2134                                 claw_strt_conn_req(dev);
2135                         privptr->system_validate_comp = 1;
2136                         break;
2137                 case CLAW_RC_NAME_MISMATCH:
2138                         dev_warn(tdev, "Validating %s failed because of"
2139                                 " a host or adapter name mismatch\n",
2140                                 dev->name);
2141                         break;
2142                 case CLAW_RC_WRONG_VERSION:
2143                         dev_warn(tdev, "Validating %s failed because of a"
2144                                 " version conflict\n",
2145                                 dev->name);
2146                         break;
2147                 case CLAW_RC_HOST_RCV_TOO_SMALL:
2148                         dev_warn(tdev, "Validating %s failed because of a"
2149                                 " frame size conflict\n",
2150                                 dev->name);
2151                         break;
2152                 default:
2153                         dev_warn(tdev, "The communication peer of %s rejected"
2154                                 " the connection\n",
2155                                  dev->name);
2156                         break;
2157                 }
2158                 break;
2159
2160         case CONNECTION_REQUEST:
2161                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2162                 dev_info(tdev, "%s: Recv Conn Req: Vers=%d,link_id=%d,"
2163                         "Corr=%d,HOST appl=%.8s,WS appl=%.8s\n",
2164                         dev->name,
2165                         p_ctlbk->version,
2166                         p_ctlbk->linkid,
2167                         p_ctlbk->correlator,
2168                         p_connect->host_name,
2169                         p_connect->WS_name);
2170                 if (privptr->active_link_ID != 0) {
2171                         claw_snd_disc(dev, p_ctlbk);
2172                         dev_info(tdev, "%s rejected a connection request"
2173                                 " because it is already active\n",
2174                                 dev->name);
2175                 }
2176                 if (p_ctlbk->linkid != 1) {
2177                         claw_snd_disc(dev, p_ctlbk);
2178                         dev_info(tdev, "%s rejected a request to open multiple"
2179                                 " connections\n",
2180                                 dev->name);
2181                 }
2182                 rc = find_link(dev, p_connect->host_name, p_connect->WS_name);
2183                 if (rc != 0) {
2184                         claw_snd_disc(dev, p_ctlbk);
2185                         dev_info(tdev, "%s rejected a connection request"
2186                                 " because of a type mismatch\n",
2187                                 dev->name);
2188                 }
2189                 claw_send_control(dev,
2190                         CONNECTION_CONFIRM, p_ctlbk->linkid,
2191                         p_ctlbk->correlator,
2192                         0, p_connect->host_name,
2193                         p_connect->WS_name);
2194                 if (p_env->packing == PACKING_ASK) {
2195                         p_env->packing = PACK_SEND;
2196                         claw_snd_conn_req(dev, 0);
2197                 }
2198                 dev_info(tdev, "%s: CLAW device %.8s: Connection "
2199                         "completed link_id=%d.\n",
2200                         dev->name, temp_ws_name,
2201                         p_ctlbk->linkid);
2202                         privptr->active_link_ID = p_ctlbk->linkid;
2203                         p_ch = &privptr->channel[WRITE];
2204                         wake_up(&p_ch->wait);  /* wake up claw_open ( WRITE) */
2205                 break;
2206         case CONNECTION_RESPONSE:
2207                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2208                 dev_info(tdev, "%s: Recv Conn Resp: Vers=%d,link_id=%d,"
2209                         "Corr=%d,RC=%d,Host appl=%.8s, WS appl=%.8s\n",
2210                         dev->name,
2211                         p_ctlbk->version,
2212                         p_ctlbk->linkid,
2213                         p_ctlbk->correlator,
2214                         p_ctlbk->rc,
2215                         p_connect->host_name,
2216                         p_connect->WS_name);
2217
2218                 if (p_ctlbk->rc != 0) {
2219                         dev_warn(tdev, "The communication peer of %s rejected"
2220                                 " a connection request\n",
2221                                 dev->name);
2222                         return 1;
2223                 }
2224                 rc = find_link(dev,
2225                         p_connect->host_name, p_connect->WS_name);
2226                 if (rc != 0) {
2227                         claw_snd_disc(dev, p_ctlbk);
2228                         dev_warn(tdev, "The communication peer of %s"
2229                                 " rejected a connection "
2230                                 "request because of a type mismatch\n",
2231                                  dev->name);
2232                 }
2233                 /* should be until CONNECTION_CONFIRM */
2234                 privptr->active_link_ID = -(p_ctlbk->linkid);
2235                 break;
2236         case CONNECTION_CONFIRM:
2237                 p_connect = (struct conncmd *)&(p_ctlbk->data);
2238                 dev_info(tdev,
2239                         "%s: Recv Conn Confirm:Vers=%d,link_id=%d,"
2240                         "Corr=%d,Host appl=%.8s,WS appl=%.8s\n",
2241                         dev->name,
2242                         p_ctlbk->version,
2243                         p_ctlbk->linkid,
2244                         p_ctlbk->correlator,
2245                         p_connect->host_name,
2246                         p_connect->WS_name);
2247                 if (p_ctlbk->linkid == -(privptr->active_link_ID)) {
2248                         privptr->active_link_ID = p_ctlbk->linkid;
2249                         if (p_env->packing > PACKING_ASK) {
2250                                 dev_info(tdev,
2251                                 "%s: Confirmed Now packing\n", dev->name);
2252                                 p_env->packing = DO_PACKED;
2253                         }
2254                         p_ch = &privptr->channel[WRITE];
2255                         wake_up(&p_ch->wait);
2256                 } else {
2257                         dev_warn(tdev, "Activating %s failed because of"
2258                                 " an incorrect link ID=%d\n",
2259                                 dev->name, p_ctlbk->linkid);
2260                         claw_snd_disc(dev, p_ctlbk);
2261                 }
2262                 break;
2263         case DISCONNECT:
2264                 dev_info(tdev, "%s: Disconnect: "
2265                         "Vers=%d,link_id=%d,Corr=%d\n",
2266                         dev->name, p_ctlbk->version,
2267                         p_ctlbk->linkid, p_ctlbk->correlator);
2268                 if ((p_ctlbk->linkid == 2) &&
2269                     (p_env->packing == PACK_SEND)) {
2270                         privptr->active_link_ID = 1;
2271                         p_env->packing = DO_PACKED;
2272                 } else
2273                         privptr->active_link_ID = 0;
2274                 break;
2275         case CLAW_ERROR:
2276                 dev_warn(tdev, "The communication peer of %s failed\n",
2277                         dev->name);
2278                 break;
2279         default:
2280                 dev_warn(tdev, "The communication peer of %s sent"
2281                         " an unknown command code\n",
2282                         dev->name);
2283                 break;
2284         }
2285
2286         return 0;
2287 }   /*    end of claw_process_control    */
2288
2289
2290 /*-------------------------------------------------------------------*
2291 *               claw_send_control                                    *
2292 *                                                                    *
2293 *--------------------------------------------------------------------*/
2294
2295 static int
2296 claw_send_control(struct net_device *dev, __u8 type, __u8 link,
2297          __u8 correlator, __u8 rc, char *local_name, char *remote_name)
2298 {
2299         struct claw_privbk              *privptr;
2300         struct clawctl                  *p_ctl;
2301         struct sysval                   *p_sysval;
2302         struct conncmd                  *p_connect;
2303         struct sk_buff                  *skb;
2304
2305         CLAW_DBF_TEXT(2, setup, "sndcntl");
2306         privptr = dev->ml_priv;
2307         p_ctl=(struct clawctl *)&privptr->ctl_bk;
2308
2309         p_ctl->command=type;
2310         p_ctl->version=CLAW_VERSION_ID;
2311         p_ctl->linkid=link;
2312         p_ctl->correlator=correlator;
2313         p_ctl->rc=rc;
2314
2315         p_sysval=(struct sysval *)&p_ctl->data;
2316         p_connect=(struct conncmd *)&p_ctl->data;
2317
2318         switch (p_ctl->command) {
2319                 case SYSTEM_VALIDATE_REQUEST:
2320                 case SYSTEM_VALIDATE_RESPONSE:
2321                         memcpy(&p_sysval->host_name, local_name, 8);
2322                         memcpy(&p_sysval->WS_name, remote_name, 8);
2323                         if (privptr->p_env->packing > 0) {
2324                                 p_sysval->read_frame_size = DEF_PACK_BUFSIZE;
2325                                 p_sysval->write_frame_size = DEF_PACK_BUFSIZE;
2326                         } else {
2327                                 /* how big is the biggest group of packets */
2328                            p_sysval->read_frame_size =
2329                                 privptr->p_env->read_size;
2330                            p_sysval->write_frame_size =
2331                                 privptr->p_env->write_size;
2332                         }
2333                         memset(&p_sysval->reserved, 0x00, 4);
2334                         break;
2335                 case CONNECTION_REQUEST:
2336                 case CONNECTION_RESPONSE:
2337                 case CONNECTION_CONFIRM:
2338                 case DISCONNECT:
2339                         memcpy(&p_sysval->host_name, local_name, 8);
2340                         memcpy(&p_sysval->WS_name, remote_name, 8);
2341                         if (privptr->p_env->packing > 0) {
2342                         /* How big is the biggest packet */
2343                                 p_connect->reserved1[0]=CLAW_FRAME_SIZE;
2344                                 p_connect->reserved1[1]=CLAW_FRAME_SIZE;
2345                         } else {
2346                                 memset(&p_connect->reserved1, 0x00, 4);
2347                                 memset(&p_connect->reserved2, 0x00, 4);
2348                         }
2349                         break;
2350                 default:
2351                         break;
2352         }
2353
2354         /*      write Control Record to the device                   */
2355
2356
2357         skb = dev_alloc_skb(sizeof(struct clawctl));
2358         if (!skb) {
2359                 return -ENOMEM;
2360         }
2361         memcpy(skb_put(skb, sizeof(struct clawctl)),
2362                 p_ctl, sizeof(struct clawctl));
2363         if (privptr->p_env->packing >= PACK_SEND)
2364                 claw_hw_tx(skb, dev, 1);
2365         else
2366                 claw_hw_tx(skb, dev, 0);
2367         return 0;
2368 }  /*   end of claw_send_control  */
2369
2370 /*-------------------------------------------------------------------*
2371 *               claw_snd_conn_req                                    *
2372 *                                                                    *
2373 *--------------------------------------------------------------------*/
2374 static int
2375 claw_snd_conn_req(struct net_device *dev, __u8 link)
2376 {
2377         int                rc;
2378         struct claw_privbk *privptr = dev->ml_priv;
2379         struct clawctl     *p_ctl;
2380
2381         CLAW_DBF_TEXT(2, setup, "snd_conn");
2382         rc = 1;
2383         p_ctl=(struct clawctl *)&privptr->ctl_bk;
2384         p_ctl->linkid = link;
2385         if ( privptr->system_validate_comp==0x00 ) {
2386                 return rc;
2387         }
2388         if (privptr->p_env->packing == PACKING_ASK )
2389                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2390                         WS_APPL_NAME_PACKED, WS_APPL_NAME_PACKED);
2391         if (privptr->p_env->packing == PACK_SEND)  {
2392                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2393                         WS_APPL_NAME_IP_NAME, WS_APPL_NAME_IP_NAME);
2394         }
2395         if (privptr->p_env->packing == 0)
2396                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
2397                         HOST_APPL_NAME, privptr->p_env->api_type);
2398         return rc;
2399
2400 }  /*  end of claw_snd_conn_req */
2401
2402
2403 /*-------------------------------------------------------------------*
2404 *               claw_snd_disc                                        *
2405 *                                                                    *
2406 *--------------------------------------------------------------------*/
2407
2408 static int
2409 claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl)
2410 {
2411         int rc;
2412         struct conncmd *  p_connect;
2413
2414         CLAW_DBF_TEXT(2, setup, "snd_dsc");
2415         p_connect=(struct conncmd *)&p_ctl->data;
2416
2417         rc=claw_send_control(dev, DISCONNECT, p_ctl->linkid,
2418                 p_ctl->correlator, 0,
2419                 p_connect->host_name, p_connect->WS_name);
2420         return rc;
2421 }     /*   end of claw_snd_disc    */
2422
2423
2424 /*-------------------------------------------------------------------*
2425 *               claw_snd_sys_validate_rsp                            *
2426 *                                                                    *
2427 *--------------------------------------------------------------------*/
2428
2429 static int
2430 claw_snd_sys_validate_rsp(struct net_device *dev,
2431         struct clawctl *p_ctl, __u32 return_code)
2432 {
2433         struct claw_env *  p_env;
2434         struct claw_privbk *privptr;
2435         int    rc;
2436
2437         CLAW_DBF_TEXT(2, setup, "chkresp");
2438         privptr = dev->ml_priv;
2439         p_env=privptr->p_env;
2440         rc=claw_send_control(dev, SYSTEM_VALIDATE_RESPONSE,
2441                 p_ctl->linkid,
2442                 p_ctl->correlator,
2443                 return_code,
2444                 p_env->host_name,
2445                 p_env->adapter_name  );
2446         return rc;
2447 }     /*    end of claw_snd_sys_validate_rsp    */
2448
2449 /*-------------------------------------------------------------------*
2450 *               claw_strt_conn_req                                   *
2451 *                                                                    *
2452 *--------------------------------------------------------------------*/
2453
2454 static int
2455 claw_strt_conn_req(struct net_device *dev )
2456 {
2457         int rc;
2458
2459         CLAW_DBF_TEXT(2, setup, "conn_req");
2460         rc=claw_snd_conn_req(dev, 1);
2461         return rc;
2462 }    /*   end of claw_strt_conn_req   */
2463
2464
2465
2466 /*-------------------------------------------------------------------*
2467  *   claw_stats                                                      *
2468  *-------------------------------------------------------------------*/
2469
2470 static struct
2471 net_device_stats *claw_stats(struct net_device *dev)
2472 {
2473         struct claw_privbk *privptr;
2474
2475         CLAW_DBF_TEXT(4, trace, "stats");
2476         privptr = dev->ml_priv;
2477         return &privptr->stats;
2478 }     /*   end of claw_stats   */
2479
2480
2481 /*-------------------------------------------------------------------*
2482 *       unpack_read                                                  *
2483 *                                                                    *
2484 *--------------------------------------------------------------------*/
2485 static void
2486 unpack_read(struct net_device *dev )
2487 {
2488         struct sk_buff *skb;
2489         struct claw_privbk *privptr;
2490         struct claw_env    *p_env;
2491         struct ccwbk    *p_this_ccw;
2492         struct ccwbk    *p_first_ccw;
2493         struct ccwbk    *p_last_ccw;
2494         struct clawph   *p_packh;
2495         void            *p_packd;
2496         struct clawctl  *p_ctlrec=NULL;
2497         struct device   *p_dev;
2498
2499         __u32   len_of_data;
2500         __u32   pack_off;
2501         __u8    link_num;
2502         __u8    mtc_this_frm=0;
2503         __u32   bytes_to_mov;
2504         int     i=0;
2505         int     p=0;
2506
2507         CLAW_DBF_TEXT(4, trace, "unpkread");
2508         p_first_ccw=NULL;
2509         p_last_ccw=NULL;
2510         p_packh=NULL;
2511         p_packd=NULL;
2512         privptr = dev->ml_priv;
2513
2514         p_dev = &privptr->channel[READ].cdev->dev;
2515         p_env = privptr->p_env;
2516         p_this_ccw=privptr->p_read_active_first;
2517         while (p_this_ccw!=NULL && p_this_ccw->header.flag!=CLAW_PENDING) {
2518                 pack_off = 0;
2519                 p = 0;
2520                 p_this_ccw->header.flag=CLAW_PENDING;
2521                 privptr->p_read_active_first=p_this_ccw->next;
2522                 p_this_ccw->next=NULL;
2523                 p_packh = (struct clawph *)p_this_ccw->p_buffer;
2524                 if ((p_env->packing == PACK_SEND) &&
2525                     (p_packh->len == 32)           &&
2526                     (p_packh->link_num == 0)) {   /* is it a packed ctl rec? */
2527                         p_packh++;  /* peek past pack header */
2528                         p_ctlrec = (struct clawctl *)p_packh;
2529                         p_packh--;  /* un peek */
2530                         if ((p_ctlrec->command == CONNECTION_RESPONSE) ||
2531                             (p_ctlrec->command == CONNECTION_CONFIRM))
2532                                 p_env->packing = DO_PACKED;
2533                 }
2534                 if (p_env->packing == DO_PACKED)
2535                         link_num=p_packh->link_num;
2536                 else
2537                         link_num=p_this_ccw->header.opcode / 8;
2538                 if ((p_this_ccw->header.opcode & MORE_to_COME_FLAG)!=0) {
2539                         mtc_this_frm=1;
2540                         if (p_this_ccw->header.length!=
2541                                 privptr->p_env->read_size ) {
2542                                 dev_warn(p_dev,
2543                                         "The communication peer of %s"
2544                                         " sent a faulty"
2545                                         " frame of length %02x\n",
2546                                         dev->name, p_this_ccw->header.length);
2547                         }
2548                 }
2549
2550                 if (privptr->mtc_skipping) {
2551                         /*
2552                         *   We're in the mode of skipping past a
2553                         *   multi-frame message
2554                         *   that we can't process for some reason or other.
2555                         *   The first frame without the More-To-Come flag is
2556                         *   the last frame of the skipped message.
2557                         */
2558                         /*  in case of More-To-Come not set in this frame */
2559                         if (mtc_this_frm==0) {
2560                                 privptr->mtc_skipping=0; /* Ok, the end */
2561                                 privptr->mtc_logical_link=-1;
2562                         }
2563                         goto NextFrame;
2564                 }
2565
2566                 if (link_num==0) {
2567                         claw_process_control(dev, p_this_ccw);
2568                         CLAW_DBF_TEXT(4, trace, "UnpkCntl");
2569                         goto NextFrame;
2570                 }
2571 unpack_next:
2572                 if (p_env->packing == DO_PACKED) {
2573                         if (pack_off > p_env->read_size)
2574                                 goto NextFrame;
2575                         p_packd = p_this_ccw->p_buffer+pack_off;
2576                         p_packh = (struct clawph *) p_packd;
2577                         if ((p_packh->len == 0) || /* done with this frame? */
2578                             (p_packh->flag != 0))
2579                                 goto NextFrame;
2580                         bytes_to_mov = p_packh->len;
2581                         pack_off += bytes_to_mov+sizeof(struct clawph);
2582                         p++;
2583                 } else {
2584                         bytes_to_mov=p_this_ccw->header.length;
2585                 }
2586                 if (privptr->mtc_logical_link<0) {
2587
2588                 /*
2589                 *  if More-To-Come is set in this frame then we don't know
2590                 *  length of entire message, and hence have to allocate
2591                 *  large buffer   */
2592
2593                 /*      We are starting a new envelope  */
2594                 privptr->mtc_offset=0;
2595                         privptr->mtc_logical_link=link_num;
2596                 }
2597
2598                 if (bytes_to_mov > (MAX_ENVELOPE_SIZE- privptr->mtc_offset) ) {
2599                         /*      error     */
2600                         privptr->stats.rx_frame_errors++;
2601                         goto NextFrame;
2602                 }
2603                 if (p_env->packing == DO_PACKED) {
2604                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
2605                                 p_packd+sizeof(struct clawph), bytes_to_mov);
2606
2607                 } else  {
2608                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
2609                                 p_this_ccw->p_buffer, bytes_to_mov);
2610                 }
2611                 if (mtc_this_frm==0) {
2612                         len_of_data=privptr->mtc_offset+bytes_to_mov;
2613                         skb=dev_alloc_skb(len_of_data);
2614                         if (skb) {
2615                                 memcpy(skb_put(skb,len_of_data),
2616                                         privptr->p_mtc_envelope,
2617                                         len_of_data);
2618                                 skb->dev=dev;
2619                                 skb_reset_mac_header(skb);
2620                                 skb->protocol=htons(ETH_P_IP);
2621                                 skb->ip_summed=CHECKSUM_UNNECESSARY;
2622                                 privptr->stats.rx_packets++;
2623                                 privptr->stats.rx_bytes+=len_of_data;
2624                                 netif_rx(skb);
2625                         }
2626                         else {
2627                                 dev_info(p_dev, "Allocating a buffer for"
2628                                         " incoming data failed\n");
2629                                 privptr->stats.rx_dropped++;
2630                         }
2631                         privptr->mtc_offset=0;
2632                         privptr->mtc_logical_link=-1;
2633                 }
2634                 else {
2635                         privptr->mtc_offset+=bytes_to_mov;
2636                 }
2637                 if (p_env->packing == DO_PACKED)
2638                         goto unpack_next;
2639 NextFrame:
2640                 /*
2641                 *   Remove ThisCCWblock from active read queue, and add it
2642                 *   to queue of free blocks to be reused.
2643                 */
2644                 i++;
2645                 p_this_ccw->header.length=0xffff;
2646                 p_this_ccw->header.opcode=0xff;
2647                 /*
2648                 *       add this one to the free queue for later reuse
2649                 */
2650                 if (p_first_ccw==NULL) {
2651                         p_first_ccw = p_this_ccw;
2652                 }
2653                 else {
2654                         p_last_ccw->next = p_this_ccw;
2655                 }
2656                 p_last_ccw = p_this_ccw;
2657                 /*
2658                 *       chain to next block on active read queue
2659                 */
2660                 p_this_ccw = privptr->p_read_active_first;
2661                 CLAW_DBF_TEXT_(4, trace, "rxpkt %d", p);
2662         } /* end of while */
2663
2664         /*      check validity                  */
2665
2666         CLAW_DBF_TEXT_(4, trace, "rxfrm %d", i);
2667         add_claw_reads(dev, p_first_ccw, p_last_ccw);
2668         claw_strt_read(dev, LOCK_YES);
2669         return;
2670 }     /*  end of unpack_read   */
2671
2672 /*-------------------------------------------------------------------*
2673 *       claw_strt_read                                               *
2674 *                                                                    *
2675 *--------------------------------------------------------------------*/
2676 static void
2677 claw_strt_read (struct net_device *dev, int lock )
2678 {
2679         int        rc = 0;
2680         __u32      parm;
2681         unsigned long  saveflags = 0;
2682         struct claw_privbk *privptr = dev->ml_priv;
2683         struct ccwbk*p_ccwbk;
2684         struct chbk *p_ch;
2685         struct clawh *p_clawh;
2686         p_ch=&privptr->channel[READ];
2687
2688         CLAW_DBF_TEXT(4, trace, "StRdNter");
2689         p_clawh=(struct clawh *)privptr->p_claw_signal_blk;
2690         p_clawh->flag=CLAW_IDLE;    /* 0x00 */
2691
2692         if ((privptr->p_write_active_first!=NULL &&
2693              privptr->p_write_active_first->header.flag!=CLAW_PENDING) ||
2694             (privptr->p_read_active_first!=NULL &&
2695              privptr->p_read_active_first->header.flag!=CLAW_PENDING )) {
2696                 p_clawh->flag=CLAW_BUSY;    /* 0xff */
2697         }
2698         if (lock==LOCK_YES) {
2699                 spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
2700         }
2701         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
2702                 CLAW_DBF_TEXT(4, trace, "HotRead");
2703                 p_ccwbk=privptr->p_read_active_first;
2704                 parm = (unsigned long) p_ch;
2705                 rc = ccw_device_start (p_ch->cdev, &p_ccwbk->read, parm,
2706                                        0xff, 0);
2707                 if (rc != 0) {
2708                         ccw_check_return_code(p_ch->cdev, rc);
2709                 }
2710         }
2711         else {
2712                 CLAW_DBF_TEXT(2, trace, "ReadAct");
2713         }
2714
2715         if (lock==LOCK_YES) {
2716                 spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
2717         }
2718         CLAW_DBF_TEXT(4, trace, "StRdExit");
2719         return;
2720 }       /*    end of claw_strt_read    */
2721
2722 /*-------------------------------------------------------------------*
2723 *       claw_strt_out_IO                                             *
2724 *                                                                    *
2725 *--------------------------------------------------------------------*/
2726
2727 static void
2728 claw_strt_out_IO( struct net_device *dev )
2729 {
2730         int                     rc = 0;
2731         unsigned long           parm;
2732         struct claw_privbk      *privptr;
2733         struct chbk             *p_ch;
2734         struct ccwbk    *p_first_ccw;
2735
2736         if (!dev) {
2737                 return;
2738         }
2739         privptr = (struct claw_privbk *)dev->ml_priv;
2740         p_ch=&privptr->channel[WRITE];
2741
2742         CLAW_DBF_TEXT(4, trace, "strt_io");
2743         p_first_ccw=privptr->p_write_active_first;
2744
2745         if (p_ch->claw_state == CLAW_STOP)
2746                 return;
2747         if (p_first_ccw == NULL) {
2748                 return;
2749         }
2750         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
2751                 parm = (unsigned long) p_ch;
2752                 CLAW_DBF_TEXT(2, trace, "StWrtIO");
2753                 rc = ccw_device_start(p_ch->cdev, &p_first_ccw->write, parm,
2754                                       0xff, 0);
2755                 if (rc != 0) {
2756                         ccw_check_return_code(p_ch->cdev, rc);
2757                 }
2758         }
2759         dev->trans_start = jiffies;
2760         return;
2761 }       /*    end of claw_strt_out_IO    */
2762
2763 /*-------------------------------------------------------------------*
2764 *       Free write buffers                                           *
2765 *                                                                    *
2766 *--------------------------------------------------------------------*/
2767
2768 static void
2769 claw_free_wrt_buf( struct net_device *dev )
2770 {
2771
2772         struct claw_privbk *privptr = (struct claw_privbk *)dev->ml_priv;
2773         struct ccwbk*p_first_ccw;
2774         struct ccwbk*p_last_ccw;
2775         struct ccwbk*p_this_ccw;
2776         struct ccwbk*p_next_ccw;
2777
2778         CLAW_DBF_TEXT(4, trace, "freewrtb");
2779         /*  scan the write queue to free any completed write packets   */
2780         p_first_ccw=NULL;
2781         p_last_ccw=NULL;
2782         p_this_ccw=privptr->p_write_active_first;
2783         while ( (p_this_ccw!=NULL) && (p_this_ccw->header.flag!=CLAW_PENDING))
2784         {
2785                 p_next_ccw = p_this_ccw->next;
2786                 if (((p_next_ccw!=NULL) &&
2787                      (p_next_ccw->header.flag!=CLAW_PENDING)) ||
2788                     ((p_this_ccw == privptr->p_write_active_last) &&
2789                      (p_this_ccw->header.flag!=CLAW_PENDING))) {
2790                         /* The next CCW is OK or this is  */
2791                         /* the last CCW...free it   @A1A  */
2792                         privptr->p_write_active_first=p_this_ccw->next;
2793                         p_this_ccw->header.flag=CLAW_PENDING;
2794                         p_this_ccw->next=privptr->p_write_free_chain;
2795                         privptr->p_write_free_chain=p_this_ccw;
2796                         ++privptr->write_free_count;
2797                         privptr->stats.tx_bytes+= p_this_ccw->write.count;
2798                         p_this_ccw=privptr->p_write_active_first;
2799                         privptr->stats.tx_packets++;
2800                 }
2801                 else {
2802                         break;
2803                 }
2804         }
2805         if (privptr->write_free_count!=0) {
2806                 claw_clearbit_busy(TB_NOBUFFER,dev);
2807         }
2808         /*   whole chain removed?   */
2809         if (privptr->p_write_active_first==NULL) {
2810                 privptr->p_write_active_last=NULL;
2811         }
2812         CLAW_DBF_TEXT_(4, trace, "FWC=%d", privptr->write_free_count);
2813         return;
2814 }
2815
2816 /*-------------------------------------------------------------------*
2817 *       claw free netdevice                                          *
2818 *                                                                    *
2819 *--------------------------------------------------------------------*/
2820 static void
2821 claw_free_netdevice(struct net_device * dev, int free_dev)
2822 {
2823         struct claw_privbk *privptr;
2824
2825         CLAW_DBF_TEXT(2, setup, "free_dev");
2826         if (!dev)
2827                 return;
2828         CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
2829         privptr = dev->ml_priv;
2830         if (dev->flags & IFF_RUNNING)
2831                 claw_release(dev);
2832         if (privptr) {
2833                 privptr->channel[READ].ndev = NULL;  /* say it's free */
2834         }
2835         dev->ml_priv = NULL;
2836 #ifdef MODULE
2837         if (free_dev) {
2838                 free_netdev(dev);
2839         }
2840 #endif
2841         CLAW_DBF_TEXT(2, setup, "free_ok");
2842 }
2843
2844 /**
2845  * Claw init netdevice
2846  * Initialize everything of the net device except the name and the
2847  * channel structs.
2848  */
2849 static const struct net_device_ops claw_netdev_ops = {
2850         .ndo_open               = claw_open,
2851         .ndo_stop               = claw_release,
2852         .ndo_get_stats          = claw_stats,
2853         .ndo_start_xmit         = claw_tx,
2854         .ndo_change_mtu         = claw_change_mtu,
2855 };
2856
2857 static void
2858 claw_init_netdevice(struct net_device * dev)
2859 {
2860         CLAW_DBF_TEXT(2, setup, "init_dev");
2861         CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
2862         dev->mtu = CLAW_DEFAULT_MTU_SIZE;
2863         dev->hard_header_len = 0;
2864         dev->addr_len = 0;
2865         dev->type = ARPHRD_SLIP;
2866         dev->tx_queue_len = 1300;
2867         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
2868         dev->netdev_ops = &claw_netdev_ops;
2869         CLAW_DBF_TEXT(2, setup, "initok");
2870         return;
2871 }
2872
2873 /**
2874  * Init a new channel in the privptr->channel[i].
2875  *
2876  * @param cdev  The ccw_device to be added.
2877  *
2878  * @return 0 on success, !0 on error.
2879  */
2880 static int
2881 add_channel(struct ccw_device *cdev,int i,struct claw_privbk *privptr)
2882 {
2883         struct chbk *p_ch;
2884         struct ccw_dev_id dev_id;
2885
2886         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cdev->dev));
2887         privptr->channel[i].flag  = i+1;   /* Read is 1 Write is 2 */
2888         p_ch = &privptr->channel[i];
2889         p_ch->cdev = cdev;
2890         snprintf(p_ch->id, CLAW_ID_SIZE, "cl-%s", dev_name(&cdev->dev));
2891         ccw_device_get_id(cdev, &dev_id);
2892         p_ch->devno = dev_id.devno;
2893         if ((p_ch->irb = kzalloc(sizeof (struct irb),GFP_KERNEL)) == NULL) {
2894                 return -ENOMEM;
2895         }
2896         return 0;
2897 }
2898
2899
2900 /**
2901  *
2902  * Setup an interface.
2903  *
2904  * @param cgdev  Device to be setup.
2905  *
2906  * @returns 0 on success, !0 on failure.
2907  */
2908 static int
2909 claw_new_device(struct ccwgroup_device *cgdev)
2910 {
2911         struct claw_privbk *privptr;
2912         struct claw_env *p_env;
2913         struct net_device *dev;
2914         int ret;
2915         struct ccw_dev_id dev_id;
2916
2917         dev_info(&cgdev->dev, "add for %s\n",
2918                  dev_name(&cgdev->cdev[READ]->dev));
2919         CLAW_DBF_TEXT(2, setup, "new_dev");
2920         privptr = dev_get_drvdata(&cgdev->dev);
2921         dev_set_drvdata(&cgdev->cdev[READ]->dev, privptr);
2922         dev_set_drvdata(&cgdev->cdev[WRITE]->dev, privptr);
2923         if (!privptr)
2924                 return -ENODEV;
2925         p_env = privptr->p_env;
2926         ccw_device_get_id(cgdev->cdev[READ], &dev_id);
2927         p_env->devno[READ] = dev_id.devno;
2928         ccw_device_get_id(cgdev->cdev[WRITE], &dev_id);
2929         p_env->devno[WRITE] = dev_id.devno;
2930         ret = add_channel(cgdev->cdev[0],0,privptr);
2931         if (ret == 0)
2932                 ret = add_channel(cgdev->cdev[1],1,privptr);
2933         if (ret != 0) {
2934                 dev_warn(&cgdev->dev, "Creating a CLAW group device"
2935                         " failed with error code %d\n", ret);
2936                 goto out;
2937         }
2938         ret = ccw_device_set_online(cgdev->cdev[READ]);
2939         if (ret != 0) {
2940                 dev_warn(&cgdev->dev,
2941                         "Setting the read subchannel online"
2942                         " failed with error code %d\n", ret);
2943                 goto out;
2944         }
2945         ret = ccw_device_set_online(cgdev->cdev[WRITE]);
2946         if (ret != 0) {
2947                 dev_warn(&cgdev->dev,
2948                         "Setting the write subchannel online "
2949                         "failed with error code %d\n", ret);
2950                 goto out;
2951         }
2952         dev = alloc_netdev(0,"claw%d",claw_init_netdevice);
2953         if (!dev) {
2954                 dev_warn(&cgdev->dev,
2955                         "Activating the CLAW device failed\n");
2956                 goto out;
2957         }
2958         dev->ml_priv = privptr;
2959         dev_set_drvdata(&cgdev->dev, privptr);
2960         dev_set_drvdata(&cgdev->cdev[READ]->dev, privptr);
2961         dev_set_drvdata(&cgdev->cdev[WRITE]->dev, privptr);
2962         /* sysfs magic */
2963         SET_NETDEV_DEV(dev, &cgdev->dev);
2964         if (register_netdev(dev) != 0) {
2965                 claw_free_netdevice(dev, 1);
2966                 CLAW_DBF_TEXT(2, trace, "regfail");
2967                 goto out;
2968         }
2969         dev->flags &=~IFF_RUNNING;
2970         if (privptr->buffs_alloc == 0) {
2971                 ret=init_ccw_bk(dev);
2972                 if (ret !=0) {
2973                         unregister_netdev(dev);
2974                         claw_free_netdevice(dev,1);
2975                         CLAW_DBF_TEXT(2, trace, "ccwmem");
2976                         goto out;
2977                 }
2978         }
2979         privptr->channel[READ].ndev = dev;
2980         privptr->channel[WRITE].ndev = dev;
2981         privptr->p_env->ndev = dev;
2982
2983         dev_info(&cgdev->dev, "%s:readsize=%d  writesize=%d "
2984                 "readbuffer=%d writebuffer=%d read=0x%04x write=0x%04x\n",
2985                 dev->name, p_env->read_size,
2986                 p_env->write_size, p_env->read_buffers,
2987                 p_env->write_buffers, p_env->devno[READ],
2988                 p_env->devno[WRITE]);
2989         dev_info(&cgdev->dev, "%s:host_name:%.8s, adapter_name "
2990                 ":%.8s api_type: %.8s\n",
2991                 dev->name, p_env->host_name,
2992                 p_env->adapter_name , p_env->api_type);
2993         return 0;
2994 out:
2995         ccw_device_set_offline(cgdev->cdev[1]);
2996         ccw_device_set_offline(cgdev->cdev[0]);
2997         return -ENODEV;
2998 }
2999
3000 static void
3001 claw_purge_skb_queue(struct sk_buff_head *q)
3002 {
3003         struct sk_buff *skb;
3004
3005         CLAW_DBF_TEXT(4, trace, "purgque");
3006         while ((skb = skb_dequeue(q))) {
3007                 atomic_dec(&skb->users);
3008                 dev_kfree_skb_any(skb);
3009         }
3010 }
3011
3012 /**
3013  * Shutdown an interface.
3014  *
3015  * @param cgdev  Device to be shut down.
3016  *
3017  * @returns 0 on success, !0 on failure.
3018  */
3019 static int
3020 claw_shutdown_device(struct ccwgroup_device *cgdev)
3021 {
3022         struct claw_privbk *priv;
3023         struct net_device *ndev;
3024         int     ret;
3025
3026         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3027         priv = dev_get_drvdata(&cgdev->dev);
3028         if (!priv)
3029                 return -ENODEV;
3030         ndev = priv->channel[READ].ndev;
3031         if (ndev) {
3032                 /* Close the device */
3033                 dev_info(&cgdev->dev, "%s: shutting down \n",
3034                         ndev->name);
3035                 if (ndev->flags & IFF_RUNNING)
3036                         ret = claw_release(ndev);
3037                 ndev->flags &=~IFF_RUNNING;
3038                 unregister_netdev(ndev);
3039                 ndev->ml_priv = NULL;  /* cgdev data, not ndev's to free */
3040                 claw_free_netdevice(ndev, 1);
3041                 priv->channel[READ].ndev = NULL;
3042                 priv->channel[WRITE].ndev = NULL;
3043                 priv->p_env->ndev = NULL;
3044         }
3045         ccw_device_set_offline(cgdev->cdev[1]);
3046         ccw_device_set_offline(cgdev->cdev[0]);
3047         return 0;
3048 }
3049
3050 static void
3051 claw_remove_device(struct ccwgroup_device *cgdev)
3052 {
3053         struct claw_privbk *priv;
3054
3055         BUG_ON(!cgdev);
3056         CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3057         priv = dev_get_drvdata(&cgdev->dev);
3058         BUG_ON(!priv);
3059         dev_info(&cgdev->dev, " will be removed.\n");
3060         if (cgdev->state == CCWGROUP_ONLINE)
3061                 claw_shutdown_device(cgdev);
3062         claw_remove_files(&cgdev->dev);
3063         kfree(priv->p_mtc_envelope);
3064         priv->p_mtc_envelope=NULL;
3065         kfree(priv->p_env);
3066         priv->p_env=NULL;
3067         kfree(priv->channel[0].irb);
3068         priv->channel[0].irb=NULL;
3069         kfree(priv->channel[1].irb);
3070         priv->channel[1].irb=NULL;
3071         kfree(priv);
3072         dev_set_drvdata(&cgdev->dev, NULL);
3073         dev_set_drvdata(&cgdev->cdev[READ]->dev, NULL);
3074         dev_set_drvdata(&cgdev->cdev[WRITE]->dev, NULL);
3075         put_device(&cgdev->dev);
3076
3077         return;
3078 }
3079
3080
3081 /*
3082  * sysfs attributes
3083  */
3084 static ssize_t
3085 claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf)
3086 {
3087         struct claw_privbk *priv;
3088         struct claw_env *  p_env;
3089
3090         priv = dev_get_drvdata(dev);
3091         if (!priv)
3092                 return -ENODEV;
3093         p_env = priv->p_env;
3094         return sprintf(buf, "%s\n",p_env->host_name);
3095 }
3096
3097 static ssize_t
3098 claw_hname_write(struct device *dev, struct device_attribute *attr,
3099          const char *buf, size_t count)
3100 {
3101         struct claw_privbk *priv;
3102         struct claw_env *  p_env;
3103
3104         priv = dev_get_drvdata(dev);
3105         if (!priv)
3106                 return -ENODEV;
3107         p_env = priv->p_env;
3108         if (count > MAX_NAME_LEN+1)
3109                 return -EINVAL;
3110         memset(p_env->host_name, 0x20, MAX_NAME_LEN);
3111         strncpy(p_env->host_name,buf, count);
3112         p_env->host_name[count-1] = 0x20;  /* clear extra 0x0a */
3113         p_env->host_name[MAX_NAME_LEN] = 0x00;
3114         CLAW_DBF_TEXT(2, setup, "HstnSet");
3115         CLAW_DBF_TEXT_(2, setup, "%s", p_env->host_name);
3116
3117         return count;
3118 }
3119
3120 static DEVICE_ATTR(host_name, 0644, claw_hname_show, claw_hname_write);
3121
3122 static ssize_t
3123 claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf)
3124 {
3125         struct claw_privbk *priv;
3126         struct claw_env *  p_env;
3127
3128         priv = dev_get_drvdata(dev);
3129         if (!priv)
3130                 return -ENODEV;
3131         p_env = priv->p_env;
3132         return sprintf(buf, "%s\n", p_env->adapter_name);
3133 }
3134
3135 static ssize_t
3136 claw_adname_write(struct device *dev, struct device_attribute *attr,
3137          const char *buf, size_t count)
3138 {
3139         struct claw_privbk *priv;
3140         struct claw_env *  p_env;
3141
3142         priv = dev_get_drvdata(dev);
3143         if (!priv)
3144                 return -ENODEV;
3145         p_env = priv->p_env;
3146         if (count > MAX_NAME_LEN+1)
3147                 return -EINVAL;
3148         memset(p_env->adapter_name, 0x20, MAX_NAME_LEN);
3149         strncpy(p_env->adapter_name,buf, count);
3150         p_env->adapter_name[count-1] = 0x20; /* clear extra 0x0a */
3151         p_env->adapter_name[MAX_NAME_LEN] = 0x00;
3152         CLAW_DBF_TEXT(2, setup, "AdnSet");
3153         CLAW_DBF_TEXT_(2, setup, "%s", p_env->adapter_name);
3154
3155         return count;
3156 }
3157
3158 static DEVICE_ATTR(adapter_name, 0644, claw_adname_show, claw_adname_write);
3159
3160 static ssize_t
3161 claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf)
3162 {
3163         struct claw_privbk *priv;
3164         struct claw_env *  p_env;
3165
3166         priv = dev_get_drvdata(dev);
3167         if (!priv)
3168                 return -ENODEV;
3169         p_env = priv->p_env;
3170         return sprintf(buf, "%s\n",
3171                        p_env->api_type);
3172 }
3173
3174 static ssize_t
3175 claw_apname_write(struct device *dev, struct device_attribute *attr,
3176         const char *buf, size_t count)
3177 {
3178         struct claw_privbk *priv;
3179         struct claw_env *  p_env;
3180
3181         priv = dev_get_drvdata(dev);
3182         if (!priv)
3183                 return -ENODEV;
3184         p_env = priv->p_env;
3185         if (count > MAX_NAME_LEN+1)
3186                 return -EINVAL;
3187         memset(p_env->api_type, 0x20, MAX_NAME_LEN);
3188         strncpy(p_env->api_type,buf, count);
3189         p_env->api_type[count-1] = 0x20;  /* we get a loose 0x0a */
3190         p_env->api_type[MAX_NAME_LEN] = 0x00;
3191         if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
3192                 p_env->read_size=DEF_PACK_BUFSIZE;
3193                 p_env->write_size=DEF_PACK_BUFSIZE;
3194                 p_env->packing=PACKING_ASK;
3195                 CLAW_DBF_TEXT(2, setup, "PACKING");
3196         }
3197         else {
3198                 p_env->packing=0;
3199                 p_env->read_size=CLAW_FRAME_SIZE;
3200                 p_env->write_size=CLAW_FRAME_SIZE;
3201                 CLAW_DBF_TEXT(2, setup, "ApiSet");
3202         }
3203         CLAW_DBF_TEXT_(2, setup, "%s", p_env->api_type);
3204         return count;
3205 }
3206
3207 static DEVICE_ATTR(api_type, 0644, claw_apname_show, claw_apname_write);
3208
3209 static ssize_t
3210 claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
3211 {
3212         struct claw_privbk *priv;
3213         struct claw_env * p_env;
3214
3215         priv = dev_get_drvdata(dev);
3216         if (!priv)
3217                 return -ENODEV;
3218         p_env = priv->p_env;
3219         return sprintf(buf, "%d\n", p_env->write_buffers);
3220 }
3221
3222 static ssize_t
3223 claw_wbuff_write(struct device *dev, struct device_attribute *attr,
3224         const char *buf, size_t count)
3225 {
3226         struct claw_privbk *priv;
3227         struct claw_env *  p_env;
3228         int nnn,max;
3229
3230         priv = dev_get_drvdata(dev);
3231         if (!priv)
3232                 return -ENODEV;
3233         p_env = priv->p_env;
3234         sscanf(buf, "%i", &nnn);
3235         if (p_env->packing) {
3236                 max = 64;
3237         }
3238         else {
3239                 max = 512;
3240         }
3241         if ((nnn > max ) || (nnn < 2))
3242                 return -EINVAL;
3243         p_env->write_buffers = nnn;
3244         CLAW_DBF_TEXT(2, setup, "Wbufset");
3245         CLAW_DBF_TEXT_(2, setup, "WB=%d", p_env->write_buffers);
3246         return count;
3247 }
3248
3249 static DEVICE_ATTR(write_buffer, 0644, claw_wbuff_show, claw_wbuff_write);
3250
3251 static ssize_t
3252 claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
3253 {
3254         struct claw_privbk *priv;
3255         struct claw_env *  p_env;
3256
3257         priv = dev_get_drvdata(dev);
3258         if (!priv)
3259                 return -ENODEV;
3260         p_env = priv->p_env;
3261         return sprintf(buf, "%d\n", p_env->read_buffers);
3262 }
3263
3264 static ssize_t
3265 claw_rbuff_write(struct device *dev, struct device_attribute *attr,
3266         const char *buf, size_t count)
3267 {
3268         struct claw_privbk *priv;
3269         struct claw_env *p_env;
3270         int nnn,max;
3271
3272         priv = dev_get_drvdata(dev);
3273         if (!priv)
3274                 return -ENODEV;
3275         p_env = priv->p_env;
3276         sscanf(buf, "%i", &nnn);
3277         if (p_env->packing) {
3278                 max = 64;
3279         }
3280         else {
3281                 max = 512;
3282         }
3283         if ((nnn > max ) || (nnn < 2))
3284                 return -EINVAL;
3285         p_env->read_buffers = nnn;
3286         CLAW_DBF_TEXT(2, setup, "Rbufset");
3287         CLAW_DBF_TEXT_(2, setup, "RB=%d", p_env->read_buffers);
3288         return count;
3289 }
3290
3291 static DEVICE_ATTR(read_buffer, 0644, claw_rbuff_show, claw_rbuff_write);
3292
3293 static struct attribute *claw_attr[] = {
3294         &dev_attr_read_buffer.attr,
3295         &dev_attr_write_buffer.attr,
3296         &dev_attr_adapter_name.attr,
3297         &dev_attr_api_type.attr,
3298         &dev_attr_host_name.attr,
3299         NULL,
3300 };
3301
3302 static struct attribute_group claw_attr_group = {
3303         .attrs = claw_attr,
3304 };
3305
3306 static int
3307 claw_add_files(struct device *dev)
3308 {
3309         CLAW_DBF_TEXT(2, setup, "add_file");
3310         return sysfs_create_group(&dev->kobj, &claw_attr_group);
3311 }
3312
3313 static void
3314 claw_remove_files(struct device *dev)
3315 {
3316         CLAW_DBF_TEXT(2, setup, "rem_file");
3317         sysfs_remove_group(&dev->kobj, &claw_attr_group);
3318 }
3319
3320 /*--------------------------------------------------------------------*
3321 *    claw_init  and cleanup                                           *
3322 *---------------------------------------------------------------------*/
3323
3324 static void __exit
3325 claw_cleanup(void)
3326 {
3327         unregister_cu3088_discipline(&claw_group_driver);
3328         claw_unregister_debug_facility();
3329         pr_info("Driver unloaded\n");
3330
3331 }
3332
3333 /**
3334  * Initialize module.
3335  * This is called just after the module is loaded.
3336  *
3337  * @return 0 on success, !0 on error.
3338  */
3339 static int __init
3340 claw_init(void)
3341 {
3342         int ret = 0;
3343
3344         pr_info("Loading %s\n", version);
3345         ret = claw_register_debug_facility();
3346         if (ret) {
3347                 pr_err("Registering with the S/390 debug feature"
3348                         " failed with error code %d\n", ret);
3349                 return ret;
3350         }
3351         CLAW_DBF_TEXT(2, setup, "init_mod");
3352         ret = register_cu3088_discipline(&claw_group_driver);
3353         if (ret) {
3354                 CLAW_DBF_TEXT(2, setup, "init_bad");
3355                 claw_unregister_debug_facility();
3356                 pr_err("Registering with the cu3088 device driver failed "
3357                            "with error code %d\n", ret);
3358         }
3359         return ret;
3360 }
3361
3362 module_init(claw_init);
3363 module_exit(claw_cleanup);
3364
3365 MODULE_AUTHOR("Andy Richter <richtera@us.ibm.com>");
3366 MODULE_DESCRIPTION("Linux for System z CLAW Driver\n" \
3367                         "Copyright 2000,2008 IBM Corporation\n");
3368 MODULE_LICENSE("GPL");