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