Merge branch 'sched/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[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  */
62 #include <asm/ccwdev.h>
63 #include <asm/ccwgroup.h>
64 #include <asm/debug.h>
65 #include <asm/idals.h>
66 #include <asm/io.h>
67
68 #include <linux/bitops.h>
69 #include <linux/ctype.h>
70 #include <linux/delay.h>
71 #include <linux/errno.h>
72 #include <linux/if_arp.h>
73 #include <linux/init.h>
74 #include <linux/interrupt.h>
75 #include <linux/ip.h>
76 #include <linux/kernel.h>
77 #include <linux/module.h>
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/proc_fs.h>
81 #include <linux/sched.h>
82 #include <linux/signal.h>
83 #include <linux/skbuff.h>
84 #include <linux/slab.h>
85 #include <linux/string.h>
86 #include <linux/tcp.h>
87 #include <linux/timer.h>
88 #include <linux/types.h>
89
90 #include "cu3088.h"
91 #include "claw.h"
92
93 MODULE_AUTHOR("Andy Richter <richtera@us.ibm.com>");
94 MODULE_DESCRIPTION("Linux for zSeries CLAW Driver\n" \
95                         "Copyright 2000,2005 IBM Corporation\n");
96 MODULE_LICENSE("GPL");
97
98 /* Debugging is based on DEBUGMSG, IOTRACE, or FUNCTRACE  options:
99    DEBUGMSG  - Enables output of various debug messages in the code
100    IOTRACE   - Enables output of CCW and other IO related traces
101    FUNCTRACE - Enables output of function entry/exit trace
102    Define any combination of above options to enable tracing
103
104    CLAW also uses the s390dbf file system  see claw_trace and claw_setup
105 */
106
107 /* following enables tracing */
108 //#define DEBUGMSG
109 //#define IOTRACE
110 //#define FUNCTRACE
111
112 #ifdef DEBUGMSG
113 #define DEBUG
114 #endif
115
116 #ifdef IOTRACE
117 #define DEBUG
118 #endif
119
120 #ifdef FUNCTRACE
121 #define DEBUG
122 #endif
123
124 static char debug_buffer[255];
125 /**
126  * Debug Facility Stuff
127  */
128 static debug_info_t *claw_dbf_setup;
129 static debug_info_t *claw_dbf_trace;
130
131 /**
132  *  CLAW Debug Facility functions
133  */
134 static void
135 claw_unregister_debug_facility(void)
136 {
137         if (claw_dbf_setup)
138                 debug_unregister(claw_dbf_setup);
139         if (claw_dbf_trace)
140                 debug_unregister(claw_dbf_trace);
141 }
142
143 static int
144 claw_register_debug_facility(void)
145 {
146         claw_dbf_setup = debug_register("claw_setup", 2, 1, 8);
147         claw_dbf_trace = debug_register("claw_trace", 2, 2, 8);
148         if (claw_dbf_setup == NULL || claw_dbf_trace == NULL) {
149                 printk(KERN_WARNING "Not enough memory for debug facility.\n");
150                 claw_unregister_debug_facility();
151                 return -ENOMEM;
152         }
153         debug_register_view(claw_dbf_setup, &debug_hex_ascii_view);
154         debug_set_level(claw_dbf_setup, 2);
155         debug_register_view(claw_dbf_trace, &debug_hex_ascii_view);
156         debug_set_level(claw_dbf_trace, 2);
157         return 0;
158 }
159
160 static inline void
161 claw_set_busy(struct net_device *dev)
162 {
163  ((struct claw_privbk *) dev->priv)->tbusy=1;
164  eieio();
165 }
166
167 static inline void
168 claw_clear_busy(struct net_device *dev)
169 {
170         clear_bit(0, &(((struct claw_privbk *) dev->priv)->tbusy));
171         netif_wake_queue(dev);
172         eieio();
173 }
174
175 static inline int
176 claw_check_busy(struct net_device *dev)
177 {
178         eieio();
179         return ((struct claw_privbk *) dev->priv)->tbusy;
180 }
181
182 static inline void
183 claw_setbit_busy(int nr,struct net_device *dev)
184 {
185         netif_stop_queue(dev);
186         set_bit(nr, (void *)&(((struct claw_privbk *)dev->priv)->tbusy));
187 }
188
189 static inline void
190 claw_clearbit_busy(int nr,struct net_device *dev)
191 {
192         clear_bit(nr,(void *)&(((struct claw_privbk *)dev->priv)->tbusy));
193         netif_wake_queue(dev);
194 }
195
196 static inline int
197 claw_test_and_setbit_busy(int nr,struct net_device *dev)
198 {
199         netif_stop_queue(dev);
200         return test_and_set_bit(nr,
201                 (void *)&(((struct claw_privbk *) dev->priv)->tbusy));
202 }
203
204
205 /* Functions for the DEV methods */
206
207 static int claw_probe(struct ccwgroup_device *cgdev);
208 static void claw_remove_device(struct ccwgroup_device *cgdev);
209 static void claw_purge_skb_queue(struct sk_buff_head *q);
210 static int claw_new_device(struct ccwgroup_device *cgdev);
211 static int claw_shutdown_device(struct ccwgroup_device *cgdev);
212 static int claw_tx(struct sk_buff *skb, struct net_device *dev);
213 static int claw_change_mtu( struct net_device *dev, int new_mtu);
214 static int claw_open(struct net_device *dev);
215 static void claw_irq_handler(struct ccw_device *cdev,
216         unsigned long intparm, struct irb *irb);
217 static void claw_irq_tasklet ( unsigned long data );
218 static int claw_release(struct net_device *dev);
219 static void claw_write_retry ( struct chbk * p_ch );
220 static void claw_write_next ( struct chbk * p_ch );
221 static void claw_timer ( struct chbk * p_ch );
222
223 /* Functions */
224 static int add_claw_reads(struct net_device *dev,
225         struct ccwbk* p_first, struct ccwbk* p_last);
226 static void ccw_check_return_code (struct ccw_device *cdev, int return_code);
227 static void ccw_check_unit_check (struct chbk * p_ch, unsigned char sense );
228 static int find_link(struct net_device *dev, char *host_name, char *ws_name );
229 static int claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid);
230 static int init_ccw_bk(struct net_device *dev);
231 static void probe_error( struct ccwgroup_device *cgdev);
232 static struct net_device_stats *claw_stats(struct net_device *dev);
233 static int pages_to_order_of_mag(int num_of_pages);
234 static struct sk_buff *claw_pack_skb(struct claw_privbk *privptr);
235 #ifdef DEBUG
236 static void dumpit (char *buf, int len);
237 #endif
238 /* sysfs Functions */
239 static ssize_t claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf);
240 static ssize_t claw_hname_write(struct device *dev, struct device_attribute *attr,
241         const char *buf, size_t count);
242 static ssize_t claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf);
243 static ssize_t claw_adname_write(struct device *dev, struct device_attribute *attr,
244         const char *buf, size_t count);
245 static ssize_t claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf);
246 static ssize_t claw_apname_write(struct device *dev, struct device_attribute *attr,
247         const char *buf, size_t count);
248 static ssize_t claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf);
249 static ssize_t claw_wbuff_write(struct device *dev, struct device_attribute *attr,
250         const char *buf, size_t count);
251 static ssize_t claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf);
252 static ssize_t claw_rbuff_write(struct device *dev, struct device_attribute *attr,
253         const char *buf, size_t count);
254 static int claw_add_files(struct device *dev);
255 static void claw_remove_files(struct device *dev);
256
257 /*   Functions for System Validate  */
258 static int claw_process_control( struct net_device *dev, struct ccwbk * p_ccw);
259 static int claw_send_control(struct net_device *dev, __u8 type, __u8 link,
260        __u8 correlator, __u8 rc , char *local_name, char *remote_name);
261 static int claw_snd_conn_req(struct net_device *dev, __u8 link);
262 static int claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl);
263 static int claw_snd_sys_validate_rsp(struct net_device *dev,
264         struct clawctl * p_ctl, __u32 return_code);
265 static int claw_strt_conn_req(struct net_device *dev );
266 static void claw_strt_read ( struct net_device *dev, int lock );
267 static void claw_strt_out_IO( struct net_device *dev );
268 static void claw_free_wrt_buf( struct net_device *dev );
269
270 /* Functions for unpack reads   */
271 static void unpack_read (struct net_device *dev );
272
273 /* ccwgroup table  */
274
275 static struct ccwgroup_driver claw_group_driver = {
276         .owner       = THIS_MODULE,
277         .name        = "claw",
278         .max_slaves  = 2,
279         .driver_id   = 0xC3D3C1E6,
280         .probe       = claw_probe,
281         .remove      = claw_remove_device,
282         .set_online  = claw_new_device,
283         .set_offline = claw_shutdown_device,
284 };
285
286 /*
287 *
288 *       Key functions
289 */
290
291 /*----------------------------------------------------------------*
292  *   claw_probe                                                   *
293  *      this function is called for each CLAW device.             *
294  *----------------------------------------------------------------*/
295 static int
296 claw_probe(struct ccwgroup_device *cgdev)
297 {
298         int             rc;
299         struct claw_privbk *privptr=NULL;
300
301 #ifdef FUNCTRACE
302         printk(KERN_INFO "%s Enter\n",__func__);
303 #endif
304         CLAW_DBF_TEXT(2,setup,"probe");
305         if (!get_device(&cgdev->dev))
306                 return -ENODEV;
307 #ifdef DEBUGMSG
308         printk(KERN_INFO "claw: variable cgdev =\n");
309         dumpit((char *)cgdev, sizeof(struct ccwgroup_device));
310 #endif
311         privptr = kzalloc(sizeof(struct claw_privbk), GFP_KERNEL);
312         if (privptr == NULL) {
313                 probe_error(cgdev);
314                 put_device(&cgdev->dev);
315                 printk(KERN_WARNING "Out of memory %s %s Exit Line %d \n",
316                         cgdev->cdev[0]->dev.bus_id,__func__,__LINE__);
317                 CLAW_DBF_TEXT_(2,setup,"probex%d",-ENOMEM);
318                 return -ENOMEM;
319         }
320         privptr->p_mtc_envelope= kzalloc( MAX_ENVELOPE_SIZE, GFP_KERNEL);
321         privptr->p_env = kzalloc(sizeof(struct claw_env), GFP_KERNEL);
322         if ((privptr->p_mtc_envelope==NULL) || (privptr->p_env==NULL)) {
323                 probe_error(cgdev);
324                 put_device(&cgdev->dev);
325                 printk(KERN_WARNING "Out of memory %s %s Exit Line %d \n",
326                         cgdev->cdev[0]->dev.bus_id,__func__,__LINE__);
327                 CLAW_DBF_TEXT_(2,setup,"probex%d",-ENOMEM);
328                 return -ENOMEM;
329         }
330         memcpy(privptr->p_env->adapter_name,WS_NAME_NOT_DEF,8);
331         memcpy(privptr->p_env->host_name,WS_NAME_NOT_DEF,8);
332         memcpy(privptr->p_env->api_type,WS_NAME_NOT_DEF,8);
333         privptr->p_env->packing = 0;
334         privptr->p_env->write_buffers = 5;
335         privptr->p_env->read_buffers = 5;
336         privptr->p_env->read_size = CLAW_FRAME_SIZE;
337         privptr->p_env->write_size = CLAW_FRAME_SIZE;
338         rc = claw_add_files(&cgdev->dev);
339         if (rc) {
340                 probe_error(cgdev);
341                 put_device(&cgdev->dev);
342                 printk(KERN_WARNING "add_files failed %s %s Exit Line %d \n",
343                         cgdev->cdev[0]->dev.bus_id,__func__,__LINE__);
344                 CLAW_DBF_TEXT_(2,setup,"probex%d",rc);
345                 return rc;
346         }
347         printk(KERN_INFO "claw: sysfs files added for %s\n",cgdev->cdev[0]->dev.bus_id);
348         privptr->p_env->p_priv = privptr;
349         cgdev->cdev[0]->handler = claw_irq_handler;
350         cgdev->cdev[1]->handler = claw_irq_handler;
351         cgdev->dev.driver_data = privptr;
352 #ifdef FUNCTRACE
353         printk(KERN_INFO "claw:%s exit on line %d, "
354                 "rc = 0\n",__func__,__LINE__);
355 #endif
356         CLAW_DBF_TEXT(2,setup,"prbext 0");
357
358         return 0;
359 }  /*  end of claw_probe       */
360
361 /*-------------------------------------------------------------------*
362  *   claw_tx                                                         *
363  *-------------------------------------------------------------------*/
364
365 static int
366 claw_tx(struct sk_buff *skb, struct net_device *dev)
367 {
368         int             rc;
369         struct claw_privbk *privptr=dev->priv;
370         unsigned long saveflags;
371         struct chbk *p_ch;
372
373 #ifdef FUNCTRACE
374         printk(KERN_INFO "%s:%s enter\n",dev->name,__func__);
375 #endif
376         CLAW_DBF_TEXT(4,trace,"claw_tx");
377         p_ch=&privptr->channel[WRITE];
378         if (skb == NULL) {
379                 printk(KERN_WARNING "%s: null pointer passed as sk_buffer\n",
380                         dev->name);
381                 privptr->stats.tx_dropped++;
382 #ifdef FUNCTRACE
383                 printk(KERN_INFO "%s: %s() exit on line %d, rc = EIO\n",
384                         dev->name,__func__, __LINE__);
385 #endif
386                 CLAW_DBF_TEXT_(2,trace,"clawtx%d",-EIO);
387                 return -EIO;
388         }
389
390 #ifdef IOTRACE
391         printk(KERN_INFO "%s: variable sk_buff=\n",dev->name);
392         dumpit((char *) skb, sizeof(struct sk_buff));
393         printk(KERN_INFO "%s: variable dev=\n",dev->name);
394         dumpit((char *) dev, sizeof(struct net_device));
395 #endif
396         spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
397         rc=claw_hw_tx( skb, dev, 1 );
398         spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
399 #ifdef FUNCTRACE
400         printk(KERN_INFO "%s:%s exit on line %d, rc = %d\n",
401                 dev->name, __func__, __LINE__, rc);
402 #endif
403         CLAW_DBF_TEXT_(4,trace,"clawtx%d",rc);
404         return rc;
405 }   /*  end of claw_tx */
406
407 /*------------------------------------------------------------------*
408  *  pack the collect queue into an skb and return it                *
409  *   If not packing just return the top skb from the queue          *
410  *------------------------------------------------------------------*/
411
412 static struct sk_buff *
413 claw_pack_skb(struct claw_privbk *privptr)
414 {
415         struct sk_buff *new_skb,*held_skb;
416         struct chbk *p_ch = &privptr->channel[WRITE];
417         struct claw_env  *p_env = privptr->p_env;
418         int     pkt_cnt,pk_ind,so_far;
419
420         new_skb = NULL;         /* assume no dice */
421         pkt_cnt = 0;
422         CLAW_DBF_TEXT(4,trace,"PackSKBe");
423         if (!skb_queue_empty(&p_ch->collect_queue)) {
424         /* some data */
425                 held_skb = skb_dequeue(&p_ch->collect_queue);
426                 if (held_skb)
427                         dev_kfree_skb_any(held_skb);
428                 else
429                         return NULL;
430                 if (p_env->packing != DO_PACKED)
431                         return held_skb;
432                 /* get a new SKB we will pack at least one */
433                 new_skb = dev_alloc_skb(p_env->write_size);
434                 if (new_skb == NULL) {
435                         atomic_inc(&held_skb->users);
436                         skb_queue_head(&p_ch->collect_queue,held_skb);
437                         return NULL;
438                 }
439                 /* we have packed packet and a place to put it  */
440                 pk_ind = 1;
441                 so_far = 0;
442                 new_skb->cb[1] = 'P'; /* every skb on queue has pack header */
443                 while ((pk_ind) && (held_skb != NULL)) {
444                         if (held_skb->len+so_far <= p_env->write_size-8) {
445                                 memcpy(skb_put(new_skb,held_skb->len),
446                                         held_skb->data,held_skb->len);
447                                 privptr->stats.tx_packets++;
448                                 so_far += held_skb->len;
449                                 pkt_cnt++;
450                                 dev_kfree_skb_any(held_skb);
451                                 held_skb = skb_dequeue(&p_ch->collect_queue);
452                                 if (held_skb)
453                                         atomic_dec(&held_skb->users);
454                         } else {
455                                 pk_ind = 0;
456                                 atomic_inc(&held_skb->users);
457                                 skb_queue_head(&p_ch->collect_queue,held_skb);
458                         }
459                 }
460 #ifdef IOTRACE
461                 printk(KERN_INFO "%s: %s() Packed %d len %d\n",
462                         p_env->ndev->name,
463                         __func__,pkt_cnt,new_skb->len);
464 #endif
465         }
466         CLAW_DBF_TEXT(4,trace,"PackSKBx");
467         return new_skb;
468 }
469
470 /*-------------------------------------------------------------------*
471  *   claw_change_mtu                                                 *
472  *                                                                   *
473  *-------------------------------------------------------------------*/
474
475 static int
476 claw_change_mtu(struct net_device *dev, int new_mtu)
477 {
478         struct claw_privbk  *privptr=dev->priv;
479         int buff_size;
480 #ifdef FUNCTRACE
481         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
482 #endif
483 #ifdef DEBUGMSG
484         printk(KERN_INFO "variable dev =\n");
485         dumpit((char *) dev, sizeof(struct net_device));
486         printk(KERN_INFO "variable new_mtu = %d\n", new_mtu);
487 #endif
488         CLAW_DBF_TEXT(4,trace,"setmtu");
489         buff_size = privptr->p_env->write_size;
490         if ((new_mtu < 60) || (new_mtu > buff_size)) {
491 #ifdef FUNCTRACE
492                 printk(KERN_INFO "%s:%s Exit on line %d, rc=EINVAL\n",
493                 dev->name,
494                 __func__, __LINE__);
495 #endif
496                 return -EINVAL;
497         }
498         dev->mtu = new_mtu;
499 #ifdef FUNCTRACE
500         printk(KERN_INFO "%s:%s Exit on line %d\n",dev->name,
501         __func__, __LINE__);
502 #endif
503         return 0;
504 }  /*   end of claw_change_mtu */
505
506
507 /*-------------------------------------------------------------------*
508  *   claw_open                                                       *
509  *                                                                   *
510  *-------------------------------------------------------------------*/
511 static int
512 claw_open(struct net_device *dev)
513 {
514
515         int     rc;
516         int     i;
517         unsigned long       saveflags=0;
518         unsigned long       parm;
519         struct claw_privbk  *privptr;
520         DECLARE_WAITQUEUE(wait, current);
521         struct timer_list  timer;
522         struct ccwbk *p_buf;
523
524 #ifdef FUNCTRACE
525         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
526 #endif
527         CLAW_DBF_TEXT(4,trace,"open");
528         if (!dev || (dev->name[0] == 0x00)) {
529                 CLAW_DBF_TEXT(2,trace,"BadDev");
530                 printk(KERN_WARNING "claw: Bad device at open failing \n");
531                 return -ENODEV;
532         }
533         privptr = (struct claw_privbk *)dev->priv;
534         /*   allocate and initialize CCW blocks */
535         if (privptr->buffs_alloc == 0) {
536                 rc=init_ccw_bk(dev);
537                 if (rc) {
538                         printk(KERN_INFO "%s:%s Exit on line %d, rc=ENOMEM\n",
539                         dev->name,
540                         __func__, __LINE__);
541                         CLAW_DBF_TEXT(2,trace,"openmem");
542                         return -ENOMEM;
543                 }
544         }
545         privptr->system_validate_comp=0;
546         privptr->release_pend=0;
547         if(strncmp(privptr->p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
548                 privptr->p_env->read_size=DEF_PACK_BUFSIZE;
549                 privptr->p_env->write_size=DEF_PACK_BUFSIZE;
550                 privptr->p_env->packing=PACKING_ASK;
551         } else {
552                 privptr->p_env->packing=0;
553                 privptr->p_env->read_size=CLAW_FRAME_SIZE;
554                 privptr->p_env->write_size=CLAW_FRAME_SIZE;
555         }
556         claw_set_busy(dev);
557         tasklet_init(&privptr->channel[READ].tasklet, claw_irq_tasklet,
558                 (unsigned long) &privptr->channel[READ]);
559         for ( i = 0; i < 2;  i++) {
560                 CLAW_DBF_TEXT_(2,trace,"opn_ch%d",i);
561                 init_waitqueue_head(&privptr->channel[i].wait);
562                 /* skb_queue_head_init(&p_ch->io_queue); */
563                 if (i == WRITE)
564                         skb_queue_head_init(
565                                 &privptr->channel[WRITE].collect_queue);
566                 privptr->channel[i].flag_a = 0;
567                 privptr->channel[i].IO_active = 0;
568                 privptr->channel[i].flag  &= ~CLAW_TIMER;
569                 init_timer(&timer);
570                 timer.function = (void *)claw_timer;
571                 timer.data = (unsigned long)(&privptr->channel[i]);
572                 timer.expires = jiffies + 15*HZ;
573                 add_timer(&timer);
574                 spin_lock_irqsave(get_ccwdev_lock(
575                         privptr->channel[i].cdev), saveflags);
576                 parm = (unsigned long) &privptr->channel[i];
577                 privptr->channel[i].claw_state = CLAW_START_HALT_IO;
578                 rc = 0;
579                 add_wait_queue(&privptr->channel[i].wait, &wait);
580                 rc = ccw_device_halt(
581                         (struct ccw_device *)privptr->channel[i].cdev,parm);
582                 set_current_state(TASK_INTERRUPTIBLE);
583                 spin_unlock_irqrestore(
584                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
585                 schedule();
586                 set_current_state(TASK_RUNNING);
587                 remove_wait_queue(&privptr->channel[i].wait, &wait);
588                 if(rc != 0)
589                         ccw_check_return_code(privptr->channel[i].cdev, rc);
590                 if((privptr->channel[i].flag & CLAW_TIMER) == 0x00)
591                         del_timer(&timer);
592         }
593         if ((((privptr->channel[READ].last_dstat |
594                 privptr->channel[WRITE].last_dstat) &
595            ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) ||
596            (((privptr->channel[READ].flag |
597                 privptr->channel[WRITE].flag) & CLAW_TIMER) != 0x00)) {
598 #ifdef DEBUGMSG
599                 printk(KERN_INFO "%s: channel problems during open - read:"
600                         " %02x -  write: %02x\n",
601                         dev->name,
602                         privptr->channel[READ].last_dstat,
603                         privptr->channel[WRITE].last_dstat);
604 #endif
605                 printk(KERN_INFO "%s: remote side is not ready\n", dev->name);
606                 CLAW_DBF_TEXT(2,trace,"notrdy");
607
608                 for ( i = 0; i < 2;  i++) {
609                         spin_lock_irqsave(
610                                 get_ccwdev_lock(privptr->channel[i].cdev),
611                                 saveflags);
612                         parm = (unsigned long) &privptr->channel[i];
613                         privptr->channel[i].claw_state = CLAW_STOP;
614                         rc = ccw_device_halt(
615                                 (struct ccw_device *)&privptr->channel[i].cdev,
616                                 parm);
617                         spin_unlock_irqrestore(
618                                 get_ccwdev_lock(privptr->channel[i].cdev),
619                                 saveflags);
620                         if (rc != 0) {
621                                 ccw_check_return_code(
622                                         privptr->channel[i].cdev, rc);
623                         }
624                 }
625                 free_pages((unsigned long)privptr->p_buff_ccw,
626                         (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
627                 if (privptr->p_env->read_size < PAGE_SIZE) {
628                         free_pages((unsigned long)privptr->p_buff_read,
629                                (int)pages_to_order_of_mag(
630                                         privptr->p_buff_read_num));
631                 }
632                 else {
633                         p_buf=privptr->p_read_active_first;
634                         while (p_buf!=NULL) {
635                                 free_pages((unsigned long)p_buf->p_buffer,
636                                       (int)pages_to_order_of_mag(
637                                         privptr->p_buff_pages_perread ));
638                                 p_buf=p_buf->next;
639                         }
640                 }
641                 if (privptr->p_env->write_size < PAGE_SIZE ) {
642                         free_pages((unsigned long)privptr->p_buff_write,
643                              (int)pages_to_order_of_mag(
644                                 privptr->p_buff_write_num));
645                 }
646                 else {
647                         p_buf=privptr->p_write_active_first;
648                         while (p_buf!=NULL) {
649                                 free_pages((unsigned long)p_buf->p_buffer,
650                                      (int)pages_to_order_of_mag(
651                                         privptr->p_buff_pages_perwrite ));
652                                 p_buf=p_buf->next;
653                         }
654                 }
655                 privptr->buffs_alloc = 0;
656                 privptr->channel[READ].flag= 0x00;
657                 privptr->channel[WRITE].flag = 0x00;
658                 privptr->p_buff_ccw=NULL;
659                 privptr->p_buff_read=NULL;
660                 privptr->p_buff_write=NULL;
661                 claw_clear_busy(dev);
662 #ifdef FUNCTRACE
663                 printk(KERN_INFO "%s:%s Exit on line %d, rc=EIO\n",
664                 dev->name,__func__,__LINE__);
665 #endif
666                 CLAW_DBF_TEXT(2,trace,"open EIO");
667                 return -EIO;
668         }
669
670         /*   Send SystemValidate command */
671
672         claw_clear_busy(dev);
673
674 #ifdef FUNCTRACE
675         printk(KERN_INFO "%s:%s Exit on line %d, rc=0\n",
676                 dev->name,__func__,__LINE__);
677 #endif
678         CLAW_DBF_TEXT(4,trace,"openok");
679         return 0;
680 }    /*     end of claw_open    */
681
682 /*-------------------------------------------------------------------*
683 *                                                                    *
684 *       claw_irq_handler                                             *
685 *                                                                    *
686 *--------------------------------------------------------------------*/
687 static void
688 claw_irq_handler(struct ccw_device *cdev,
689         unsigned long intparm, struct irb *irb)
690 {
691         struct chbk *p_ch = NULL;
692         struct claw_privbk *privptr = NULL;
693         struct net_device *dev = NULL;
694         struct claw_env  *p_env;
695         struct chbk *p_ch_r=NULL;
696
697
698 #ifdef FUNCTRACE
699         printk(KERN_INFO "%s enter  \n",__func__);
700 #endif
701         CLAW_DBF_TEXT(4,trace,"clawirq");
702         /* Bypass all 'unsolicited interrupts' */
703         if (!cdev->dev.driver_data) {
704                 printk(KERN_WARNING "claw: unsolicited interrupt for device:"
705                         "%s received c-%02x d-%02x\n",
706                        cdev->dev.bus_id, irb->scsw.cmd.cstat,
707                        irb->scsw.cmd.dstat);
708 #ifdef FUNCTRACE
709                 printk(KERN_INFO "claw: %s() "
710                         "exit on line %d\n",__func__,__LINE__);
711 #endif
712                 CLAW_DBF_TEXT(2,trace,"badirq");
713                 return;
714         }
715         privptr = (struct claw_privbk *)cdev->dev.driver_data;
716
717         /* Try to extract channel from driver data. */
718         if (privptr->channel[READ].cdev == cdev)
719                 p_ch = &privptr->channel[READ];
720         else if (privptr->channel[WRITE].cdev == cdev)
721                 p_ch = &privptr->channel[WRITE];
722         else {
723                 printk(KERN_WARNING "claw: Can't determine channel for "
724                         "interrupt, device %s\n", cdev->dev.bus_id);
725                 CLAW_DBF_TEXT(2,trace,"badchan");
726                 return;
727         }
728         CLAW_DBF_TEXT_(4,trace,"IRQCH=%d",p_ch->flag);
729
730         dev = (struct net_device *) (p_ch->ndev);
731         p_env=privptr->p_env;
732
733 #ifdef IOTRACE
734         printk(KERN_INFO "%s: interrupt for device: %04x "
735                 "received c-%02x d-%02x state-%02x\n",
736                dev->name, p_ch->devno, irb->scsw.cmd.cstat,
737                irb->scsw.cmd.dstat, p_ch->claw_state);
738 #endif
739
740         /* Copy interruption response block. */
741         memcpy(p_ch->irb, irb, sizeof(struct irb));
742
743         /* Check for good subchannel return code, otherwise error message */
744         if (irb->scsw.cmd.cstat && !(irb->scsw.cmd.cstat & SCHN_STAT_PCI)) {
745                 printk(KERN_INFO "%s: subchannel check for device: %04x -"
746                         " Sch Stat %02x  Dev Stat %02x CPA - %04x\n",
747                         dev->name, p_ch->devno,
748                         irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
749                         irb->scsw.cmd.cpa);
750 #ifdef IOTRACE
751                 dumpit((char *)irb,sizeof(struct irb));
752                 dumpit((char *)(unsigned long)irb->scsw.cmd.cpa,
753                         sizeof(struct ccw1));
754 #endif
755 #ifdef FUNCTRACE
756                 printk(KERN_INFO "%s:%s Exit on line %d\n",
757                 dev->name,__func__,__LINE__);
758 #endif
759                 CLAW_DBF_TEXT(2,trace,"chanchk");
760                 /* return; */
761         }
762
763         /* Check the reason-code of a unit check */
764         if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
765                 ccw_check_unit_check(p_ch, irb->ecw[0]);
766
767         /* State machine to bring the connection up, down and to restart */
768         p_ch->last_dstat = irb->scsw.cmd.dstat;
769
770         switch (p_ch->claw_state) {
771                 case CLAW_STOP:/* HALT_IO by claw_release (halt sequence) */
772 #ifdef DEBUGMSG
773                         printk(KERN_INFO "%s: CLAW_STOP enter\n", dev->name);
774 #endif
775                         if (!((p_ch->irb->scsw.cmd.stctl &
776                                SCSW_STCTL_SEC_STATUS) ||
777                             (p_ch->irb->scsw.cmd.stctl ==
778                                 SCSW_STCTL_STATUS_PEND) ||
779                             (p_ch->irb->scsw.cmd.stctl ==
780                                 (SCSW_STCTL_ALERT_STATUS |
781                                  SCSW_STCTL_STATUS_PEND)))) {
782 #ifdef FUNCTRACE
783                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
784                                         dev->name,__func__,__LINE__);
785 #endif
786                                 return;
787                         }
788                         wake_up(&p_ch->wait);   /* wake up claw_release */
789
790 #ifdef DEBUGMSG
791                         printk(KERN_INFO "%s: CLAW_STOP exit\n", dev->name);
792 #endif
793 #ifdef FUNCTRACE
794                         printk(KERN_INFO "%s:%s Exit on line %d\n",
795                                 dev->name,__func__,__LINE__);
796 #endif
797                         CLAW_DBF_TEXT(4,trace,"stop");
798                         return;
799
800                 case CLAW_START_HALT_IO: /* HALT_IO issued by claw_open  */
801 #ifdef DEBUGMSG
802                         printk(KERN_INFO "%s: process CLAW_STAT_HALT_IO\n",
803                                 dev->name);
804 #endif
805                         if (!((p_ch->irb->scsw.cmd.stctl &
806                                SCSW_STCTL_SEC_STATUS) ||
807                             (p_ch->irb->scsw.cmd.stctl ==
808                              SCSW_STCTL_STATUS_PEND) ||
809                             (p_ch->irb->scsw.cmd.stctl ==
810                              (SCSW_STCTL_ALERT_STATUS |
811                               SCSW_STCTL_STATUS_PEND)))) {
812 #ifdef FUNCTRACE
813                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
814                                         dev->name,__func__,__LINE__);
815 #endif
816                                 CLAW_DBF_TEXT(4,trace,"haltio");
817                                 return;
818                         }
819                         if (p_ch->flag == CLAW_READ) {
820                                 p_ch->claw_state = CLAW_START_READ;
821                                 wake_up(&p_ch->wait); /* wake claw_open (READ)*/
822                         }
823                         else
824                            if (p_ch->flag == CLAW_WRITE) {
825                                 p_ch->claw_state = CLAW_START_WRITE;
826                                 /*      send SYSTEM_VALIDATE                    */
827                                 claw_strt_read(dev, LOCK_NO);
828                                 claw_send_control(dev,
829                                         SYSTEM_VALIDATE_REQUEST,
830                                         0, 0, 0,
831                                         p_env->host_name,
832                                         p_env->adapter_name );
833                         } else {
834                                 printk(KERN_WARNING "claw: unsolicited "
835                                         "interrupt for device:"
836                                         "%s received c-%02x d-%02x\n",
837                                         cdev->dev.bus_id,
838                                         irb->scsw.cmd.cstat,
839                                         irb->scsw.cmd.dstat);
840                                 return;
841                                 }
842 #ifdef DEBUGMSG
843                         printk(KERN_INFO "%s: process CLAW_STAT_HALT_IO exit\n",
844                                 dev->name);
845 #endif
846 #ifdef FUNCTRACE
847                         printk(KERN_INFO "%s:%s Exit on line %d\n",
848                                 dev->name,__func__,__LINE__);
849 #endif
850                         CLAW_DBF_TEXT(4,trace,"haltio");
851                         return;
852                 case CLAW_START_READ:
853                         CLAW_DBF_TEXT(4,trace,"ReadIRQ");
854                         if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
855                                 clear_bit(0, (void *)&p_ch->IO_active);
856                                 if ((p_ch->irb->ecw[0] & 0x41) == 0x41 ||
857                                     (p_ch->irb->ecw[0] & 0x40) == 0x40 ||
858                                     (p_ch->irb->ecw[0])        == 0)
859                                 {
860                                         privptr->stats.rx_errors++;
861                                         printk(KERN_INFO "%s: Restart is "
862                                                 "required after remote "
863                                                 "side recovers \n",
864                                                 dev->name);
865                                 }
866 #ifdef FUNCTRACE
867                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
868                                         dev->name,__func__,__LINE__);
869 #endif
870                                         CLAW_DBF_TEXT(4,trace,"notrdy");
871                                         return;
872                         }
873                         if ((p_ch->irb->scsw.cmd.cstat & SCHN_STAT_PCI) &&
874                             (p_ch->irb->scsw.cmd.dstat == 0)) {
875                                 if (test_and_set_bit(CLAW_BH_ACTIVE,
876                                         (void *)&p_ch->flag_a) == 0) {
877                                         tasklet_schedule(&p_ch->tasklet);
878                                 }
879                                 else {
880                                         CLAW_DBF_TEXT(4,trace,"PCINoBH");
881                                 }
882 #ifdef FUNCTRACE
883                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
884                                         dev->name,__func__,__LINE__);
885 #endif
886                                 CLAW_DBF_TEXT(4,trace,"PCI_read");
887                                 return;
888                         }
889                         if (!((p_ch->irb->scsw.cmd.stctl &
890                                SCSW_STCTL_SEC_STATUS) ||
891                              (p_ch->irb->scsw.cmd.stctl ==
892                               SCSW_STCTL_STATUS_PEND) ||
893                              (p_ch->irb->scsw.cmd.stctl ==
894                               (SCSW_STCTL_ALERT_STATUS |
895                                SCSW_STCTL_STATUS_PEND)))) {
896 #ifdef FUNCTRACE
897                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
898                                         dev->name,__func__,__LINE__);
899 #endif
900                                 CLAW_DBF_TEXT(4,trace,"SPend_rd");
901                                 return;
902                         }
903                         clear_bit(0, (void *)&p_ch->IO_active);
904                         claw_clearbit_busy(TB_RETRY,dev);
905                         if (test_and_set_bit(CLAW_BH_ACTIVE,
906                                 (void *)&p_ch->flag_a) == 0) {
907                                 tasklet_schedule(&p_ch->tasklet);
908                          }
909                         else {
910                                 CLAW_DBF_TEXT(4,trace,"RdBHAct");
911                         }
912
913 #ifdef DEBUGMSG
914                         printk(KERN_INFO "%s: process CLAW_START_READ exit\n",
915                                 dev->name);
916 #endif
917 #ifdef FUNCTRACE
918                         printk(KERN_INFO "%s:%s Exit on line %d\n",
919                                 dev->name,__func__,__LINE__);
920 #endif
921                         CLAW_DBF_TEXT(4,trace,"RdIRQXit");
922                         return;
923                 case CLAW_START_WRITE:
924                         if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
925                                 printk(KERN_INFO "%s: Unit Check Occured in "
926                                         "write channel\n",dev->name);
927                                 clear_bit(0, (void *)&p_ch->IO_active);
928                                 if (p_ch->irb->ecw[0] & 0x80 ) {
929                                         printk(KERN_INFO "%s: Resetting Event "
930                                                 "occurred:\n",dev->name);
931                                         init_timer(&p_ch->timer);
932                                         p_ch->timer.function =
933                                                 (void *)claw_write_retry;
934                                         p_ch->timer.data = (unsigned long)p_ch;
935                                         p_ch->timer.expires = jiffies + 10*HZ;
936                                         add_timer(&p_ch->timer);
937                                         printk(KERN_INFO "%s: write connection "
938                                                 "restarting\n",dev->name);
939                                 }
940 #ifdef FUNCTRACE
941                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
942                                         dev->name,__func__,__LINE__);
943 #endif
944                                 CLAW_DBF_TEXT(4,trace,"rstrtwrt");
945                                 return;
946                         }
947                         if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
948                                         clear_bit(0, (void *)&p_ch->IO_active);
949                                         printk(KERN_INFO "%s: Unit Exception "
950                                                 "Occured in write channel\n",
951                                                 dev->name);
952                         }
953                         if (!((p_ch->irb->scsw.cmd.stctl &
954                                SCSW_STCTL_SEC_STATUS) ||
955                              (p_ch->irb->scsw.cmd.stctl ==
956                               SCSW_STCTL_STATUS_PEND) ||
957                              (p_ch->irb->scsw.cmd.stctl ==
958                               (SCSW_STCTL_ALERT_STATUS |
959                                SCSW_STCTL_STATUS_PEND)))) {
960 #ifdef FUNCTRACE
961                                 printk(KERN_INFO "%s:%s Exit on line %d\n",
962                                         dev->name,__func__,__LINE__);
963 #endif
964                                 CLAW_DBF_TEXT(4,trace,"writeUE");
965                                 return;
966                         }
967                         clear_bit(0, (void *)&p_ch->IO_active);
968                         if (claw_test_and_setbit_busy(TB_TX,dev)==0) {
969                                 claw_write_next(p_ch);
970                                 claw_clearbit_busy(TB_TX,dev);
971                                 claw_clear_busy(dev);
972                         }
973                         p_ch_r=(struct chbk *)&privptr->channel[READ];
974                         if (test_and_set_bit(CLAW_BH_ACTIVE,
975                                         (void *)&p_ch_r->flag_a) == 0) {
976                                 tasklet_schedule(&p_ch_r->tasklet);
977                         }
978
979 #ifdef DEBUGMSG
980                         printk(KERN_INFO "%s: process CLAW_START_WRITE exit\n",
981                                  dev->name);
982 #endif
983 #ifdef FUNCTRACE
984                         printk(KERN_INFO "%s:%s Exit on line %d\n",
985                                 dev->name,__func__,__LINE__);
986 #endif
987                         CLAW_DBF_TEXT(4,trace,"StWtExit");
988                         return;
989                 default:
990                         printk(KERN_WARNING "%s: wrong selection code - irq "
991                                 "state=%d\n",dev->name,p_ch->claw_state);
992 #ifdef FUNCTRACE
993                         printk(KERN_INFO "%s:%s Exit on line %d\n",
994                                 dev->name,__func__,__LINE__);
995 #endif
996                         CLAW_DBF_TEXT(2,trace,"badIRQ");
997                         return;
998         }
999
1000 }       /*   end of claw_irq_handler    */
1001
1002
1003 /*-------------------------------------------------------------------*
1004 *       claw_irq_tasklet                                             *
1005 *                                                                    *
1006 *--------------------------------------------------------------------*/
1007 static void
1008 claw_irq_tasklet ( unsigned long data )
1009 {
1010         struct chbk * p_ch;
1011         struct net_device  *dev;
1012         struct claw_privbk *       privptr;
1013
1014         p_ch = (struct chbk *) data;
1015         dev = (struct net_device *)p_ch->ndev;
1016 #ifdef FUNCTRACE
1017         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
1018 #endif
1019 #ifdef DEBUGMSG
1020         printk(KERN_INFO "%s: variable p_ch =\n",dev->name);
1021         dumpit((char *) p_ch, sizeof(struct chbk));
1022 #endif
1023         CLAW_DBF_TEXT(4,trace,"IRQtask");
1024
1025         privptr = (struct claw_privbk *) dev->priv;
1026
1027 #ifdef DEBUGMSG
1028         printk(KERN_INFO "%s: bh routine - state-%02x\n" ,
1029                 dev->name, p_ch->claw_state);
1030 #endif
1031
1032         unpack_read(dev);
1033         clear_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a);
1034         CLAW_DBF_TEXT(4,trace,"TskletXt");
1035 #ifdef FUNCTRACE
1036         printk(KERN_INFO "%s:%s Exit on line %d\n",
1037                 dev->name,__func__,__LINE__);
1038 #endif
1039         return;
1040 }       /*    end of claw_irq_bh    */
1041
1042 /*-------------------------------------------------------------------*
1043 *       claw_release                                                 *
1044 *                                                                    *
1045 *--------------------------------------------------------------------*/
1046 static int
1047 claw_release(struct net_device *dev)
1048 {
1049         int                rc;
1050         int                i;
1051         unsigned long      saveflags;
1052         unsigned long      parm;
1053         struct claw_privbk *privptr;
1054         DECLARE_WAITQUEUE(wait, current);
1055         struct ccwbk*             p_this_ccw;
1056         struct ccwbk*             p_buf;
1057
1058         if (!dev)
1059                 return 0;
1060         privptr = (struct claw_privbk *) dev->priv;
1061         if (!privptr)
1062                 return 0;
1063 #ifdef FUNCTRACE
1064         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
1065 #endif
1066         CLAW_DBF_TEXT(4,trace,"release");
1067 #ifdef DEBUGMSG
1068         printk(KERN_INFO "%s: variable dev =\n",dev->name);
1069         dumpit((char *) dev, sizeof(struct net_device));
1070         printk(KERN_INFO "Priv Buffalloc %d\n",privptr->buffs_alloc);
1071         printk(KERN_INFO "Priv p_buff_ccw = %p\n",&privptr->p_buff_ccw);
1072 #endif
1073         privptr->release_pend=1;
1074         claw_setbit_busy(TB_STOP,dev);
1075         for ( i = 1; i >=0 ;  i--) {
1076                 spin_lock_irqsave(
1077                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
1078              /*   del_timer(&privptr->channel[READ].timer);  */
1079                 privptr->channel[i].claw_state = CLAW_STOP;
1080                 privptr->channel[i].IO_active = 0;
1081                 parm = (unsigned long) &privptr->channel[i];
1082                 if (i == WRITE)
1083                         claw_purge_skb_queue(
1084                                 &privptr->channel[WRITE].collect_queue);
1085                 rc = ccw_device_halt (privptr->channel[i].cdev, parm);
1086                 if (privptr->system_validate_comp==0x00)  /* never opened? */
1087                    init_waitqueue_head(&privptr->channel[i].wait);
1088                 add_wait_queue(&privptr->channel[i].wait, &wait);
1089                 set_current_state(TASK_INTERRUPTIBLE);
1090                 spin_unlock_irqrestore(
1091                         get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
1092                 schedule();
1093                 set_current_state(TASK_RUNNING);
1094                 remove_wait_queue(&privptr->channel[i].wait, &wait);
1095                 if (rc != 0) {
1096                         ccw_check_return_code(privptr->channel[i].cdev, rc);
1097                 }
1098         }
1099         if (privptr->pk_skb != NULL) {
1100                 dev_kfree_skb_any(privptr->pk_skb);
1101                 privptr->pk_skb = NULL;
1102         }
1103         if(privptr->buffs_alloc != 1) {
1104 #ifdef FUNCTRACE
1105         printk(KERN_INFO "%s:%s Exit on line %d\n",
1106                 dev->name,__func__,__LINE__);
1107 #endif
1108                 CLAW_DBF_TEXT(4,trace,"none2fre");
1109                 return 0;
1110         }
1111         CLAW_DBF_TEXT(4,trace,"freebufs");
1112         if (privptr->p_buff_ccw != NULL) {
1113                 free_pages((unsigned long)privptr->p_buff_ccw,
1114                         (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
1115         }
1116         CLAW_DBF_TEXT(4,trace,"freeread");
1117         if (privptr->p_env->read_size < PAGE_SIZE) {
1118             if (privptr->p_buff_read != NULL) {
1119                 free_pages((unsigned long)privptr->p_buff_read,
1120                       (int)pages_to_order_of_mag(privptr->p_buff_read_num));
1121                 }
1122         }
1123         else {
1124                 p_buf=privptr->p_read_active_first;
1125                 while (p_buf!=NULL) {
1126                         free_pages((unsigned long)p_buf->p_buffer,
1127                              (int)pages_to_order_of_mag(
1128                                 privptr->p_buff_pages_perread ));
1129                         p_buf=p_buf->next;
1130                 }
1131         }
1132          CLAW_DBF_TEXT(4,trace,"freewrit");
1133         if (privptr->p_env->write_size < PAGE_SIZE ) {
1134                 free_pages((unsigned long)privptr->p_buff_write,
1135                       (int)pages_to_order_of_mag(privptr->p_buff_write_num));
1136         }
1137         else {
1138                 p_buf=privptr->p_write_active_first;
1139                 while (p_buf!=NULL) {
1140                         free_pages((unsigned long)p_buf->p_buffer,
1141                               (int)pages_to_order_of_mag(
1142                               privptr->p_buff_pages_perwrite ));
1143                         p_buf=p_buf->next;
1144                 }
1145         }
1146          CLAW_DBF_TEXT(4,trace,"clearptr");
1147         privptr->buffs_alloc = 0;
1148         privptr->p_buff_ccw=NULL;
1149         privptr->p_buff_read=NULL;
1150         privptr->p_buff_write=NULL;
1151         privptr->system_validate_comp=0;
1152         privptr->release_pend=0;
1153         /*      Remove any writes that were pending and reset all reads   */
1154         p_this_ccw=privptr->p_read_active_first;
1155         while (p_this_ccw!=NULL) {
1156                 p_this_ccw->header.length=0xffff;
1157                 p_this_ccw->header.opcode=0xff;
1158                 p_this_ccw->header.flag=0x00;
1159                 p_this_ccw=p_this_ccw->next;
1160         }
1161
1162         while (privptr->p_write_active_first!=NULL) {
1163                 p_this_ccw=privptr->p_write_active_first;
1164                 p_this_ccw->header.flag=CLAW_PENDING;
1165                 privptr->p_write_active_first=p_this_ccw->next;
1166                 p_this_ccw->next=privptr->p_write_free_chain;
1167                 privptr->p_write_free_chain=p_this_ccw;
1168                 ++privptr->write_free_count;
1169         }
1170         privptr->p_write_active_last=NULL;
1171         privptr->mtc_logical_link = -1;
1172         privptr->mtc_skipping = 1;
1173         privptr->mtc_offset=0;
1174
1175         if (((privptr->channel[READ].last_dstat |
1176                 privptr->channel[WRITE].last_dstat) &
1177                 ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) {
1178                 printk(KERN_WARNING "%s: channel problems during close - "
1179                         "read: %02x -  write: %02x\n",
1180                 dev->name,
1181                 privptr->channel[READ].last_dstat,
1182                 privptr->channel[WRITE].last_dstat);
1183                  CLAW_DBF_TEXT(2,trace,"badclose");
1184         }
1185 #ifdef FUNCTRACE
1186         printk(KERN_INFO "%s:%s Exit on line %d\n",
1187                 dev->name,__func__,__LINE__);
1188 #endif
1189         CLAW_DBF_TEXT(4,trace,"rlsexit");
1190         return 0;
1191 }      /* end of claw_release     */
1192
1193
1194
1195 /*-------------------------------------------------------------------*
1196 *       claw_write_retry                                             *
1197 *                                                                    *
1198 *--------------------------------------------------------------------*/
1199
1200 static void
1201 claw_write_retry ( struct chbk *p_ch )
1202 {
1203
1204         struct net_device  *dev=p_ch->ndev;
1205
1206
1207 #ifdef FUNCTRACE
1208         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
1209         printk(KERN_INFO "claw: variable p_ch =\n");
1210         dumpit((char *) p_ch, sizeof(struct chbk));
1211 #endif
1212         CLAW_DBF_TEXT(4,trace,"w_retry");
1213         if (p_ch->claw_state == CLAW_STOP) {
1214 #ifdef FUNCTRACE
1215                 printk(KERN_INFO "%s:%s Exit on line %d\n",
1216                         dev->name,__func__,__LINE__);
1217 #endif
1218                 return;
1219         }
1220 #ifdef DEBUGMSG
1221         printk( KERN_INFO "%s:%s  state-%02x\n" ,
1222                 dev->name,
1223                 __func__,
1224                 p_ch->claw_state);
1225 #endif
1226         claw_strt_out_IO( dev );
1227 #ifdef FUNCTRACE
1228         printk(KERN_INFO "%s:%s Exit on line %d\n",
1229                 dev->name,__func__,__LINE__);
1230 #endif
1231         CLAW_DBF_TEXT(4,trace,"rtry_xit");
1232         return;
1233 }      /* end of claw_write_retry      */
1234
1235
1236 /*-------------------------------------------------------------------*
1237 *       claw_write_next                                              *
1238 *                                                                    *
1239 *--------------------------------------------------------------------*/
1240
1241 static void
1242 claw_write_next ( struct chbk * p_ch )
1243 {
1244
1245         struct net_device  *dev;
1246         struct claw_privbk *privptr=NULL;
1247         struct sk_buff *pk_skb;
1248         int     rc;
1249
1250 #ifdef FUNCTRACE
1251         printk(KERN_INFO "%s:%s Enter  \n",p_ch->ndev->name,__func__);
1252         printk(KERN_INFO "%s: variable p_ch =\n",p_ch->ndev->name);
1253         dumpit((char *) p_ch, sizeof(struct chbk));
1254 #endif
1255         CLAW_DBF_TEXT(4,trace,"claw_wrt");
1256         if (p_ch->claw_state == CLAW_STOP)
1257                 return;
1258         dev = (struct net_device *) p_ch->ndev;
1259         privptr = (struct claw_privbk *) dev->priv;
1260         claw_free_wrt_buf( dev );
1261         if ((privptr->write_free_count > 0) &&
1262             !skb_queue_empty(&p_ch->collect_queue)) {
1263                 pk_skb = claw_pack_skb(privptr);
1264                 while (pk_skb != NULL) {
1265                         rc = claw_hw_tx( pk_skb, dev,1);
1266                         if (privptr->write_free_count > 0) {
1267                                 pk_skb = claw_pack_skb(privptr);
1268                         } else
1269                                 pk_skb = NULL;
1270                 }
1271         }
1272         if (privptr->p_write_active_first!=NULL) {
1273                 claw_strt_out_IO(dev);
1274         }
1275
1276 #ifdef FUNCTRACE
1277         printk(KERN_INFO "%s:%s Exit on line %d\n",
1278                 dev->name,__func__,__LINE__);
1279 #endif
1280         return;
1281 }      /* end of claw_write_next      */
1282
1283 /*-------------------------------------------------------------------*
1284 *                                                                    *
1285 *       claw_timer                                                   *
1286 *--------------------------------------------------------------------*/
1287
1288 static void
1289 claw_timer ( struct chbk * p_ch )
1290 {
1291 #ifdef FUNCTRACE
1292         printk(KERN_INFO "%s:%s Entry\n",p_ch->ndev->name,__func__);
1293         printk(KERN_INFO "%s: variable p_ch =\n",p_ch->ndev->name);
1294         dumpit((char *) p_ch, sizeof(struct chbk));
1295 #endif
1296         CLAW_DBF_TEXT(4,trace,"timer");
1297         p_ch->flag |= CLAW_TIMER;
1298         wake_up(&p_ch->wait);
1299 #ifdef FUNCTRACE
1300         printk(KERN_INFO "%s:%s Exit on line %d\n",
1301                 p_ch->ndev->name,__func__,__LINE__);
1302 #endif
1303         return;
1304 }      /* end of claw_timer  */
1305
1306
1307 /*
1308 *
1309 *       functions
1310 */
1311
1312
1313 /*-------------------------------------------------------------------*
1314 *                                                                    *
1315 *     pages_to_order_of_mag                                          *
1316 *                                                                    *
1317 *    takes a number of pages from 1 to 512 and returns the           *
1318 *    log(num_pages)/log(2) get_free_pages() needs a base 2 order     *
1319 *    of magnitude get_free_pages() has an upper order of 9           *
1320 *--------------------------------------------------------------------*/
1321
1322 static int
1323 pages_to_order_of_mag(int num_of_pages)
1324 {
1325         int     order_of_mag=1;         /* assume 2 pages */
1326         int     nump=2;
1327 #ifdef FUNCTRACE
1328         printk(KERN_INFO "%s Enter pages = %d \n",__func__,num_of_pages);
1329 #endif
1330         CLAW_DBF_TEXT_(5,trace,"pages%d",num_of_pages);
1331         if (num_of_pages == 1)   {return 0; }  /* magnitude of 0 = 1 page */
1332         /* 512 pages = 2Meg on 4k page systems */
1333         if (num_of_pages >= 512) {return 9; }
1334         /* we have two or more pages order is at least 1 */
1335         for (nump=2 ;nump <= 512;nump*=2) {
1336           if (num_of_pages <= nump)
1337                   break;
1338           order_of_mag +=1;
1339         }
1340         if (order_of_mag > 9) { order_of_mag = 9; }  /* I know it's paranoid */
1341 #ifdef FUNCTRACE
1342         printk(KERN_INFO "%s Exit on line %d, order = %d\n",
1343         __func__,__LINE__, order_of_mag);
1344 #endif
1345         CLAW_DBF_TEXT_(5,trace,"mag%d",order_of_mag);
1346         return order_of_mag;
1347 }
1348
1349 /*-------------------------------------------------------------------*
1350 *                                                                    *
1351 *     add_claw_reads                                                 *
1352 *                                                                    *
1353 *--------------------------------------------------------------------*/
1354 static int
1355 add_claw_reads(struct net_device *dev, struct ccwbk* p_first,
1356         struct ccwbk* p_last)
1357 {
1358         struct claw_privbk *privptr;
1359         struct ccw1  temp_ccw;
1360         struct endccw * p_end;
1361 #ifdef IOTRACE
1362         struct ccwbk*  p_buf;
1363 #endif
1364 #ifdef FUNCTRACE
1365         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
1366 #endif
1367 #ifdef DEBUGMSG
1368         printk(KERN_INFO "dev\n");
1369         dumpit((char *) dev, sizeof(struct net_device));
1370         printk(KERN_INFO "p_first\n");
1371         dumpit((char *) p_first, sizeof(struct ccwbk));
1372         printk(KERN_INFO "p_last\n");
1373         dumpit((char *) p_last, sizeof(struct ccwbk));
1374 #endif
1375         CLAW_DBF_TEXT(4,trace,"addreads");
1376         privptr = dev->priv;
1377         p_end = privptr->p_end_ccw;
1378
1379         /* first CCW and last CCW contains a new set of read channel programs
1380         *       to apend the running channel programs
1381         */
1382         if ( p_first==NULL) {
1383 #ifdef FUNCTRACE
1384                 printk(KERN_INFO "%s:%s Exit on line %d\n",
1385                         dev->name,__func__,__LINE__);
1386 #endif
1387                 CLAW_DBF_TEXT(4,trace,"addexit");
1388                 return 0;
1389         }
1390
1391         /* set up ending CCW sequence for this segment */
1392         if (p_end->read1) {
1393                 p_end->read1=0x00;    /*  second ending CCW is now active */
1394                 /*      reset ending CCWs and setup TIC CCWs              */
1395                 p_end->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1396                 p_end->read2_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1397                 p_last->r_TIC_1.cda =(__u32)__pa(&p_end->read2_nop1);
1398                 p_last->r_TIC_2.cda =(__u32)__pa(&p_end->read2_nop1);
1399                 p_end->read2_nop2.cda=0;
1400                 p_end->read2_nop2.count=1;
1401         }
1402         else {
1403                 p_end->read1=0x01;  /* first ending CCW is now active */
1404                 /*      reset ending CCWs and setup TIC CCWs          */
1405                 p_end->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1406                 p_end->read1_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
1407                 p_last->r_TIC_1.cda = (__u32)__pa(&p_end->read1_nop1);
1408                 p_last->r_TIC_2.cda = (__u32)__pa(&p_end->read1_nop1);
1409                 p_end->read1_nop2.cda=0;
1410                 p_end->read1_nop2.count=1;
1411         }
1412
1413         if ( privptr-> p_read_active_first ==NULL ) {
1414 #ifdef DEBUGMSG
1415                 printk(KERN_INFO "%s:%s p_read_active_first == NULL \n",
1416                         dev->name,__func__);
1417                 printk(KERN_INFO "%s:%s Read active first/last changed \n",
1418                         dev->name,__func__);
1419 #endif
1420                 privptr-> p_read_active_first= p_first;  /*    set new first */
1421                 privptr-> p_read_active_last = p_last;   /*    set new last  */
1422         }
1423         else {
1424
1425 #ifdef DEBUGMSG
1426                 printk(KERN_INFO "%s:%s Read in progress \n",
1427                 dev->name,__func__);
1428 #endif
1429                 /* set up TIC ccw  */
1430                 temp_ccw.cda= (__u32)__pa(&p_first->read);
1431                 temp_ccw.count=0;
1432                 temp_ccw.flags=0;
1433                 temp_ccw.cmd_code = CCW_CLAW_CMD_TIC;
1434
1435
1436                 if (p_end->read1) {
1437
1438                /* first set of CCW's is chained to the new read              */
1439                /* chain, so the second set is chained to the active chain.   */
1440                /* Therefore modify the second set to point to the new        */
1441                /* read chain set up TIC CCWs                                 */
1442                /* make sure we update the CCW so channel doesn't fetch it    */
1443                /* when it's only half done                                   */
1444                         memcpy( &p_end->read2_nop2, &temp_ccw ,
1445                                 sizeof(struct ccw1));
1446                         privptr->p_read_active_last->r_TIC_1.cda=
1447                                 (__u32)__pa(&p_first->read);
1448                         privptr->p_read_active_last->r_TIC_2.cda=
1449                                 (__u32)__pa(&p_first->read);
1450                 }
1451                 else {
1452                         /* make sure we update the CCW so channel doesn't   */
1453                         /* fetch it when it is only half done               */
1454                         memcpy( &p_end->read1_nop2, &temp_ccw ,
1455                                 sizeof(struct ccw1));
1456                         privptr->p_read_active_last->r_TIC_1.cda=
1457                                 (__u32)__pa(&p_first->read);
1458                         privptr->p_read_active_last->r_TIC_2.cda=
1459                                 (__u32)__pa(&p_first->read);
1460                 }
1461                 /*      chain in new set of blocks                              */
1462                 privptr->p_read_active_last->next = p_first;
1463                 privptr->p_read_active_last=p_last;
1464         } /* end of if ( privptr-> p_read_active_first ==NULL)  */
1465 #ifdef IOTRACE
1466         printk(KERN_INFO "%s:%s  dump p_last CCW BK \n",dev->name,__func__);
1467         dumpit((char *)p_last, sizeof(struct ccwbk));
1468         printk(KERN_INFO "%s:%s  dump p_end CCW BK \n",dev->name,__func__);
1469         dumpit((char *)p_end, sizeof(struct endccw));
1470
1471         printk(KERN_INFO "%s:%s dump p_first CCW BK \n",dev->name,__func__);
1472         dumpit((char *)p_first, sizeof(struct ccwbk));
1473         printk(KERN_INFO "%s:%s Dump Active CCW chain \n",
1474                 dev->name,__func__);
1475         p_buf=privptr->p_read_active_first;
1476         while (p_buf!=NULL) {
1477                 dumpit((char *)p_buf, sizeof(struct ccwbk));
1478                 p_buf=p_buf->next;
1479         }
1480 #endif
1481 #ifdef FUNCTRACE
1482         printk(KERN_INFO "%s:%s Exit on line %d\n",
1483                 dev->name,__func__,__LINE__);
1484 #endif
1485         CLAW_DBF_TEXT(4,trace,"addexit");
1486         return 0;
1487 }    /*     end of add_claw_reads   */
1488
1489 /*-------------------------------------------------------------------*
1490  *   ccw_check_return_code                                           *
1491  *                                                                   *
1492  *-------------------------------------------------------------------*/
1493
1494 static void
1495 ccw_check_return_code(struct ccw_device *cdev, int return_code)
1496 {
1497 #ifdef FUNCTRACE
1498         printk(KERN_INFO "%s: %s() > enter  \n",
1499                 cdev->dev.bus_id,__func__);
1500 #endif
1501         CLAW_DBF_TEXT(4,trace,"ccwret");
1502 #ifdef DEBUGMSG
1503         printk(KERN_INFO "variable cdev =\n");
1504         dumpit((char *) cdev, sizeof(struct ccw_device));
1505         printk(KERN_INFO "variable return_code = %d\n",return_code);
1506 #endif
1507         if (return_code != 0) {
1508                 switch (return_code) {
1509                         case -EBUSY:
1510                                 printk(KERN_INFO "%s: Busy !\n",
1511                                         cdev->dev.bus_id);
1512                                 break;
1513                         case -ENODEV:
1514                                 printk(KERN_EMERG "%s: Missing device called "
1515                                         "for IO ENODEV\n", cdev->dev.bus_id);
1516                                 break;
1517                         case -EIO:
1518                                 printk(KERN_EMERG "%s: Status pending... EIO \n",
1519                                         cdev->dev.bus_id);
1520                                 break;
1521                         case -EINVAL:
1522                                 printk(KERN_EMERG "%s: Invalid Dev State EINVAL \n",
1523                                         cdev->dev.bus_id);
1524                                 break;
1525                         default:
1526                                 printk(KERN_EMERG "%s: Unknown error in "
1527                                  "Do_IO %d\n",cdev->dev.bus_id, return_code);
1528                 }
1529         }
1530 #ifdef FUNCTRACE
1531         printk(KERN_INFO "%s: %s() > exit on line %d\n",
1532                 cdev->dev.bus_id,__func__,__LINE__);
1533 #endif
1534         CLAW_DBF_TEXT(4,trace,"ccwret");
1535 }    /*    end of ccw_check_return_code   */
1536
1537 /*-------------------------------------------------------------------*
1538 *       ccw_check_unit_check                                         *
1539 *--------------------------------------------------------------------*/
1540
1541 static void
1542 ccw_check_unit_check(struct chbk * p_ch, unsigned char sense )
1543 {
1544         struct net_device *dev = p_ch->ndev;
1545
1546 #ifdef FUNCTRACE
1547         printk(KERN_INFO "%s: %s() > enter\n",dev->name,__func__);
1548 #endif
1549 #ifdef DEBUGMSG
1550         printk(KERN_INFO "%s: variable dev =\n",dev->name);
1551         dumpit((char *)dev, sizeof(struct net_device));
1552         printk(KERN_INFO "%s: variable sense =\n",dev->name);
1553         dumpit((char *)&sense, 2);
1554 #endif
1555         CLAW_DBF_TEXT(4,trace,"unitchek");
1556
1557         printk(KERN_INFO "%s: Unit Check with sense byte:0x%04x\n",
1558                 dev->name, sense);
1559
1560         if (sense & 0x40) {
1561                 if (sense & 0x01) {
1562                         printk(KERN_WARNING "%s: Interface disconnect or "
1563                                 "Selective reset "
1564                                 "occurred (remote side)\n", dev->name);
1565                 }
1566                 else {
1567                         printk(KERN_WARNING "%s: System reset occured"
1568                                 " (remote side)\n", dev->name);
1569                 }
1570         }
1571         else if (sense & 0x20) {
1572                 if (sense & 0x04) {
1573                         printk(KERN_WARNING "%s: Data-streaming "
1574                                 "timeout)\n", dev->name);
1575                 }
1576                 else  {
1577                         printk(KERN_WARNING "%s: Data-transfer parity"
1578                                 " error\n", dev->name);
1579                 }
1580         }
1581         else if (sense & 0x10) {
1582                 if (sense & 0x20) {
1583                         printk(KERN_WARNING "%s: Hardware malfunction "
1584                                 "(remote side)\n", dev->name);
1585                 }
1586                 else {
1587                         printk(KERN_WARNING "%s: read-data parity error "
1588                                 "(remote side)\n", dev->name);
1589                 }
1590         }
1591
1592 #ifdef FUNCTRACE
1593         printk(KERN_INFO "%s: %s() exit on line %d\n",
1594                 dev->name,__func__,__LINE__);
1595 #endif
1596 }   /*    end of ccw_check_unit_check    */
1597
1598
1599
1600 /*-------------------------------------------------------------------*
1601 * Dump buffer format                                                 *
1602 *                                                                    *
1603 *--------------------------------------------------------------------*/
1604 #ifdef DEBUG
1605 static void
1606 dumpit(char* buf, int len)
1607 {
1608
1609         __u32      ct, sw, rm, dup;
1610         char       *ptr, *rptr;
1611         char       tbuf[82], tdup[82];
1612 #if (CONFIG_64BIT)
1613         char       addr[22];
1614 #else
1615         char       addr[12];
1616 #endif
1617         char       boff[12];
1618         char       bhex[82], duphex[82];
1619         char       basc[40];
1620
1621         sw  = 0;
1622         rptr =ptr=buf;
1623         rm  = 16;
1624         duphex[0]  = 0x00;
1625         dup = 0;
1626         for ( ct=0; ct < len; ct++, ptr++, rptr++ )  {
1627                 if (sw == 0) {
1628 #if (CONFIG_64BIT)
1629                         sprintf(addr, "%16.16lX",(unsigned long)rptr);
1630 #else
1631                         sprintf(addr, "%8.8X",(__u32)rptr);
1632 #endif
1633                         sprintf(boff, "%4.4X", (__u32)ct);
1634                         bhex[0] = '\0';
1635                         basc[0] = '\0';
1636                 }
1637                 if ((sw == 4) || (sw == 12)) {
1638                         strcat(bhex, " ");
1639                 }
1640                 if (sw == 8) {
1641                         strcat(bhex, "  ");
1642                 }
1643 #if (CONFIG_64BIT)
1644                 sprintf(tbuf,"%2.2lX", (unsigned long)*ptr);
1645 #else
1646                 sprintf(tbuf,"%2.2X", (__u32)*ptr);
1647 #endif
1648                 tbuf[2] = '\0';
1649                 strcat(bhex, tbuf);
1650                 if ((0!=isprint(*ptr)) && (*ptr >= 0x20)) {
1651                         basc[sw] = *ptr;
1652                 }
1653                 else {
1654                         basc[sw] = '.';
1655                 }
1656                 basc[sw+1] = '\0';
1657                 sw++;
1658                 rm--;
1659                 if (sw==16) {
1660                         if ((strcmp(duphex, bhex)) !=0) {
1661                                 if (dup !=0) {
1662                                         sprintf(tdup,"Duplicate as above to"
1663                                                 " %s", addr);
1664                                         printk( KERN_INFO "                 "
1665                                                 "   --- %s ---\n",tdup);
1666                                 }
1667                                 printk( KERN_INFO "   %s (+%s) : %s  [%s]\n",
1668                                          addr, boff, bhex, basc);
1669                                 dup = 0;
1670                                 strcpy(duphex, bhex);
1671                         }
1672                         else {
1673                                 dup++;
1674                         }
1675                         sw = 0;
1676                         rm = 16;
1677                 }
1678         }  /* endfor */
1679
1680         if (sw != 0) {
1681                 for ( ; rm > 0; rm--, sw++ ) {
1682                         if ((sw==4) || (sw==12)) strcat(bhex, " ");
1683                         if (sw==8)               strcat(bhex, "  ");
1684                         strcat(bhex, "  ");
1685                         strcat(basc, " ");
1686                 }
1687                 if (dup !=0) {
1688                         sprintf(tdup,"Duplicate as above to %s", addr);
1689                         printk( KERN_INFO "                    --- %s ---\n",
1690                                 tdup);
1691                 }
1692                 printk( KERN_INFO "   %s (+%s) : %s  [%s]\n",
1693                         addr, boff, bhex, basc);
1694         }
1695         else {
1696                 if (dup >=1) {
1697                         sprintf(tdup,"Duplicate as above to %s", addr);
1698                         printk( KERN_INFO "                    --- %s ---\n",
1699                                 tdup);
1700                 }
1701                 if (dup !=0) {
1702                         printk( KERN_INFO "   %s (+%s) : %s  [%s]\n",
1703                                 addr, boff, bhex, basc);
1704                 }
1705         }
1706         return;
1707
1708 }   /*   end of dumpit  */
1709 #endif
1710
1711 /*-------------------------------------------------------------------*
1712 *               find_link                                            *
1713 *--------------------------------------------------------------------*/
1714 static int
1715 find_link(struct net_device *dev, char *host_name, char *ws_name )
1716 {
1717         struct claw_privbk *privptr;
1718         struct claw_env *p_env;
1719         int    rc=0;
1720
1721 #ifdef FUNCTRACE
1722         printk(KERN_INFO "%s:%s > enter  \n",dev->name,__func__);
1723 #endif
1724         CLAW_DBF_TEXT(2,setup,"findlink");
1725 #ifdef DEBUGMSG
1726         printk(KERN_INFO "%s: variable dev = \n",dev->name);
1727         dumpit((char *) dev, sizeof(struct net_device));
1728         printk(KERN_INFO "%s: variable host_name = %s\n",dev->name, host_name);
1729         printk(KERN_INFO "%s: variable ws_name = %s\n",dev->name, ws_name);
1730 #endif
1731         privptr=dev->priv;
1732         p_env=privptr->p_env;
1733         switch (p_env->packing)
1734         {
1735                 case  PACKING_ASK:
1736                         if ((memcmp(WS_APPL_NAME_PACKED, host_name, 8)!=0) ||
1737                             (memcmp(WS_APPL_NAME_PACKED, ws_name, 8)!=0 ))
1738                              rc = EINVAL;
1739                         break;
1740                 case  DO_PACKED:
1741                 case  PACK_SEND:
1742                         if ((memcmp(WS_APPL_NAME_IP_NAME, host_name, 8)!=0) ||
1743                             (memcmp(WS_APPL_NAME_IP_NAME, ws_name, 8)!=0 ))
1744                                 rc = EINVAL;
1745                         break;
1746                 default:
1747                         if ((memcmp(HOST_APPL_NAME, host_name, 8)!=0) ||
1748                             (memcmp(p_env->api_type , ws_name, 8)!=0))
1749                                 rc = EINVAL;
1750                         break;
1751         }
1752
1753 #ifdef FUNCTRACE
1754         printk(KERN_INFO "%s:%s Exit on line %d\n",
1755                 dev->name,__func__,__LINE__);
1756 #endif
1757         return 0;
1758 }    /*    end of find_link    */
1759
1760 /*-------------------------------------------------------------------*
1761  *   claw_hw_tx                                                      *
1762  *                                                                   *
1763  *                                                                   *
1764  *-------------------------------------------------------------------*/
1765
1766 static int
1767 claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid)
1768 {
1769         int                             rc=0;
1770         struct claw_privbk              *privptr;
1771         struct ccwbk           *p_this_ccw;
1772         struct ccwbk           *p_first_ccw;
1773         struct ccwbk           *p_last_ccw;
1774         __u32                           numBuffers;
1775         signed long                     len_of_data;
1776         unsigned long                   bytesInThisBuffer;
1777         unsigned char                   *pDataAddress;
1778         struct endccw                   *pEnd;
1779         struct ccw1                     tempCCW;
1780         struct chbk                     *p_ch;
1781         struct claw_env                 *p_env;
1782         int                             lock;
1783         struct clawph                   *pk_head;
1784         struct chbk                     *ch;
1785 #ifdef IOTRACE
1786         struct ccwbk                   *p_buf;
1787 #endif
1788 #ifdef FUNCTRACE
1789         printk(KERN_INFO "%s: %s() > enter\n",dev->name,__func__);
1790 #endif
1791         CLAW_DBF_TEXT(4,trace,"hw_tx");
1792 #ifdef DEBUGMSG
1793         printk(KERN_INFO "%s: variable dev skb =\n",dev->name);
1794         dumpit((char *) skb, sizeof(struct sk_buff));
1795         printk(KERN_INFO "%s: variable dev =\n",dev->name);
1796         dumpit((char *) dev, sizeof(struct net_device));
1797         printk(KERN_INFO "%s: variable linkid = %ld\n",dev->name,linkid);
1798 #endif
1799         privptr = (struct claw_privbk *) (dev->priv);
1800         p_ch=(struct chbk *)&privptr->channel[WRITE];
1801         p_env =privptr->p_env;
1802 #ifdef IOTRACE
1803         printk(KERN_INFO "%s: %s() dump sk_buff  \n",dev->name,__func__);
1804         dumpit((char *)skb ,sizeof(struct sk_buff));
1805 #endif
1806         claw_free_wrt_buf(dev); /* Clean up free chain if posible */
1807         /*  scan the write queue to free any completed write packets   */
1808         p_first_ccw=NULL;
1809         p_last_ccw=NULL;
1810         if ((p_env->packing >= PACK_SEND) &&
1811             (skb->cb[1] != 'P')) {
1812                 skb_push(skb,sizeof(struct clawph));
1813                 pk_head=(struct clawph *)skb->data;
1814                 pk_head->len=skb->len-sizeof(struct clawph);
1815                 if (pk_head->len%4)  {
1816                         pk_head->len+= 4-(pk_head->len%4);
1817                         skb_pad(skb,4-(pk_head->len%4));
1818                         skb_put(skb,4-(pk_head->len%4));
1819                 }
1820                 if (p_env->packing == DO_PACKED)
1821                         pk_head->link_num = linkid;
1822                 else
1823                         pk_head->link_num = 0;
1824                 pk_head->flag = 0x00;
1825                 skb_pad(skb,4);
1826                 skb->cb[1] = 'P';
1827         }
1828         if (linkid == 0) {
1829                 if (claw_check_busy(dev)) {
1830                         if (privptr->write_free_count!=0) {
1831                                 claw_clear_busy(dev);
1832                         }
1833                         else {
1834                                 claw_strt_out_IO(dev );
1835                                 claw_free_wrt_buf( dev );
1836                                 if (privptr->write_free_count==0) {
1837 #ifdef IOTRACE
1838                                         printk(KERN_INFO "%s: "
1839                                            "(claw_check_busy) no free write "
1840                                            "buffers\n", dev->name);
1841 #endif
1842                                         ch = &privptr->channel[WRITE];
1843                                         atomic_inc(&skb->users);
1844                                         skb_queue_tail(&ch->collect_queue, skb);
1845                                         goto Done;
1846                                 }
1847                                 else {
1848                                         claw_clear_busy(dev);
1849                                 }
1850                         }
1851                 }
1852                 /*  tx lock  */
1853                 if (claw_test_and_setbit_busy(TB_TX,dev)) { /* set to busy */
1854 #ifdef DEBUGMSG
1855                         printk(KERN_INFO "%s:  busy  (claw_test_and_setbit_"
1856                                 "busy)\n", dev->name);
1857 #endif
1858                         ch = &privptr->channel[WRITE];
1859                         atomic_inc(&skb->users);
1860                         skb_queue_tail(&ch->collect_queue, skb);
1861                         claw_strt_out_IO(dev );
1862                         rc=-EBUSY;
1863                         goto Done2;
1864                 }
1865         }
1866         /*      See how many write buffers are required to hold this data */
1867         numBuffers = DIV_ROUND_UP(skb->len, privptr->p_env->write_size);
1868
1869         /*      If that number of buffers isn't available, give up for now */
1870         if (privptr->write_free_count < numBuffers ||
1871             privptr->p_write_free_chain == NULL ) {
1872
1873                 claw_setbit_busy(TB_NOBUFFER,dev);
1874
1875 #ifdef DEBUGMSG
1876                 printk(KERN_INFO "%s:  busy  (claw_setbit_busy"
1877                         "(TB_NOBUFFER))\n", dev->name);
1878                 printk(KERN_INFO "       free_count: %d, numBuffers : %d\n",
1879                         (int)privptr->write_free_count,(int) numBuffers );
1880 #endif
1881                 ch = &privptr->channel[WRITE];
1882                 atomic_inc(&skb->users);
1883                 skb_queue_tail(&ch->collect_queue, skb);
1884                 CLAW_DBF_TEXT(2,trace,"clawbusy");
1885                 goto Done2;
1886         }
1887         pDataAddress=skb->data;
1888         len_of_data=skb->len;
1889
1890         while (len_of_data > 0) {
1891 #ifdef DEBUGMSG
1892                 printk(KERN_INFO "%s: %s() length-of-data is %ld \n",
1893                         dev->name ,__func__,len_of_data);
1894                 dumpit((char *)pDataAddress ,64);
1895 #endif
1896                 p_this_ccw=privptr->p_write_free_chain;  /* get a block */
1897                 if (p_this_ccw == NULL) { /* lost the race */
1898                         ch = &privptr->channel[WRITE];
1899                         atomic_inc(&skb->users);
1900                         skb_queue_tail(&ch->collect_queue, skb);
1901                         goto Done2;
1902                 }
1903                 privptr->p_write_free_chain=p_this_ccw->next;
1904                 p_this_ccw->next=NULL;
1905                 --privptr->write_free_count; /* -1 */
1906                 bytesInThisBuffer=len_of_data;
1907                 memcpy( p_this_ccw->p_buffer,pDataAddress, bytesInThisBuffer);
1908                 len_of_data-=bytesInThisBuffer;
1909                 pDataAddress+=(unsigned long)bytesInThisBuffer;
1910                 /*      setup write CCW         */
1911                 p_this_ccw->write.cmd_code = (linkid * 8) +1;
1912                 if (len_of_data>0) {
1913                         p_this_ccw->write.cmd_code+=MORE_to_COME_FLAG;
1914                 }
1915                 p_this_ccw->write.count=bytesInThisBuffer;
1916                 /*      now add to end of this chain    */
1917                 if (p_first_ccw==NULL)    {
1918                         p_first_ccw=p_this_ccw;
1919                 }
1920                 if (p_last_ccw!=NULL) {
1921                         p_last_ccw->next=p_this_ccw;
1922                         /*      set up TIC ccws         */
1923                         p_last_ccw->w_TIC_1.cda=
1924                                 (__u32)__pa(&p_this_ccw->write);
1925                 }
1926                 p_last_ccw=p_this_ccw;      /* save new last block */
1927 #ifdef IOTRACE
1928                 printk(KERN_INFO "%s: %s() > CCW and Buffer %ld bytes long \n",
1929                         dev->name,__func__,bytesInThisBuffer);
1930                 dumpit((char *)p_this_ccw, sizeof(struct ccwbk));
1931                 dumpit((char *)p_this_ccw->p_buffer, 64);
1932 #endif
1933         }
1934
1935         /*      FirstCCW and LastCCW now contain a new set of write channel
1936         *       programs to append to the running channel program
1937         */
1938
1939         if (p_first_ccw!=NULL) {
1940                 /*      setup ending ccw sequence for this segment              */
1941                 pEnd=privptr->p_end_ccw;
1942                 if (pEnd->write1) {
1943                         pEnd->write1=0x00;   /* second end ccw is now active */
1944                         /*      set up Tic CCWs         */
1945                         p_last_ccw->w_TIC_1.cda=
1946                                 (__u32)__pa(&pEnd->write2_nop1);
1947                         pEnd->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1948                         pEnd->write2_nop2.flags    =
1949                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1950                         pEnd->write2_nop2.cda=0;
1951                         pEnd->write2_nop2.count=1;
1952                 }
1953                 else {  /*  end of if (pEnd->write1)*/
1954                         pEnd->write1=0x01;   /* first end ccw is now active */
1955                         /*      set up Tic CCWs         */
1956                         p_last_ccw->w_TIC_1.cda=
1957                                 (__u32)__pa(&pEnd->write1_nop1);
1958                         pEnd->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
1959                         pEnd->write1_nop2.flags    =
1960                                 CCW_FLAG_SLI | CCW_FLAG_SKIP;
1961                         pEnd->write1_nop2.cda=0;
1962                         pEnd->write1_nop2.count=1;
1963                 }  /* end if if (pEnd->write1) */
1964
1965
1966                 if (privptr->p_write_active_first==NULL ) {
1967                         privptr->p_write_active_first=p_first_ccw;
1968                         privptr->p_write_active_last=p_last_ccw;
1969                 }
1970                 else {
1971
1972                         /*      set up Tic CCWs         */
1973
1974                         tempCCW.cda=(__u32)__pa(&p_first_ccw->write);
1975                         tempCCW.count=0;
1976                         tempCCW.flags=0;
1977                         tempCCW.cmd_code=CCW_CLAW_CMD_TIC;
1978
1979                         if (pEnd->write1) {
1980
1981                  /*
1982                  * first set of ending CCW's is chained to the new write
1983                  * chain, so the second set is chained to the active chain
1984                  * Therefore modify the second set to point the new write chain.
1985                  * make sure we update the CCW atomically
1986                  * so channel does not fetch it when it's only half done
1987                  */
1988                                 memcpy( &pEnd->write2_nop2, &tempCCW ,
1989                                         sizeof(struct ccw1));
1990                                 privptr->p_write_active_last->w_TIC_1.cda=
1991                                         (__u32)__pa(&p_first_ccw->write);
1992                         }
1993                         else {
1994
1995                         /*make sure we update the CCW atomically
1996                          *so channel does not fetch it when it's only half done
1997                          */
1998                                 memcpy(&pEnd->write1_nop2, &tempCCW ,
1999                                         sizeof(struct ccw1));
2000                                 privptr->p_write_active_last->w_TIC_1.cda=
2001                                         (__u32)__pa(&p_first_ccw->write);
2002
2003                         } /* end if if (pEnd->write1) */
2004
2005                         privptr->p_write_active_last->next=p_first_ccw;
2006                         privptr->p_write_active_last=p_last_ccw;
2007                 }
2008
2009         } /* endif (p_first_ccw!=NULL)  */
2010
2011
2012 #ifdef IOTRACE
2013         printk(KERN_INFO "%s: %s() >  Dump Active CCW chain \n",
2014                 dev->name,__func__);
2015         p_buf=privptr->p_write_active_first;
2016         while (p_buf!=NULL) {
2017                 dumpit((char *)p_buf, sizeof(struct ccwbk));
2018                 p_buf=p_buf->next;
2019         }
2020         p_buf=(struct ccwbk*)privptr->p_end_ccw;
2021         dumpit((char *)p_buf, sizeof(struct endccw));
2022 #endif
2023         dev_kfree_skb_any(skb);
2024         if (linkid==0) {
2025                 lock=LOCK_NO;
2026         }
2027         else  {
2028                 lock=LOCK_YES;
2029         }
2030         claw_strt_out_IO(dev );
2031         /*      if write free count is zero , set NOBUFFER       */
2032 #ifdef DEBUGMSG
2033         printk(KERN_INFO "%s: %s() > free_count is %d\n",
2034                 dev->name,__func__,
2035                 (int) privptr->write_free_count );
2036 #endif
2037         if (privptr->write_free_count==0) {
2038                 claw_setbit_busy(TB_NOBUFFER,dev);
2039         }
2040 Done2:
2041         claw_clearbit_busy(TB_TX,dev);
2042 Done:
2043 #ifdef FUNCTRACE
2044         printk(KERN_INFO "%s: %s() > exit on line %d, rc = %d \n",
2045                 dev->name,__func__,__LINE__, rc);
2046 #endif
2047         return(rc);
2048 }    /*    end of claw_hw_tx    */
2049
2050 /*-------------------------------------------------------------------*
2051 *                                                                    *
2052 *     init_ccw_bk                                                    *
2053 *                                                                    *
2054 *--------------------------------------------------------------------*/
2055
2056 static int
2057 init_ccw_bk(struct net_device *dev)
2058 {
2059
2060         __u32   ccw_blocks_required;
2061         __u32   ccw_blocks_perpage;
2062         __u32   ccw_pages_required;
2063         __u32   claw_reads_perpage=1;
2064         __u32   claw_read_pages;
2065         __u32   claw_writes_perpage=1;
2066         __u32   claw_write_pages;
2067         void    *p_buff=NULL;
2068         struct ccwbk*p_free_chain;
2069         struct ccwbk*p_buf;
2070         struct ccwbk*p_last_CCWB;
2071         struct ccwbk*p_first_CCWB;
2072         struct endccw *p_endccw=NULL;
2073         addr_t  real_address;
2074         struct claw_privbk *privptr=dev->priv;
2075         struct clawh *pClawH=NULL;
2076         addr_t   real_TIC_address;
2077         int i,j;
2078 #ifdef FUNCTRACE
2079         printk(KERN_INFO "%s: %s() enter  \n",dev->name,__func__);
2080 #endif
2081         CLAW_DBF_TEXT(4,trace,"init_ccw");
2082 #ifdef DEBUGMSG
2083         printk(KERN_INFO "%s: variable dev =\n",dev->name);
2084         dumpit((char *) dev, sizeof(struct net_device));
2085 #endif
2086
2087         /*  initialize  statistics field */
2088         privptr->active_link_ID=0;
2089         /*  initialize  ccwbk pointers  */
2090         privptr->p_write_free_chain=NULL;   /* pointer to free ccw chain*/
2091         privptr->p_write_active_first=NULL; /* pointer to the first write ccw*/
2092         privptr->p_write_active_last=NULL;  /* pointer to the last write ccw*/
2093         privptr->p_read_active_first=NULL;  /* pointer to the first read ccw*/
2094         privptr->p_read_active_last=NULL;   /* pointer to the last read ccw */
2095         privptr->p_end_ccw=NULL;            /* pointer to ending ccw        */
2096         privptr->p_claw_signal_blk=NULL;    /* pointer to signal block      */
2097         privptr->buffs_alloc = 0;
2098         memset(&privptr->end_ccw, 0x00, sizeof(struct endccw));
2099         memset(&privptr->ctl_bk, 0x00, sizeof(struct clawctl));
2100         /*  initialize  free write ccwbk counter  */
2101         privptr->write_free_count=0;  /* number of free bufs on write chain */
2102         p_last_CCWB = NULL;
2103         p_first_CCWB= NULL;
2104         /*
2105         *  We need 1 CCW block for each read buffer, 1 for each
2106         *  write buffer, plus 1 for ClawSignalBlock
2107         */
2108         ccw_blocks_required =
2109                 privptr->p_env->read_buffers+privptr->p_env->write_buffers+1;
2110 #ifdef DEBUGMSG
2111         printk(KERN_INFO "%s: %s() "
2112                 "ccw_blocks_required=%d\n",
2113                 dev->name,__func__,
2114                 ccw_blocks_required);
2115         printk(KERN_INFO "%s: %s() "
2116                 "PAGE_SIZE=0x%x\n",
2117                 dev->name,__func__,
2118                 (unsigned int)PAGE_SIZE);
2119         printk(KERN_INFO "%s: %s() > "
2120                 "PAGE_MASK=0x%x\n",
2121                 dev->name,__func__,
2122                 (unsigned int)PAGE_MASK);
2123 #endif
2124         /*
2125         * compute number of CCW blocks that will fit in a page
2126         */
2127         ccw_blocks_perpage= PAGE_SIZE /  CCWBK_SIZE;
2128         ccw_pages_required=
2129                 DIV_ROUND_UP(ccw_blocks_required, ccw_blocks_perpage);
2130
2131 #ifdef DEBUGMSG
2132         printk(KERN_INFO "%s: %s() > ccw_blocks_perpage=%d\n",
2133                 dev->name,__func__,
2134                 ccw_blocks_perpage);
2135         printk(KERN_INFO "%s: %s() > ccw_pages_required=%d\n",
2136                 dev->name,__func__,
2137                 ccw_pages_required);
2138 #endif
2139         /*
2140          *  read and write sizes are set by 2 constants in claw.h
2141          *  4k and 32k.  Unpacked values other than 4k are not going to
2142          * provide good performance. With packing buffers support 32k
2143          * buffers are used.
2144          */
2145         if (privptr->p_env->read_size < PAGE_SIZE) {
2146                 claw_reads_perpage = PAGE_SIZE / privptr->p_env->read_size;
2147                 claw_read_pages = DIV_ROUND_UP(privptr->p_env->read_buffers,
2148                                                 claw_reads_perpage);
2149          }
2150          else {       /* > or equal  */
2151                 privptr->p_buff_pages_perread =
2152                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
2153                 claw_read_pages = privptr->p_env->read_buffers *
2154                                         privptr->p_buff_pages_perread;
2155          }
2156         if (privptr->p_env->write_size < PAGE_SIZE) {
2157                 claw_writes_perpage =
2158                         PAGE_SIZE / privptr->p_env->write_size;
2159                 claw_write_pages = DIV_ROUND_UP(privptr->p_env->write_buffers,
2160                                                 claw_writes_perpage);
2161
2162         }
2163         else {      /* >  or equal  */
2164                 privptr->p_buff_pages_perwrite =
2165                         DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
2166                 claw_write_pages = privptr->p_env->write_buffers *
2167                                         privptr->p_buff_pages_perwrite;
2168         }
2169 #ifdef DEBUGMSG
2170         if (privptr->p_env->read_size < PAGE_SIZE) {
2171             printk(KERN_INFO "%s: %s() reads_perpage=%d\n",
2172                 dev->name,__func__,
2173                 claw_reads_perpage);
2174         }
2175         else {
2176             printk(KERN_INFO "%s: %s() pages_perread=%d\n",
2177                 dev->name,__func__,
2178                 privptr->p_buff_pages_perread);
2179         }
2180         printk(KERN_INFO "%s: %s() read_pages=%d\n",
2181                 dev->name,__func__,
2182                 claw_read_pages);
2183         if (privptr->p_env->write_size < PAGE_SIZE) {
2184             printk(KERN_INFO "%s: %s() writes_perpage=%d\n",
2185                 dev->name,__func__,
2186                 claw_writes_perpage);
2187         }
2188         else {
2189             printk(KERN_INFO "%s: %s() pages_perwrite=%d\n",
2190                 dev->name,__func__,
2191                 privptr->p_buff_pages_perwrite);
2192         }
2193         printk(KERN_INFO "%s: %s() write_pages=%d\n",
2194                 dev->name,__func__,
2195                 claw_write_pages);
2196 #endif
2197
2198
2199         /*
2200         *               allocate ccw_pages_required
2201         */
2202         if (privptr->p_buff_ccw==NULL) {
2203                 privptr->p_buff_ccw=
2204                         (void *)__get_free_pages(__GFP_DMA,
2205                         (int)pages_to_order_of_mag(ccw_pages_required ));
2206                 if (privptr->p_buff_ccw==NULL) {
2207                         printk(KERN_INFO "%s: %s()  "
2208                                 "__get_free_pages for CCWs failed : "
2209                                 "pages is %d\n",
2210                                 dev->name,__func__,
2211                                 ccw_pages_required );
2212 #ifdef FUNCTRACE
2213                         printk(KERN_INFO "%s: %s() > "
2214                                 "exit on line %d, rc = ENOMEM\n",
2215                                 dev->name,__func__,
2216                                  __LINE__);
2217 #endif
2218                         return -ENOMEM;
2219                 }
2220                 privptr->p_buff_ccw_num=ccw_pages_required;
2221         }
2222         memset(privptr->p_buff_ccw, 0x00,
2223                 privptr->p_buff_ccw_num * PAGE_SIZE);
2224
2225         /*
2226         *               obtain ending ccw block address
2227         *
2228         */
2229         privptr->p_end_ccw = (struct endccw *)&privptr->end_ccw;
2230         real_address  = (__u32)__pa(privptr->p_end_ccw);
2231         /*                              Initialize ending CCW block       */
2232 #ifdef DEBUGMSG
2233         printk(KERN_INFO "%s: %s() begin initialize ending CCW blocks\n",
2234                 dev->name,__func__);
2235 #endif
2236
2237         p_endccw=privptr->p_end_ccw;
2238         p_endccw->real=real_address;
2239         p_endccw->write1=0x00;
2240         p_endccw->read1=0x00;
2241
2242         /*      write1_nop1                                     */
2243         p_endccw->write1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
2244         p_endccw->write1_nop1.flags       = CCW_FLAG_SLI | CCW_FLAG_CC;
2245         p_endccw->write1_nop1.count       = 1;
2246         p_endccw->write1_nop1.cda         = 0;
2247
2248         /*      write1_nop2                                     */
2249         p_endccw->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
2250         p_endccw->write1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
2251         p_endccw->write1_nop2.count      = 1;
2252         p_endccw->write1_nop2.cda        = 0;
2253
2254         /*      write2_nop1                                     */
2255         p_endccw->write2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
2256         p_endccw->write2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
2257         p_endccw->write2_nop1.count        = 1;
2258         p_endccw->write2_nop1.cda          = 0;
2259
2260         /*      write2_nop2                                     */
2261         p_endccw->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
2262         p_endccw->write2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
2263         p_endccw->write2_nop2.count        = 1;
2264         p_endccw->write2_nop2.cda          = 0;
2265
2266         /*      read1_nop1                                      */
2267         p_endccw->read1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
2268         p_endccw->read1_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
2269         p_endccw->read1_nop1.count        = 1;
2270         p_endccw->read1_nop1.cda          = 0;
2271
2272         /*      read1_nop2                                      */
2273         p_endccw->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
2274         p_endccw->read1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
2275         p_endccw->read1_nop2.count        = 1;
2276         p_endccw->read1_nop2.cda          = 0;
2277
2278         /*      read2_nop1                                      */
2279         p_endccw->read2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
2280         p_endccw->read2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
2281         p_endccw->read2_nop1.count        = 1;
2282         p_endccw->read2_nop1.cda          = 0;
2283
2284         /*      read2_nop2                                      */
2285         p_endccw->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
2286         p_endccw->read2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
2287         p_endccw->read2_nop2.count        = 1;
2288         p_endccw->read2_nop2.cda          = 0;
2289
2290 #ifdef IOTRACE
2291         printk(KERN_INFO "%s: %s() dump claw ending CCW BK \n",
2292                 dev->name,__func__);
2293         dumpit((char *)p_endccw, sizeof(struct endccw));
2294 #endif
2295
2296         /*
2297         *                               Build a chain of CCWs
2298         *
2299         */
2300
2301 #ifdef DEBUGMSG
2302         printk(KERN_INFO "%s: %s()  Begin build a chain of CCW buffer \n",
2303                 dev->name,__func__);
2304 #endif
2305         p_buff=privptr->p_buff_ccw;
2306
2307         p_free_chain=NULL;
2308         for (i=0 ; i < ccw_pages_required; i++ ) {
2309                 real_address  = (__u32)__pa(p_buff);
2310                 p_buf=p_buff;
2311                 for (j=0 ; j < ccw_blocks_perpage ; j++) {
2312                         p_buf->next  = p_free_chain;
2313                         p_free_chain = p_buf;
2314                         p_buf->real=(__u32)__pa(p_buf);
2315                         ++p_buf;
2316                 }
2317                 p_buff+=PAGE_SIZE;
2318         }
2319 #ifdef DEBUGMSG
2320         printk(KERN_INFO "%s: %s() "
2321                 "End build a chain of CCW buffer \n",
2322                         dev->name,__func__);
2323         p_buf=p_free_chain;
2324         while (p_buf!=NULL) {
2325                 dumpit((char *)p_buf, sizeof(struct ccwbk));
2326                 p_buf=p_buf->next;
2327         }
2328 #endif
2329
2330         /*
2331         *                               Initialize ClawSignalBlock
2332         *
2333         */
2334 #ifdef DEBUGMSG
2335         printk(KERN_INFO "%s: %s() "
2336                 "Begin initialize ClawSignalBlock \n",
2337                 dev->name,__func__);
2338 #endif
2339         if (privptr->p_claw_signal_blk==NULL) {
2340                 privptr->p_claw_signal_blk=p_free_chain;
2341                 p_free_chain=p_free_chain->next;
2342                 pClawH=(struct clawh *)privptr->p_claw_signal_blk;
2343                 pClawH->length=0xffff;
2344                 pClawH->opcode=0xff;
2345                 pClawH->flag=CLAW_BUSY;
2346         }
2347 #ifdef DEBUGMSG
2348         printk(KERN_INFO "%s: %s() >  End initialize "
2349                 "ClawSignalBlock\n",
2350                 dev->name,__func__);
2351         dumpit((char *)privptr->p_claw_signal_blk, sizeof(struct ccwbk));
2352 #endif
2353
2354         /*
2355         *               allocate write_pages_required and add to free chain
2356         */
2357         if (privptr->p_buff_write==NULL) {
2358             if (privptr->p_env->write_size < PAGE_SIZE) {
2359                 privptr->p_buff_write=
2360                         (void *)__get_free_pages(__GFP_DMA,
2361                         (int)pages_to_order_of_mag(claw_write_pages ));
2362                 if (privptr->p_buff_write==NULL) {
2363                         printk(KERN_INFO "%s: %s() __get_free_pages for write"
2364                                 " bufs failed : get is for %d pages\n",
2365                                 dev->name,__func__,claw_write_pages );
2366                         free_pages((unsigned long)privptr->p_buff_ccw,
2367                            (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
2368                         privptr->p_buff_ccw=NULL;
2369 #ifdef FUNCTRACE
2370                         printk(KERN_INFO "%s: %s() > exit on line %d,"
2371                                 "rc = ENOMEM\n",
2372                                 dev->name,__func__,__LINE__);
2373 #endif
2374                         return -ENOMEM;
2375                 }
2376                 /*
2377                 *                               Build CLAW write free chain
2378                 *
2379                 */
2380
2381                 memset(privptr->p_buff_write, 0x00,
2382                         ccw_pages_required * PAGE_SIZE);
2383 #ifdef DEBUGMSG
2384                 printk(KERN_INFO "%s: %s() Begin build claw write free "
2385                         "chain \n",dev->name,__func__);
2386 #endif
2387                 privptr->p_write_free_chain=NULL;
2388
2389                 p_buff=privptr->p_buff_write;
2390
2391                 for (i=0 ; i< privptr->p_env->write_buffers ; i++) {
2392                         p_buf        = p_free_chain;      /*  get a CCW */
2393                         p_free_chain = p_buf->next;
2394                         p_buf->next  =privptr->p_write_free_chain;
2395                         privptr->p_write_free_chain = p_buf;
2396                         p_buf-> p_buffer        = (struct clawbuf *)p_buff;
2397                         p_buf-> write.cda       = (__u32)__pa(p_buff);
2398                         p_buf-> write.flags     = CCW_FLAG_SLI | CCW_FLAG_CC;
2399                         p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
2400                         p_buf-> w_read_FF.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
2401                         p_buf-> w_read_FF.count   = 1;
2402                         p_buf-> w_read_FF.cda     =
2403                                 (__u32)__pa(&p_buf-> header.flag);
2404                         p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
2405                         p_buf-> w_TIC_1.flags      = 0;
2406                         p_buf-> w_TIC_1.count      = 0;
2407
2408                         if (((unsigned long)p_buff+privptr->p_env->write_size) >=
2409                            ((unsigned long)(p_buff+2*
2410                                 (privptr->p_env->write_size) -1) & PAGE_MASK)) {
2411                         p_buff= p_buff+privptr->p_env->write_size;
2412                         }
2413                 }
2414            }
2415            else      /*  Buffers are => PAGE_SIZE. 1 buff per get_free_pages */
2416            {
2417                privptr->p_write_free_chain=NULL;
2418                for (i = 0; i< privptr->p_env->write_buffers ; i++) {
2419                    p_buff=(void *)__get_free_pages(__GFP_DMA,
2420                         (int)pages_to_order_of_mag(
2421                         privptr->p_buff_pages_perwrite) );
2422 #ifdef IOTRACE
2423                    printk(KERN_INFO "%s:%s __get_free_pages "
2424                     "for writes buf: get for %d pages\n",
2425                     dev->name,__func__,
2426                     privptr->p_buff_pages_perwrite);
2427 #endif
2428                    if (p_buff==NULL) {
2429                         printk(KERN_INFO "%s:%s __get_free_pages "
2430                                 "for writes buf failed : get is for %d pages\n",
2431                                 dev->name,
2432                                 __func__,
2433                                 privptr->p_buff_pages_perwrite );
2434                         free_pages((unsigned long)privptr->p_buff_ccw,
2435                               (int)pages_to_order_of_mag(
2436                                         privptr->p_buff_ccw_num));
2437                         privptr->p_buff_ccw=NULL;
2438                         p_buf=privptr->p_buff_write;
2439                         while (p_buf!=NULL) {
2440                                 free_pages((unsigned long)
2441                                         p_buf->p_buffer,
2442                                         (int)pages_to_order_of_mag(
2443                                         privptr->p_buff_pages_perwrite));
2444                                 p_buf=p_buf->next;
2445                         }
2446 #ifdef FUNCTRACE
2447                         printk(KERN_INFO "%s: %s exit on line %d, rc = ENOMEM\n",
2448                         dev->name,
2449                         __func__,
2450                         __LINE__);
2451 #endif
2452                         return -ENOMEM;
2453                    }  /* Error on get_pages   */
2454                    memset(p_buff, 0x00, privptr->p_env->write_size );
2455                    p_buf         = p_free_chain;
2456                    p_free_chain  = p_buf->next;
2457                    p_buf->next   = privptr->p_write_free_chain;
2458                    privptr->p_write_free_chain = p_buf;
2459                    privptr->p_buff_write = p_buf;
2460                    p_buf->p_buffer=(struct clawbuf *)p_buff;
2461                    p_buf-> write.cda     = (__u32)__pa(p_buff);
2462                    p_buf-> write.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
2463                    p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
2464                    p_buf-> w_read_FF.flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
2465                    p_buf-> w_read_FF.count    = 1;
2466                    p_buf-> w_read_FF.cda      =
2467                         (__u32)__pa(&p_buf-> header.flag);
2468                    p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
2469                    p_buf-> w_TIC_1.flags   = 0;
2470                    p_buf-> w_TIC_1.count   = 0;
2471                }  /* for all write_buffers   */
2472
2473            }    /* else buffers are PAGE_SIZE or bigger */
2474
2475         }
2476         privptr->p_buff_write_num=claw_write_pages;
2477         privptr->write_free_count=privptr->p_env->write_buffers;
2478
2479
2480 #ifdef DEBUGMSG
2481         printk(KERN_INFO "%s:%s  End build claw write free chain \n",
2482         dev->name,__func__);
2483         p_buf=privptr->p_write_free_chain;
2484         while (p_buf!=NULL) {
2485                 dumpit((char *)p_buf, sizeof(struct ccwbk));
2486                 p_buf=p_buf->next;
2487         }
2488 #endif
2489         /*
2490         *               allocate read_pages_required and chain to free chain
2491         */
2492         if (privptr->p_buff_read==NULL) {
2493             if (privptr->p_env->read_size < PAGE_SIZE)  {
2494                 privptr->p_buff_read=
2495                         (void *)__get_free_pages(__GFP_DMA,
2496                         (int)pages_to_order_of_mag(claw_read_pages) );
2497                 if (privptr->p_buff_read==NULL) {
2498                         printk(KERN_INFO "%s: %s() "
2499                                 "__get_free_pages for read buf failed : "
2500                                 "get is for %d pages\n",
2501                                 dev->name,__func__,claw_read_pages );
2502                         free_pages((unsigned long)privptr->p_buff_ccw,
2503                                 (int)pages_to_order_of_mag(
2504                                         privptr->p_buff_ccw_num));
2505                         /* free the write pages size is < page size  */
2506                         free_pages((unsigned long)privptr->p_buff_write,
2507                                 (int)pages_to_order_of_mag(
2508                                 privptr->p_buff_write_num));
2509                         privptr->p_buff_ccw=NULL;
2510                         privptr->p_buff_write=NULL;
2511 #ifdef FUNCTRACE
2512                         printk(KERN_INFO "%s: %s() > exit on line %d, rc ="
2513                                 " ENOMEM\n",dev->name,__func__,__LINE__);
2514 #endif
2515                         return -ENOMEM;
2516                 }
2517                 memset(privptr->p_buff_read, 0x00, claw_read_pages * PAGE_SIZE);
2518                 privptr->p_buff_read_num=claw_read_pages;
2519                 /*
2520                 *                               Build CLAW read free chain
2521                 *
2522                 */
2523 #ifdef DEBUGMSG
2524                 printk(KERN_INFO "%s: %s() Begin build claw read free chain \n",
2525                         dev->name,__func__);
2526 #endif
2527                 p_buff=privptr->p_buff_read;
2528                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
2529                         p_buf        = p_free_chain;
2530                         p_free_chain = p_buf->next;
2531
2532                         if (p_last_CCWB==NULL) {
2533                                 p_buf->next=NULL;
2534                                 real_TIC_address=0;
2535                                 p_last_CCWB=p_buf;
2536                         }
2537                         else {
2538                                 p_buf->next=p_first_CCWB;
2539                                 real_TIC_address=
2540                                 (__u32)__pa(&p_first_CCWB -> read );
2541                         }
2542
2543                         p_first_CCWB=p_buf;
2544
2545                         p_buf->p_buffer=(struct clawbuf *)p_buff;
2546                         /*  initialize read command */
2547                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
2548                         p_buf-> read.cda = (__u32)__pa(p_buff);
2549                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2550                         p_buf-> read.count       = privptr->p_env->read_size;
2551
2552                         /*  initialize read_h command */
2553                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
2554                         p_buf-> read_h.cda =
2555                                 (__u32)__pa(&(p_buf->header));
2556                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2557                         p_buf-> read_h.count      = sizeof(struct clawh);
2558
2559                         /*  initialize Signal command */
2560                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
2561                         p_buf-> signal.cda =
2562                                 (__u32)__pa(&(pClawH->flag));
2563                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2564                         p_buf-> signal.count     = 1;
2565
2566                         /*  initialize r_TIC_1 command */
2567                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
2568                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
2569                         p_buf-> r_TIC_1.flags = 0;
2570                         p_buf-> r_TIC_1.count      = 0;
2571
2572                         /*  initialize r_read_FF command */
2573                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
2574                         p_buf-> r_read_FF.cda =
2575                                 (__u32)__pa(&(pClawH->flag));
2576                         p_buf-> r_read_FF.flags =
2577                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
2578                         p_buf-> r_read_FF.count    = 1;
2579
2580                         /*    initialize r_TIC_2          */
2581                         memcpy(&p_buf->r_TIC_2,
2582                                 &p_buf->r_TIC_1, sizeof(struct ccw1));
2583
2584                         /*     initialize Header     */
2585                         p_buf->header.length=0xffff;
2586                         p_buf->header.opcode=0xff;
2587                         p_buf->header.flag=CLAW_PENDING;
2588
2589                         if (((unsigned long)p_buff+privptr->p_env->read_size) >=
2590                                 ((unsigned long)(p_buff+2*(privptr->p_env->read_size) -1)
2591                                  & PAGE_MASK) ) {
2592                                 p_buff= p_buff+privptr->p_env->read_size;
2593                         }
2594                         else {
2595                                 p_buff=
2596                                 (void *)((unsigned long)
2597                                         (p_buff+2*(privptr->p_env->read_size) -1)
2598                                          & PAGE_MASK) ;
2599                         }
2600                 }   /* for read_buffers   */
2601           }         /* read_size < PAGE_SIZE  */
2602           else {  /* read Size >= PAGE_SIZE  */
2603
2604 #ifdef DEBUGMSG
2605         printk(KERN_INFO "%s: %s() Begin build claw read free chain \n",
2606                 dev->name,__func__);
2607 #endif
2608                 for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
2609                         p_buff = (void *)__get_free_pages(__GFP_DMA,
2610                                 (int)pages_to_order_of_mag(privptr->p_buff_pages_perread) );
2611                         if (p_buff==NULL) {
2612                                 printk(KERN_INFO "%s: %s() __get_free_pages for read "
2613                                         "buf failed : get is for %d pages\n",
2614                                         dev->name,__func__,
2615                                         privptr->p_buff_pages_perread );
2616                                 free_pages((unsigned long)privptr->p_buff_ccw,
2617                                         (int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
2618                                 /* free the write pages  */
2619                                 p_buf=privptr->p_buff_write;
2620                                 while (p_buf!=NULL) {
2621                                         free_pages((unsigned long)p_buf->p_buffer,
2622                                                 (int)pages_to_order_of_mag(
2623                                                 privptr->p_buff_pages_perwrite ));
2624                                         p_buf=p_buf->next;
2625                                 }
2626                                 /* free any read pages already alloc  */
2627                                 p_buf=privptr->p_buff_read;
2628                                 while (p_buf!=NULL) {
2629                                         free_pages((unsigned long)p_buf->p_buffer,
2630                                                 (int)pages_to_order_of_mag(
2631                                                 privptr->p_buff_pages_perread ));
2632                                         p_buf=p_buf->next;
2633                                 }
2634                                 privptr->p_buff_ccw=NULL;
2635                                 privptr->p_buff_write=NULL;
2636 #ifdef FUNCTRACE
2637                                 printk(KERN_INFO "%s: %s() exit on line %d, rc = ENOMEM\n",
2638                                         dev->name,__func__,
2639                                         __LINE__);
2640 #endif
2641                                 return -ENOMEM;
2642                         }
2643                         memset(p_buff, 0x00, privptr->p_env->read_size);
2644                         p_buf        = p_free_chain;
2645                         privptr->p_buff_read = p_buf;
2646                         p_free_chain = p_buf->next;
2647
2648                         if (p_last_CCWB==NULL) {
2649                                 p_buf->next=NULL;
2650                                 real_TIC_address=0;
2651                                 p_last_CCWB=p_buf;
2652                         }
2653                         else {
2654                                 p_buf->next=p_first_CCWB;
2655                                 real_TIC_address=
2656                                         (addr_t)__pa(
2657                                                 &p_first_CCWB -> read );
2658                         }
2659
2660                         p_first_CCWB=p_buf;
2661                                 /* save buff address */
2662                         p_buf->p_buffer=(struct clawbuf *)p_buff;
2663                         /*  initialize read command */
2664                         p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
2665                         p_buf-> read.cda = (__u32)__pa(p_buff);
2666                         p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2667                         p_buf-> read.count       = privptr->p_env->read_size;
2668
2669                         /*  initialize read_h command */
2670                         p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
2671                         p_buf-> read_h.cda =
2672                                 (__u32)__pa(&(p_buf->header));
2673                         p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2674                         p_buf-> read_h.count      = sizeof(struct clawh);
2675
2676                         /*  initialize Signal command */
2677                         p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
2678                         p_buf-> signal.cda =
2679                                 (__u32)__pa(&(pClawH->flag));
2680                         p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
2681                         p_buf-> signal.count     = 1;
2682
2683                         /*  initialize r_TIC_1 command */
2684                         p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
2685                         p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
2686                         p_buf-> r_TIC_1.flags = 0;
2687                         p_buf-> r_TIC_1.count      = 0;
2688
2689                         /*  initialize r_read_FF command */
2690                         p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
2691                         p_buf-> r_read_FF.cda =
2692                                 (__u32)__pa(&(pClawH->flag));
2693                         p_buf-> r_read_FF.flags =
2694                                 CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
2695                         p_buf-> r_read_FF.count    = 1;
2696
2697                         /*    initialize r_TIC_2          */
2698                         memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1,
2699                                 sizeof(struct ccw1));
2700
2701                         /*     initialize Header     */
2702                         p_buf->header.length=0xffff;
2703                         p_buf->header.opcode=0xff;
2704                         p_buf->header.flag=CLAW_PENDING;
2705
2706                 }    /* For read_buffers   */
2707           }     /*  read_size >= PAGE_SIZE   */
2708         }       /*  pBuffread = NULL */
2709 #ifdef DEBUGMSG
2710         printk(KERN_INFO "%s: %s() >  End build claw read free chain \n",
2711                 dev->name,__func__);
2712         p_buf=p_first_CCWB;
2713         while (p_buf!=NULL) {
2714                 dumpit((char *)p_buf, sizeof(struct ccwbk));
2715                 p_buf=p_buf->next;
2716         }
2717
2718 #endif
2719         add_claw_reads( dev  ,p_first_CCWB , p_last_CCWB);
2720         privptr->buffs_alloc = 1;
2721 #ifdef FUNCTRACE
2722         printk(KERN_INFO "%s: %s() exit on line %d\n",
2723                 dev->name,__func__,__LINE__);
2724 #endif
2725         return 0;
2726 }    /*    end of init_ccw_bk */
2727
2728 /*-------------------------------------------------------------------*
2729 *                                                                    *
2730 *       probe_error                                                  *
2731 *                                                                    *
2732 *--------------------------------------------------------------------*/
2733
2734 static void
2735 probe_error( struct ccwgroup_device *cgdev)
2736 {
2737   struct claw_privbk *privptr;
2738 #ifdef FUNCTRACE
2739         printk(KERN_INFO "%s enter  \n",__func__);
2740 #endif
2741         CLAW_DBF_TEXT(4,trace,"proberr");
2742 #ifdef DEBUGMSG
2743         printk(KERN_INFO "%s variable cgdev =\n",__func__);
2744         dumpit((char *) cgdev, sizeof(struct ccwgroup_device));
2745 #endif
2746         privptr=(struct claw_privbk *)cgdev->dev.driver_data;
2747         if (privptr!=NULL) {
2748                 kfree(privptr->p_env);
2749                 privptr->p_env=NULL;
2750                 kfree(privptr->p_mtc_envelope);
2751                 privptr->p_mtc_envelope=NULL;
2752                 kfree(privptr);
2753                 privptr=NULL;
2754         }
2755 #ifdef FUNCTRACE
2756         printk(KERN_INFO "%s > exit on line %d\n",
2757                  __func__,__LINE__);
2758 #endif
2759
2760         return;
2761 }    /*    probe_error    */
2762
2763
2764
2765 /*-------------------------------------------------------------------*
2766 *    claw_process_control                                            *
2767 *                                                                    *
2768 *                                                                    *
2769 *--------------------------------------------------------------------*/
2770
2771 static int
2772 claw_process_control( struct net_device *dev, struct ccwbk * p_ccw)
2773 {
2774
2775         struct clawbuf *p_buf;
2776         struct clawctl  ctlbk;
2777         struct clawctl *p_ctlbk;
2778         char    temp_host_name[8];
2779         char    temp_ws_name[8];
2780         struct claw_privbk *privptr;
2781         struct claw_env *p_env;
2782         struct sysval *p_sysval;
2783         struct conncmd *p_connect=NULL;
2784         int rc;
2785         struct chbk *p_ch = NULL;
2786 #ifdef FUNCTRACE
2787         printk(KERN_INFO "%s: %s() > enter  \n",
2788                 dev->name,__func__);
2789 #endif
2790         CLAW_DBF_TEXT(2,setup,"clw_cntl");
2791 #ifdef DEBUGMSG
2792         printk(KERN_INFO "%s: variable dev =\n",dev->name);
2793         dumpit((char *) dev, sizeof(struct net_device));
2794         printk(KERN_INFO "%s: variable p_ccw =\n",dev->name);
2795         dumpit((char *) p_ccw, sizeof(struct ccwbk *));
2796 #endif
2797         udelay(1000);  /* Wait a ms for the control packets to
2798                         *catch up to each other */
2799         privptr=dev->priv;
2800         p_env=privptr->p_env;
2801         memcpy( &temp_host_name, p_env->host_name, 8);
2802         memcpy( &temp_ws_name, p_env->adapter_name , 8);
2803         printk(KERN_INFO "%s: CLAW device %.8s: "
2804                 "Received Control Packet\n",
2805                 dev->name, temp_ws_name);
2806         if (privptr->release_pend==1) {
2807 #ifdef FUNCTRACE
2808                 printk(KERN_INFO "%s: %s() > "
2809                         "exit on line %d, rc=0\n",
2810                         dev->name,__func__,__LINE__);
2811 #endif
2812                 return 0;
2813         }
2814         p_buf=p_ccw->p_buffer;
2815         p_ctlbk=&ctlbk;
2816         if (p_env->packing == DO_PACKED) { /* packing in progress?*/
2817                 memcpy(p_ctlbk, &p_buf->buffer[4], sizeof(struct clawctl));
2818         } else {
2819                 memcpy(p_ctlbk, p_buf, sizeof(struct clawctl));
2820         }
2821 #ifdef IOTRACE
2822         printk(KERN_INFO "%s: dump claw control data inbound\n",dev->name);
2823         dumpit((char *)p_ctlbk, sizeof(struct clawctl));
2824 #endif
2825         switch (p_ctlbk->command)
2826         {
2827                 case SYSTEM_VALIDATE_REQUEST:
2828                         if (p_ctlbk->version!=CLAW_VERSION_ID) {
2829                                 claw_snd_sys_validate_rsp(dev, p_ctlbk,
2830                                         CLAW_RC_WRONG_VERSION );
2831                                 printk("%s: %d is wrong version id. "
2832                                         "Expected %d\n",
2833                                         dev->name, p_ctlbk->version,
2834                                         CLAW_VERSION_ID);
2835                         }
2836                         p_sysval=(struct sysval *)&(p_ctlbk->data);
2837                         printk( "%s: Recv Sys Validate Request: "
2838                                 "Vers=%d,link_id=%d,Corr=%d,WS name=%."
2839                                 "8s,Host name=%.8s\n",
2840                                 dev->name, p_ctlbk->version,
2841                                 p_ctlbk->linkid,
2842                                 p_ctlbk->correlator,
2843                                 p_sysval->WS_name,
2844                                 p_sysval->host_name);
2845                         if (0!=memcmp(temp_host_name,p_sysval->host_name,8)) {
2846                                 claw_snd_sys_validate_rsp(dev, p_ctlbk,
2847                                         CLAW_RC_NAME_MISMATCH );
2848                                 CLAW_DBF_TEXT(2,setup,"HSTBAD");
2849                                 CLAW_DBF_TEXT_(2,setup,"%s",p_sysval->host_name);
2850                                 CLAW_DBF_TEXT_(2,setup,"%s",temp_host_name);
2851                                 printk(KERN_INFO "%s:  Host name mismatch\n",
2852                                         dev->name);
2853                                 printk(KERN_INFO "%s: Received :%s: "
2854                                         "expected :%s: \n",
2855                                         dev->name,
2856                                         p_sysval->host_name,
2857                                         temp_host_name);
2858                         }
2859                         if (0!=memcmp(temp_ws_name,p_sysval->WS_name,8)) {
2860                                 claw_snd_sys_validate_rsp(dev, p_ctlbk,
2861                                         CLAW_RC_NAME_MISMATCH );
2862                                 CLAW_DBF_TEXT(2,setup,"WSNBAD");
2863                                 CLAW_DBF_TEXT_(2,setup,"%s",p_sysval->WS_name);
2864                                 CLAW_DBF_TEXT_(2,setup,"%s",temp_ws_name);
2865                                 printk(KERN_INFO "%s: WS name mismatch\n",
2866                                         dev->name);
2867                                  printk(KERN_INFO "%s: Received :%s: "
2868                                         "expected :%s: \n",
2869                                         dev->name,
2870                                         p_sysval->WS_name,
2871                                         temp_ws_name);
2872                         }
2873                         if (( p_sysval->write_frame_size < p_env->write_size) &&
2874                            ( p_env->packing == 0)) {
2875                                 claw_snd_sys_validate_rsp(dev, p_ctlbk,
2876                                         CLAW_RC_HOST_RCV_TOO_SMALL );
2877                                 printk(KERN_INFO "%s: host write size is too "
2878                                         "small\n", dev->name);
2879                                 CLAW_DBF_TEXT(2,setup,"wrtszbad");
2880                         }
2881                         if (( p_sysval->read_frame_size < p_env->read_size) &&
2882                            ( p_env->packing == 0)) {
2883                                 claw_snd_sys_validate_rsp(dev, p_ctlbk,
2884                                         CLAW_RC_HOST_RCV_TOO_SMALL );
2885                                 printk(KERN_INFO "%s: host read size is too "
2886                                         "small\n", dev->name);
2887                                 CLAW_DBF_TEXT(2,setup,"rdsizbad");
2888                         }
2889                         claw_snd_sys_validate_rsp(dev, p_ctlbk, 0 );
2890                         printk("%s: CLAW device %.8s: System validate"
2891                                 " completed.\n",dev->name, temp_ws_name);
2892                         printk("%s: sys Validate Rsize:%d Wsize:%d\n",dev->name,
2893                                 p_sysval->read_frame_size,p_sysval->write_frame_size);
2894                         privptr->system_validate_comp=1;
2895                         if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
2896                                 p_env->packing = PACKING_ASK;
2897                         }
2898                         claw_strt_conn_req(dev);
2899                         break;
2900
2901                 case SYSTEM_VALIDATE_RESPONSE:
2902                         p_sysval=(struct sysval *)&(p_ctlbk->data);
2903                         printk("%s: Recv Sys Validate Resp: Vers=%d,Corr=%d,RC=%d,"
2904                                 "WS name=%.8s,Host name=%.8s\n",
2905                                 dev->name,
2906                                 p_ctlbk->version,
2907                                 p_ctlbk->correlator,
2908                                 p_ctlbk->rc,
2909                                 p_sysval->WS_name,
2910                                 p_sysval->host_name);
2911                         switch (p_ctlbk->rc)
2912                         {
2913                                 case 0:
2914                                         printk(KERN_INFO "%s: CLAW device "
2915                                                 "%.8s: System validate "
2916                                                 "completed.\n",
2917                                                 dev->name, temp_ws_name);
2918                                         if (privptr->system_validate_comp == 0)
2919                                                 claw_strt_conn_req(dev);
2920                                         privptr->system_validate_comp=1;
2921                                         break;
2922                                 case CLAW_RC_NAME_MISMATCH:
2923                                         printk(KERN_INFO "%s: Sys Validate "
2924                                                 "Resp : Host, WS name is "
2925                                                 "mismatch\n",
2926                                                 dev->name);
2927                                         break;
2928                                 case CLAW_RC_WRONG_VERSION:
2929                                         printk(KERN_INFO "%s: Sys Validate "
2930                                                 "Resp : Wrong version\n",
2931                                                 dev->name);
2932                                         break;
2933                                 case CLAW_RC_HOST_RCV_TOO_SMALL:
2934                                         printk(KERN_INFO "%s: Sys Validate "
2935                                                 "Resp : bad frame size\n",
2936                                                 dev->name);
2937                                         break;
2938                                 default:
2939                                         printk(KERN_INFO "%s: Sys Validate "
2940                                                 "error code=%d \n",
2941                                                  dev->name, p_ctlbk->rc );
2942                                         break;
2943                         }
2944                         break;
2945
2946                 case CONNECTION_REQUEST:
2947                         p_connect=(struct conncmd *)&(p_ctlbk->data);
2948                         printk(KERN_INFO "%s: Recv Conn Req: Vers=%d,link_id=%d,"
2949                                 "Corr=%d,HOST appl=%.8s,WS appl=%.8s\n",
2950                                 dev->name,
2951                                 p_ctlbk->version,
2952                                 p_ctlbk->linkid,
2953                                 p_ctlbk->correlator,
2954                                 p_connect->host_name,
2955                                 p_connect->WS_name);
2956                         if (privptr->active_link_ID!=0 ) {
2957                                 claw_snd_disc(dev, p_ctlbk);
2958                                 printk(KERN_INFO "%s: Conn Req error : "
2959                                         "already logical link is active \n",
2960                                         dev->name);
2961                         }
2962                         if (p_ctlbk->linkid!=1 ) {
2963                                 claw_snd_disc(dev, p_ctlbk);
2964                                 printk(KERN_INFO "%s: Conn Req error : "
2965                                         "req logical link id is not 1\n",
2966                                         dev->name);
2967                         }
2968                         rc=find_link(dev,
2969                                 p_connect->host_name, p_connect->WS_name);
2970                         if (rc!=0) {
2971                                 claw_snd_disc(dev, p_ctlbk);
2972                                 printk(KERN_INFO "%s: Conn Req error : "
2973                                         "req appl name does not match\n",
2974                                          dev->name);
2975                         }
2976                         claw_send_control(dev,
2977                                 CONNECTION_CONFIRM, p_ctlbk->linkid,
2978                                 p_ctlbk->correlator,
2979                                 0, p_connect->host_name,
2980                                 p_connect->WS_name);
2981                         if (p_env->packing == PACKING_ASK) {
2982                                 printk("%s: Now Pack ask\n",dev->name);
2983                                 p_env->packing = PACK_SEND;
2984                                 claw_snd_conn_req(dev,0);
2985                         }
2986                         printk(KERN_INFO "%s: CLAW device %.8s: Connection "
2987                                 "completed link_id=%d.\n",
2988                                 dev->name, temp_ws_name,
2989                                 p_ctlbk->linkid);
2990                         privptr->active_link_ID=p_ctlbk->linkid;
2991                         p_ch=&privptr->channel[WRITE];
2992                         wake_up(&p_ch->wait);  /* wake up claw_open ( WRITE) */
2993                         break;
2994                 case CONNECTION_RESPONSE:
2995                         p_connect=(struct conncmd *)&(p_ctlbk->data);
2996                         printk(KERN_INFO "%s: Revc Conn Resp: Vers=%d,link_id=%d,"
2997                                 "Corr=%d,RC=%d,Host appl=%.8s, WS appl=%.8s\n",
2998                                 dev->name,
2999                                 p_ctlbk->version,
3000                                 p_ctlbk->linkid,
3001                                 p_ctlbk->correlator,
3002                                 p_ctlbk->rc,
3003                                 p_connect->host_name,
3004                                 p_connect->WS_name);
3005
3006                         if (p_ctlbk->rc !=0 ) {
3007                                 printk(KERN_INFO "%s: Conn Resp error: rc=%d \n",
3008                                         dev->name, p_ctlbk->rc);
3009                                 return 1;
3010                         }
3011                         rc=find_link(dev,
3012                                 p_connect->host_name, p_connect->WS_name);
3013                         if (rc!=0) {
3014                                 claw_snd_disc(dev, p_ctlbk);
3015                                 printk(KERN_INFO "%s: Conn Resp error: "
3016                                         "req appl name does not match\n",
3017                                          dev->name);
3018                         }
3019                         /* should be until CONNECTION_CONFIRM */
3020                         privptr->active_link_ID =  - (p_ctlbk->linkid);
3021                         break;
3022                 case CONNECTION_CONFIRM:
3023                         p_connect=(struct conncmd *)&(p_ctlbk->data);
3024                         printk(KERN_INFO "%s: Recv Conn Confirm:Vers=%d,link_id=%d,"
3025                                 "Corr=%d,Host appl=%.8s,WS appl=%.8s\n",
3026                         dev->name,
3027                         p_ctlbk->version,
3028                         p_ctlbk->linkid,
3029                         p_ctlbk->correlator,
3030                         p_connect->host_name,
3031                         p_connect->WS_name);
3032                         if (p_ctlbk->linkid== -(privptr->active_link_ID)) {
3033                                 privptr->active_link_ID=p_ctlbk->linkid;
3034                                 if (p_env->packing > PACKING_ASK) {
3035                                         printk(KERN_INFO "%s: Confirmed Now packing\n",dev->name);
3036                                         p_env->packing = DO_PACKED;
3037                                         }
3038                                 p_ch=&privptr->channel[WRITE];
3039                                 wake_up(&p_ch->wait);
3040                         }
3041                         else {
3042                                 printk(KERN_INFO "%s: Conn confirm: "
3043                                         "unexpected linkid=%d \n",
3044                                         dev->name, p_ctlbk->linkid);
3045                                 claw_snd_disc(dev, p_ctlbk);
3046                         }
3047                         break;
3048                 case DISCONNECT:
3049                         printk(KERN_INFO "%s: Disconnect: "
3050                                 "Vers=%d,link_id=%d,Corr=%d\n",
3051                                 dev->name, p_ctlbk->version,
3052                                 p_ctlbk->linkid, p_ctlbk->correlator);
3053                         if ((p_ctlbk->linkid == 2) &&
3054                             (p_env->packing == PACK_SEND)) {
3055                                 privptr->active_link_ID = 1;
3056                                 p_env->packing = DO_PACKED;
3057                         }
3058                         else
3059                                 privptr->active_link_ID=0;
3060                         break;
3061                 case CLAW_ERROR:
3062                         printk(KERN_INFO "%s: CLAW ERROR detected\n",
3063                                 dev->name);
3064                         break;
3065                 default:
3066                         printk(KERN_INFO "%s:  Unexpected command code=%d \n",
3067                                 dev->name,  p_ctlbk->command);
3068                         break;
3069         }
3070
3071 #ifdef FUNCTRACE
3072         printk(KERN_INFO "%s: %s() exit on line %d, rc = 0\n",
3073                 dev->name,__func__,__LINE__);
3074 #endif
3075
3076         return 0;
3077 }   /*    end of claw_process_control    */
3078
3079
3080 /*-------------------------------------------------------------------*
3081 *               claw_send_control                                    *
3082 *                                                                    *
3083 *--------------------------------------------------------------------*/
3084
3085 static int
3086 claw_send_control(struct net_device *dev, __u8 type, __u8 link,
3087          __u8 correlator, __u8 rc, char *local_name, char *remote_name)
3088 {
3089         struct claw_privbk              *privptr;
3090         struct clawctl                  *p_ctl;
3091         struct sysval                   *p_sysval;
3092         struct conncmd                  *p_connect;
3093         struct sk_buff                  *skb;
3094
3095 #ifdef FUNCTRACE
3096         printk(KERN_INFO "%s:%s > enter  \n",dev->name,__func__);
3097 #endif
3098         CLAW_DBF_TEXT(2,setup,"sndcntl");
3099 #ifdef DEBUGMSG
3100         printk(KERN_INFO "%s: Sending Control Packet \n",dev->name);
3101         printk(KERN_INFO "%s: variable type = 0x%X, link = "
3102                 "%d, correlator = %d, rc = %d\n",
3103                 dev->name,type, link, correlator, rc);
3104         printk(KERN_INFO "%s: variable local_name = %s, "
3105                 "remote_name = %s\n",dev->name, local_name, remote_name);
3106 #endif
3107         privptr=dev->priv;
3108         p_ctl=(struct clawctl *)&privptr->ctl_bk;
3109
3110         p_ctl->command=type;
3111         p_ctl->version=CLAW_VERSION_ID;
3112         p_ctl->linkid=link;
3113         p_ctl->correlator=correlator;
3114         p_ctl->rc=rc;
3115
3116         p_sysval=(struct sysval *)&p_ctl->data;
3117         p_connect=(struct conncmd *)&p_ctl->data;
3118
3119         switch (p_ctl->command) {
3120                 case SYSTEM_VALIDATE_REQUEST:
3121                 case SYSTEM_VALIDATE_RESPONSE:
3122                         memcpy(&p_sysval->host_name, local_name, 8);
3123                         memcpy(&p_sysval->WS_name, remote_name, 8);
3124                         if (privptr->p_env->packing > 0) {
3125                                 p_sysval->read_frame_size=DEF_PACK_BUFSIZE;
3126                                 p_sysval->write_frame_size=DEF_PACK_BUFSIZE;
3127                         } else {
3128                                 /* how big is the piggest group of packets */
3129                                 p_sysval->read_frame_size=privptr->p_env->read_size;
3130                                 p_sysval->write_frame_size=privptr->p_env->write_size;
3131                         }
3132                         memset(&p_sysval->reserved, 0x00, 4);
3133                         break;
3134                 case CONNECTION_REQUEST:
3135                 case CONNECTION_RESPONSE:
3136                 case CONNECTION_CONFIRM:
3137                 case DISCONNECT:
3138                         memcpy(&p_sysval->host_name, local_name, 8);
3139                         memcpy(&p_sysval->WS_name, remote_name, 8);
3140                         if (privptr->p_env->packing > 0) {
3141                         /* How big is the biggest packet */
3142                                 p_connect->reserved1[0]=CLAW_FRAME_SIZE;
3143                                 p_connect->reserved1[1]=CLAW_FRAME_SIZE;
3144                         } else {
3145                                 memset(&p_connect->reserved1, 0x00, 4);
3146                                 memset(&p_connect->reserved2, 0x00, 4);
3147                         }
3148                         break;
3149                 default:
3150                         break;
3151         }
3152
3153         /*      write Control Record to the device                   */
3154
3155
3156         skb = dev_alloc_skb(sizeof(struct clawctl));
3157         if (!skb) {
3158                 printk(  "%s:%s low on mem, returning...\n",
3159                         dev->name,__func__);
3160 #ifdef DEBUG
3161                 printk(KERN_INFO "%s:%s Exit, rc = ENOMEM\n",
3162                         dev->name,__func__);
3163 #endif
3164                 return -ENOMEM;
3165         }
3166         memcpy(skb_put(skb, sizeof(struct clawctl)),
3167                 p_ctl, sizeof(struct clawctl));
3168 #ifdef IOTRACE
3169          printk(KERN_INFO "%s: outbnd claw cntl data \n",dev->name);
3170         dumpit((char *)p_ctl,sizeof(struct clawctl));
3171 #endif
3172         if (privptr->p_env->packing >= PACK_SEND)
3173                 claw_hw_tx(skb, dev, 1);
3174         else
3175                 claw_hw_tx(skb, dev, 0);
3176 #ifdef FUNCTRACE
3177         printk(KERN_INFO "%s:%s Exit on line %d\n",
3178                 dev->name,__func__,__LINE__);
3179 #endif
3180
3181         return 0;
3182 }  /*   end of claw_send_control  */
3183
3184 /*-------------------------------------------------------------------*
3185 *               claw_snd_conn_req                                    *
3186 *                                                                    *
3187 *--------------------------------------------------------------------*/
3188 static int
3189 claw_snd_conn_req(struct net_device *dev, __u8 link)
3190 {
3191         int                rc;
3192         struct claw_privbk *privptr=dev->priv;
3193         struct clawctl     *p_ctl;
3194
3195 #ifdef FUNCTRACE
3196         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
3197 #endif
3198         CLAW_DBF_TEXT(2,setup,"snd_conn");
3199 #ifdef  DEBUGMSG
3200         printk(KERN_INFO "%s: variable link = %X, dev =\n",dev->name, link);
3201         dumpit((char *) dev, sizeof(struct net_device));
3202 #endif
3203         rc = 1;
3204         p_ctl=(struct clawctl *)&privptr->ctl_bk;
3205         p_ctl->linkid = link;
3206         if ( privptr->system_validate_comp==0x00 ) {
3207 #ifdef FUNCTRACE
3208                 printk(KERN_INFO "%s:%s Exit on line %d, rc = 1\n",
3209                         dev->name,__func__,__LINE__);
3210 #endif
3211                 return rc;
3212         }
3213         if (privptr->p_env->packing == PACKING_ASK )
3214                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
3215                         WS_APPL_NAME_PACKED, WS_APPL_NAME_PACKED);
3216         if (privptr->p_env->packing == PACK_SEND)  {
3217                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
3218                         WS_APPL_NAME_IP_NAME, WS_APPL_NAME_IP_NAME);
3219         }
3220         if (privptr->p_env->packing == 0)
3221                 rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
3222                         HOST_APPL_NAME, privptr->p_env->api_type);
3223 #ifdef FUNCTRACE
3224         printk(KERN_INFO "%s:%s Exit on line %d, rc = %d\n",
3225                 dev->name,__func__,__LINE__, rc);
3226 #endif
3227         return rc;
3228
3229 }  /*  end of claw_snd_conn_req */
3230
3231
3232 /*-------------------------------------------------------------------*
3233 *               claw_snd_disc                                        *
3234 *                                                                    *
3235 *--------------------------------------------------------------------*/
3236
3237 static int
3238 claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl)
3239 {
3240         int rc;
3241         struct conncmd *  p_connect;
3242
3243 #ifdef FUNCTRACE
3244         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3245 #endif
3246         CLAW_DBF_TEXT(2,setup,"snd_dsc");
3247 #ifdef  DEBUGMSG
3248         printk(KERN_INFO "%s: variable dev =\n",dev->name);
3249         dumpit((char *) dev, sizeof(struct net_device));
3250         printk(KERN_INFO "%s: variable p_ctl",dev->name);
3251         dumpit((char *) p_ctl, sizeof(struct clawctl));
3252 #endif
3253         p_connect=(struct conncmd *)&p_ctl->data;
3254
3255         rc=claw_send_control(dev, DISCONNECT, p_ctl->linkid,
3256                 p_ctl->correlator, 0,
3257                 p_connect->host_name, p_connect->WS_name);
3258 #ifdef FUNCTRACE
3259         printk(KERN_INFO "%s:%s Exit on line %d, rc = %d\n",
3260                 dev->name,__func__, __LINE__, rc);
3261 #endif
3262         return rc;
3263 }     /*   end of claw_snd_disc    */
3264
3265
3266 /*-------------------------------------------------------------------*
3267 *               claw_snd_sys_validate_rsp                            *
3268 *                                                                    *
3269 *--------------------------------------------------------------------*/
3270
3271 static int
3272 claw_snd_sys_validate_rsp(struct net_device *dev,
3273         struct clawctl *p_ctl, __u32 return_code)
3274 {
3275         struct claw_env *  p_env;
3276         struct claw_privbk *privptr;
3277         int    rc;
3278
3279 #ifdef FUNCTRACE
3280         printk(KERN_INFO "%s:%s Enter\n",
3281                 dev->name,__func__);
3282 #endif
3283         CLAW_DBF_TEXT(2,setup,"chkresp");
3284 #ifdef DEBUGMSG
3285         printk(KERN_INFO "%s: variable return_code = %d, dev =\n",
3286                 dev->name, return_code);
3287         dumpit((char *) dev, sizeof(struct net_device));
3288         printk(KERN_INFO "%s: variable p_ctl =\n",dev->name);
3289         dumpit((char *) p_ctl, sizeof(struct clawctl));
3290 #endif
3291         privptr = dev->priv;
3292         p_env=privptr->p_env;
3293         rc=claw_send_control(dev, SYSTEM_VALIDATE_RESPONSE,
3294                 p_ctl->linkid,
3295                 p_ctl->correlator,
3296                 return_code,
3297                 p_env->host_name,
3298                 p_env->adapter_name  );
3299 #ifdef FUNCTRACE
3300         printk(KERN_INFO "%s:%s Exit on line %d, rc = %d\n",
3301                 dev->name,__func__,__LINE__, rc);
3302 #endif
3303         return rc;
3304 }     /*    end of claw_snd_sys_validate_rsp    */
3305
3306 /*-------------------------------------------------------------------*
3307 *               claw_strt_conn_req                                   *
3308 *                                                                    *
3309 *--------------------------------------------------------------------*/
3310
3311 static int
3312 claw_strt_conn_req(struct net_device *dev )
3313 {
3314         int rc;
3315
3316 #ifdef FUNCTRACE
3317         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3318 #endif
3319         CLAW_DBF_TEXT(2,setup,"conn_req");
3320 #ifdef DEBUGMSG
3321         printk(KERN_INFO "%s: variable dev =\n",dev->name);
3322         dumpit((char *) dev, sizeof(struct net_device));
3323 #endif
3324         rc=claw_snd_conn_req(dev, 1);
3325 #ifdef FUNCTRACE
3326         printk(KERN_INFO "%s:%s Exit on line %d, rc = %d\n",
3327                 dev->name,__func__,__LINE__, rc);
3328 #endif
3329         return rc;
3330 }    /*   end of claw_strt_conn_req   */
3331
3332
3333
3334 /*-------------------------------------------------------------------*
3335  *   claw_stats                                                      *
3336  *-------------------------------------------------------------------*/
3337
3338 static struct
3339 net_device_stats *claw_stats(struct net_device *dev)
3340 {
3341         struct claw_privbk *privptr;
3342 #ifdef FUNCTRACE
3343         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3344 #endif
3345         CLAW_DBF_TEXT(4,trace,"stats");
3346         privptr = dev->priv;
3347 #ifdef FUNCTRACE
3348         printk(KERN_INFO "%s:%s Exit on line %d\n",
3349                 dev->name,__func__,__LINE__);
3350 #endif
3351         return &privptr->stats;
3352 }     /*   end of claw_stats   */
3353
3354
3355 /*-------------------------------------------------------------------*
3356 *       unpack_read                                                  *
3357 *                                                                    *
3358 *--------------------------------------------------------------------*/
3359 static void
3360 unpack_read(struct net_device *dev )
3361 {
3362         struct sk_buff *skb;
3363         struct claw_privbk *privptr;
3364         struct claw_env    *p_env;
3365         struct ccwbk    *p_this_ccw;
3366         struct ccwbk    *p_first_ccw;
3367         struct ccwbk    *p_last_ccw;
3368         struct clawph   *p_packh;
3369         void            *p_packd;
3370         struct clawctl  *p_ctlrec=NULL;
3371
3372         __u32   len_of_data;
3373         __u32   pack_off;
3374         __u8    link_num;
3375         __u8    mtc_this_frm=0;
3376         __u32   bytes_to_mov;
3377         struct chbk *p_ch = NULL;
3378         int     i=0;
3379         int     p=0;
3380
3381 #ifdef FUNCTRACE
3382         printk(KERN_INFO "%s:%s enter  \n",dev->name,__func__);
3383 #endif
3384         CLAW_DBF_TEXT(4,trace,"unpkread");
3385         p_first_ccw=NULL;
3386         p_last_ccw=NULL;
3387         p_packh=NULL;
3388         p_packd=NULL;
3389         privptr=dev->priv;
3390         p_env = privptr->p_env;
3391         p_this_ccw=privptr->p_read_active_first;
3392         i=0;
3393         while (p_this_ccw!=NULL && p_this_ccw->header.flag!=CLAW_PENDING) {
3394 #ifdef IOTRACE
3395                 printk(KERN_INFO "%s p_this_ccw \n",dev->name);
3396                 dumpit((char*)p_this_ccw, sizeof(struct ccwbk));
3397                 printk(KERN_INFO "%s Inbound p_this_ccw->p_buffer(64)"
3398                         " pk=%d \n",dev->name,p_env->packing);
3399                 dumpit((char *)p_this_ccw->p_buffer, 64 );
3400 #endif
3401                 pack_off = 0;
3402                 p = 0;
3403                 p_this_ccw->header.flag=CLAW_PENDING;
3404                 privptr->p_read_active_first=p_this_ccw->next;
3405                 p_this_ccw->next=NULL;
3406                 p_packh = (struct clawph *)p_this_ccw->p_buffer;
3407                 if ((p_env->packing == PACK_SEND) &&
3408                     (p_packh->len == 32)           &&
3409                     (p_packh->link_num == 0)) {   /* is it a packed ctl rec? */
3410                         p_packh++;  /* peek past pack header */
3411                         p_ctlrec = (struct clawctl *)p_packh;
3412                         p_packh--;  /* un peek */
3413                         if ((p_ctlrec->command == CONNECTION_RESPONSE) ||
3414                             (p_ctlrec->command == CONNECTION_CONFIRM))
3415                                 p_env->packing = DO_PACKED;
3416                 }
3417                 if (p_env->packing == DO_PACKED)
3418                         link_num=p_packh->link_num;
3419                 else
3420                         link_num=p_this_ccw->header.opcode / 8;
3421                 if ((p_this_ccw->header.opcode & MORE_to_COME_FLAG)!=0) {
3422 #ifdef DEBUGMSG
3423                         printk(KERN_INFO "%s: %s > More_to_come is ON\n",
3424                         dev->name,__func__);
3425 #endif
3426                         mtc_this_frm=1;
3427                         if (p_this_ccw->header.length!=
3428                                 privptr->p_env->read_size ) {
3429                                 printk(KERN_INFO " %s: Invalid frame detected "
3430                                         "length is %02x\n" ,
3431                                         dev->name, p_this_ccw->header.length);
3432                         }
3433                 }
3434
3435                 if (privptr->mtc_skipping) {
3436                         /*
3437                         *   We're in the mode of skipping past a
3438                         *   multi-frame message
3439                         *   that we can't process for some reason or other.
3440                         *   The first frame without the More-To-Come flag is
3441                         *   the last frame of the skipped message.
3442                         */
3443                         /*  in case of More-To-Come not set in this frame */
3444                         if (mtc_this_frm==0) {
3445                                 privptr->mtc_skipping=0; /* Ok, the end */
3446                                 privptr->mtc_logical_link=-1;
3447                         }
3448 #ifdef DEBUGMSG
3449                         printk(KERN_INFO "%s:%s goto next "
3450                                 "frame from MoretoComeSkip \n",
3451                                 dev->name,__func__);
3452 #endif
3453                         goto NextFrame;
3454                 }
3455
3456                 if (link_num==0) {
3457                         claw_process_control(dev, p_this_ccw);
3458 #ifdef DEBUGMSG
3459                         printk(KERN_INFO "%s:%s goto next "
3460                                 "frame from claw_process_control \n",
3461                                 dev->name,__func__);
3462 #endif
3463                         CLAW_DBF_TEXT(4,trace,"UnpkCntl");
3464                         goto NextFrame;
3465                 }
3466 unpack_next:
3467                 if (p_env->packing == DO_PACKED) {
3468                         if (pack_off > p_env->read_size)
3469                                 goto NextFrame;
3470                         p_packd = p_this_ccw->p_buffer+pack_off;
3471                         p_packh = (struct clawph *) p_packd;
3472                         if ((p_packh->len == 0) || /* all done with this frame? */
3473                             (p_packh->flag != 0))
3474                                 goto NextFrame;
3475                         bytes_to_mov = p_packh->len;
3476                         pack_off += bytes_to_mov+sizeof(struct clawph);
3477                         p++;
3478                 } else {
3479                         bytes_to_mov=p_this_ccw->header.length;
3480                 }
3481                 if (privptr->mtc_logical_link<0) {
3482 #ifdef DEBUGMSG
3483                 printk(KERN_INFO "%s: %s mtc_logical_link < 0  \n",
3484                         dev->name,__func__);
3485 #endif
3486
3487                 /*
3488                 *  if More-To-Come is set in this frame then we don't know
3489                 *  length of entire message, and hence have to allocate
3490                 *  large buffer   */
3491
3492                 /*      We are starting a new envelope  */
3493                 privptr->mtc_offset=0;
3494                         privptr->mtc_logical_link=link_num;
3495                 }
3496
3497                 if (bytes_to_mov > (MAX_ENVELOPE_SIZE- privptr->mtc_offset) ) {
3498                         /*      error     */
3499 #ifdef DEBUGMSG
3500                         printk(KERN_INFO "%s: %s > goto next "
3501                                 "frame from MoretoComeSkip \n",
3502                                 dev->name,
3503                                 __func__);
3504                         printk(KERN_INFO "      bytes_to_mov %d > (MAX_ENVELOPE_"
3505                                 "SIZE-privptr->mtc_offset %d)\n",
3506                                 bytes_to_mov,(MAX_ENVELOPE_SIZE- privptr->mtc_offset));
3507 #endif
3508                         privptr->stats.rx_frame_errors++;
3509                         goto NextFrame;
3510                 }
3511                 if (p_env->packing == DO_PACKED) {
3512                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
3513                                 p_packd+sizeof(struct clawph), bytes_to_mov);
3514
3515                 } else  {
3516                         memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
3517                                 p_this_ccw->p_buffer, bytes_to_mov);
3518                 }
3519 #ifdef DEBUGMSG
3520                 printk(KERN_INFO "%s: %s() received data \n",
3521                         dev->name,__func__);
3522                 if (p_env->packing == DO_PACKED)
3523                         dumpit((char *)p_packd+sizeof(struct clawph),32);
3524                 else
3525                         dumpit((char *)p_this_ccw->p_buffer, 32);
3526                 printk(KERN_INFO "%s: %s() bytelength %d \n",
3527                         dev->name,__func__,bytes_to_mov);
3528 #endif
3529                 if (mtc_this_frm==0) {
3530                         len_of_data=privptr->mtc_offset+bytes_to_mov;
3531                         skb=dev_alloc_skb(len_of_data);
3532                         if (skb) {
3533                                 memcpy(skb_put(skb,len_of_data),
3534                                         privptr->p_mtc_envelope,
3535                                         len_of_data);
3536                                 skb->dev=dev;
3537                                 skb_reset_mac_header(skb);
3538                                 skb->protocol=htons(ETH_P_IP);
3539                                 skb->ip_summed=CHECKSUM_UNNECESSARY;
3540                                 privptr->stats.rx_packets++;
3541                                 privptr->stats.rx_bytes+=len_of_data;
3542                                 netif_rx(skb);
3543 #ifdef DEBUGMSG
3544                                 printk(KERN_INFO "%s: %s() netif_"
3545                                         "rx(skb) completed \n",
3546                                         dev->name,__func__);
3547 #endif
3548                         }
3549                         else {
3550                                 privptr->stats.rx_dropped++;
3551                                 printk(KERN_WARNING "%s: %s() low on memory\n",
3552                                 dev->name,__func__);
3553                         }
3554                         privptr->mtc_offset=0;
3555                         privptr->mtc_logical_link=-1;
3556                 }
3557                 else {
3558                         privptr->mtc_offset+=bytes_to_mov;
3559                 }
3560                 if (p_env->packing == DO_PACKED)
3561                         goto unpack_next;
3562 NextFrame:
3563                 /*
3564                 *   Remove ThisCCWblock from active read queue, and add it
3565                 *   to queue of free blocks to be reused.
3566                 */
3567                 i++;
3568                 p_this_ccw->header.length=0xffff;
3569                 p_this_ccw->header.opcode=0xff;
3570                 /*
3571                 *       add this one to the free queue for later reuse
3572                 */
3573                 if (p_first_ccw==NULL) {
3574                         p_first_ccw = p_this_ccw;
3575                 }
3576                 else {
3577                         p_last_ccw->next = p_this_ccw;
3578                 }
3579                 p_last_ccw = p_this_ccw;
3580                 /*
3581                 *       chain to next block on active read queue
3582                 */
3583                 p_this_ccw = privptr->p_read_active_first;
3584                 CLAW_DBF_TEXT_(4,trace,"rxpkt %d",p);
3585         } /* end of while */
3586
3587         /*      check validity                  */
3588
3589 #ifdef IOTRACE
3590         printk(KERN_INFO "%s:%s processed frame is %d \n",
3591                 dev->name,__func__,i);
3592         printk(KERN_INFO "%s:%s  F:%lx L:%lx\n",
3593                 dev->name,
3594                 __func__,
3595                 (unsigned long)p_first_ccw,
3596                 (unsigned long)p_last_ccw);
3597 #endif
3598         CLAW_DBF_TEXT_(4,trace,"rxfrm %d",i);
3599         add_claw_reads(dev, p_first_ccw, p_last_ccw);
3600         p_ch=&privptr->channel[READ];
3601         claw_strt_read(dev, LOCK_YES);
3602 #ifdef FUNCTRACE
3603         printk(KERN_INFO "%s: %s exit on line %d\n",
3604                 dev->name, __func__, __LINE__);
3605 #endif
3606         return;
3607 }     /*  end of unpack_read   */
3608
3609 /*-------------------------------------------------------------------*
3610 *       claw_strt_read                                               *
3611 *                                                                    *
3612 *--------------------------------------------------------------------*/
3613 static void
3614 claw_strt_read (struct net_device *dev, int lock )
3615 {
3616         int        rc = 0;
3617         __u32      parm;
3618         unsigned long  saveflags = 0;
3619         struct claw_privbk *privptr=dev->priv;
3620         struct ccwbk*p_ccwbk;
3621         struct chbk *p_ch;
3622         struct clawh *p_clawh;
3623         p_ch=&privptr->channel[READ];
3624
3625 #ifdef FUNCTRACE
3626         printk(KERN_INFO "%s:%s Enter  \n",dev->name,__func__);
3627         printk(KERN_INFO "%s: variable lock = %d, dev =\n",dev->name, lock);
3628         dumpit((char *) dev, sizeof(struct net_device));
3629 #endif
3630         CLAW_DBF_TEXT(4,trace,"StRdNter");
3631         p_clawh=(struct clawh *)privptr->p_claw_signal_blk;
3632         p_clawh->flag=CLAW_IDLE;    /* 0x00 */
3633
3634         if ((privptr->p_write_active_first!=NULL &&
3635              privptr->p_write_active_first->header.flag!=CLAW_PENDING) ||
3636             (privptr->p_read_active_first!=NULL &&
3637              privptr->p_read_active_first->header.flag!=CLAW_PENDING )) {
3638                 p_clawh->flag=CLAW_BUSY;    /* 0xff */
3639         }
3640 #ifdef DEBUGMSG
3641         printk(KERN_INFO "%s:%s state-%02x\n" ,
3642                 dev->name,__func__, p_ch->claw_state);
3643 #endif
3644         if (lock==LOCK_YES) {
3645                 spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
3646         }
3647         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
3648 #ifdef DEBUGMSG
3649                 printk(KERN_INFO "%s: HOT READ started in %s\n" ,
3650                         dev->name,__func__);
3651                 p_clawh=(struct clawh *)privptr->p_claw_signal_blk;
3652                 dumpit((char *)&p_clawh->flag , 1);
3653 #endif
3654                 CLAW_DBF_TEXT(4,trace,"HotRead");
3655                 p_ccwbk=privptr->p_read_active_first;
3656                 parm = (unsigned long) p_ch;
3657                 rc = ccw_device_start (p_ch->cdev, &p_ccwbk->read, parm,
3658                                        0xff, 0);
3659                 if (rc != 0) {
3660                         ccw_check_return_code(p_ch->cdev, rc);
3661                 }
3662         }
3663         else {
3664 #ifdef DEBUGMSG
3665                 printk(KERN_INFO "%s: No READ started by %s() In progress\n" ,
3666                         dev->name,__func__);
3667 #endif
3668                 CLAW_DBF_TEXT(2,trace,"ReadAct");
3669         }
3670
3671         if (lock==LOCK_YES) {
3672                 spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
3673         }
3674 #ifdef FUNCTRACE
3675         printk(KERN_INFO "%s:%s Exit on line %d\n",
3676                 dev->name,__func__,__LINE__);
3677 #endif
3678         CLAW_DBF_TEXT(4,trace,"StRdExit");
3679         return;
3680 }       /*    end of claw_strt_read    */
3681
3682 /*-------------------------------------------------------------------*
3683 *       claw_strt_out_IO                                             *
3684 *                                                                    *
3685 *--------------------------------------------------------------------*/
3686
3687 static void
3688 claw_strt_out_IO( struct net_device *dev )
3689 {
3690         int                     rc = 0;
3691         unsigned long           parm;
3692         struct claw_privbk      *privptr;
3693         struct chbk             *p_ch;
3694         struct ccwbk    *p_first_ccw;
3695
3696 #ifdef FUNCTRACE
3697         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3698 #endif
3699         if (!dev) {
3700                 return;
3701         }
3702         privptr=(struct claw_privbk *)dev->priv;
3703         p_ch=&privptr->channel[WRITE];
3704
3705 #ifdef DEBUGMSG
3706         printk(KERN_INFO "%s:%s state-%02x\n" ,
3707                 dev->name,__func__,p_ch->claw_state);
3708 #endif
3709         CLAW_DBF_TEXT(4,trace,"strt_io");
3710         p_first_ccw=privptr->p_write_active_first;
3711
3712         if (p_ch->claw_state == CLAW_STOP)
3713                 return;
3714         if (p_first_ccw == NULL) {
3715 #ifdef FUNCTRACE
3716                 printk(KERN_INFO "%s:%s Exit on line %d\n",
3717                         dev->name,__func__,__LINE__);
3718 #endif
3719                 return;
3720         }
3721         if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
3722                 parm = (unsigned long) p_ch;
3723 #ifdef DEBUGMSG
3724                 printk(KERN_INFO "%s:%s do_io \n" ,dev->name,__func__);
3725                 dumpit((char *)p_first_ccw, sizeof(struct ccwbk));
3726 #endif
3727                 CLAW_DBF_TEXT(2,trace,"StWrtIO");
3728                 rc = ccw_device_start (p_ch->cdev,&p_first_ccw->write, parm,
3729                                        0xff, 0);
3730                 if (rc != 0) {
3731                         ccw_check_return_code(p_ch->cdev, rc);
3732                 }
3733         }
3734         dev->trans_start = jiffies;
3735 #ifdef FUNCTRACE
3736         printk(KERN_INFO "%s:%s Exit on line %d\n",
3737                 dev->name,__func__,__LINE__);
3738 #endif
3739
3740         return;
3741 }       /*    end of claw_strt_out_IO    */
3742
3743 /*-------------------------------------------------------------------*
3744 *       Free write buffers                                           *
3745 *                                                                    *
3746 *--------------------------------------------------------------------*/
3747
3748 static void
3749 claw_free_wrt_buf( struct net_device *dev )
3750 {
3751
3752         struct claw_privbk *privptr=(struct claw_privbk *)dev->priv;
3753         struct ccwbk*p_first_ccw;
3754         struct ccwbk*p_last_ccw;
3755         struct ccwbk*p_this_ccw;
3756         struct ccwbk*p_next_ccw;
3757 #ifdef IOTRACE
3758         struct ccwbk*p_buf;
3759 #endif
3760 #ifdef FUNCTRACE
3761         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3762         printk(KERN_INFO "%s: free count = %d  variable dev =\n",
3763                 dev->name,privptr->write_free_count);
3764 #endif
3765         CLAW_DBF_TEXT(4,trace,"freewrtb");
3766         /*  scan the write queue to free any completed write packets   */
3767         p_first_ccw=NULL;
3768         p_last_ccw=NULL;
3769 #ifdef IOTRACE
3770         printk(KERN_INFO "%s:  Dump current CCW chain \n",dev->name  );
3771         p_buf=privptr->p_write_active_first;
3772         while (p_buf!=NULL) {
3773                 dumpit((char *)p_buf, sizeof(struct ccwbk));
3774                 p_buf=p_buf->next;
3775         }
3776         if (p_buf==NULL) {
3777                 printk(KERN_INFO "%s: privptr->p_write_"
3778                         "active_first==NULL\n",dev->name  );
3779         }
3780         p_buf=(struct ccwbk*)privptr->p_end_ccw;
3781         dumpit((char *)p_buf, sizeof(struct endccw));
3782 #endif
3783         p_this_ccw=privptr->p_write_active_first;
3784         while ( (p_this_ccw!=NULL) && (p_this_ccw->header.flag!=CLAW_PENDING))
3785         {
3786                 p_next_ccw = p_this_ccw->next;
3787                 if (((p_next_ccw!=NULL) &&
3788                      (p_next_ccw->header.flag!=CLAW_PENDING)) ||
3789                     ((p_this_ccw == privptr->p_write_active_last) &&
3790                      (p_this_ccw->header.flag!=CLAW_PENDING))) {
3791                         /* The next CCW is OK or this is  */
3792                         /* the last CCW...free it   @A1A  */
3793                         privptr->p_write_active_first=p_this_ccw->next;
3794                         p_this_ccw->header.flag=CLAW_PENDING;
3795                         p_this_ccw->next=privptr->p_write_free_chain;
3796                         privptr->p_write_free_chain=p_this_ccw;
3797                         ++privptr->write_free_count;
3798                         privptr->stats.tx_bytes+= p_this_ccw->write.count;
3799                         p_this_ccw=privptr->p_write_active_first;
3800                         privptr->stats.tx_packets++;
3801                 }
3802                 else {
3803                         break;
3804                 }
3805         }
3806         if (privptr->write_free_count!=0) {
3807                 claw_clearbit_busy(TB_NOBUFFER,dev);
3808         }
3809         /*   whole chain removed?   */
3810         if (privptr->p_write_active_first==NULL) {
3811                 privptr->p_write_active_last=NULL;
3812 #ifdef DEBUGMSG
3813                 printk(KERN_INFO "%s:%s p_write_"
3814                         "active_first==NULL\n",dev->name,__func__);
3815 #endif
3816         }
3817 #ifdef IOTRACE
3818         printk(KERN_INFO "%s: Dump arranged CCW chain \n",dev->name  );
3819         p_buf=privptr->p_write_active_first;
3820         while (p_buf!=NULL) {
3821                 dumpit((char *)p_buf, sizeof(struct ccwbk));
3822                 p_buf=p_buf->next;
3823         }
3824         if (p_buf==NULL) {
3825                 printk(KERN_INFO "%s: privptr->p_write_active_"
3826                         "first==NULL\n",dev->name  );
3827         }
3828         p_buf=(struct ccwbk*)privptr->p_end_ccw;
3829         dumpit((char *)p_buf, sizeof(struct endccw));
3830 #endif
3831
3832         CLAW_DBF_TEXT_(4,trace,"FWC=%d",privptr->write_free_count);
3833 #ifdef FUNCTRACE
3834         printk(KERN_INFO "%s:%s Exit on line %d free_count =%d\n",
3835                 dev->name,__func__, __LINE__,privptr->write_free_count);
3836 #endif
3837         return;
3838 }
3839
3840 /*-------------------------------------------------------------------*
3841 *       claw free netdevice                                          *
3842 *                                                                    *
3843 *--------------------------------------------------------------------*/
3844 static void
3845 claw_free_netdevice(struct net_device * dev, int free_dev)
3846 {
3847         struct claw_privbk *privptr;
3848 #ifdef FUNCTRACE
3849         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3850 #endif
3851         CLAW_DBF_TEXT(2,setup,"free_dev");
3852
3853         if (!dev)
3854                 return;
3855         CLAW_DBF_TEXT_(2,setup,"%s",dev->name);
3856         privptr = dev->priv;
3857         if (dev->flags & IFF_RUNNING)
3858                 claw_release(dev);
3859         if (privptr) {
3860                 privptr->channel[READ].ndev = NULL;  /* say it's free */
3861         }
3862         dev->priv=NULL;
3863 #ifdef MODULE
3864         if (free_dev) {
3865                 free_netdev(dev);
3866         }
3867 #endif
3868         CLAW_DBF_TEXT(2,setup,"feee_ok");
3869 #ifdef FUNCTRACE
3870         printk(KERN_INFO "%s:%s Exit\n",dev->name,__func__);
3871 #endif
3872 }
3873
3874 /**
3875  * Claw init netdevice
3876  * Initialize everything of the net device except the name and the
3877  * channel structs.
3878  */
3879 static void
3880 claw_init_netdevice(struct net_device * dev)
3881 {
3882 #ifdef FUNCTRACE
3883         printk(KERN_INFO "%s:%s Enter\n",dev->name,__func__);
3884 #endif
3885         CLAW_DBF_TEXT(2,setup,"init_dev");
3886         CLAW_DBF_TEXT_(2,setup,"%s",dev->name);
3887         if (!dev) {
3888         printk(KERN_WARNING "claw:%s BAD Device exit line %d\n",
3889                 __func__,__LINE__);
3890                 CLAW_DBF_TEXT(2,setup,"baddev");
3891                 return;
3892         }
3893         dev->mtu = CLAW_DEFAULT_MTU_SIZE;
3894         dev->hard_start_xmit = claw_tx;
3895         dev->open = claw_open;
3896         dev->stop = claw_release;
3897         dev->get_stats = claw_stats;
3898         dev->change_mtu = claw_change_mtu;
3899         dev->hard_header_len = 0;
3900         dev->addr_len = 0;
3901         dev->type = ARPHRD_SLIP;
3902         dev->tx_queue_len = 1300;
3903         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
3904 #ifdef FUNCTRACE
3905         printk(KERN_INFO "%s:%s Exit\n",dev->name,__func__);
3906 #endif
3907         CLAW_DBF_TEXT(2,setup,"initok");
3908         return;
3909 }
3910
3911 /**
3912  * Init a new channel in the privptr->channel[i].
3913  *
3914  * @param cdev  The ccw_device to be added.
3915  *
3916  * @return 0 on success, !0 on error.
3917  */
3918 static int
3919 add_channel(struct ccw_device *cdev,int i,struct claw_privbk *privptr)
3920 {
3921         struct chbk *p_ch;
3922         struct ccw_dev_id dev_id;
3923
3924 #ifdef FUNCTRACE
3925         printk(KERN_INFO "%s:%s Enter\n",cdev->dev.bus_id,__func__);
3926 #endif
3927         CLAW_DBF_TEXT_(2,setup,"%s",cdev->dev.bus_id);
3928         privptr->channel[i].flag  = i+1;   /* Read is 1 Write is 2 */
3929         p_ch = &privptr->channel[i];
3930         p_ch->cdev = cdev;
3931         snprintf(p_ch->id, CLAW_ID_SIZE, "cl-%s", cdev->dev.bus_id);
3932         ccw_device_get_id(cdev, &dev_id);
3933         p_ch->devno = dev_id.devno;
3934         if ((p_ch->irb = kzalloc(sizeof (struct irb),GFP_KERNEL)) == NULL) {
3935                 printk(KERN_WARNING "%s Out of memory in %s for irb\n",
3936                         p_ch->id,__func__);
3937 #ifdef FUNCTRACE
3938                 printk(KERN_INFO "%s:%s Exit on line %d\n",
3939                         p_ch->id,__func__,__LINE__);
3940 #endif
3941                 return -ENOMEM;
3942         }
3943 #ifdef FUNCTRACE
3944                 printk(KERN_INFO "%s:%s Exit on line %d\n",
3945                         cdev->dev.bus_id,__func__,__LINE__);
3946 #endif
3947         return 0;
3948 }
3949
3950
3951 /**
3952  *
3953  * Setup an interface.
3954  *
3955  * @param cgdev  Device to be setup.
3956  *
3957  * @returns 0 on success, !0 on failure.
3958  */
3959 static int
3960 claw_new_device(struct ccwgroup_device *cgdev)
3961 {
3962         struct claw_privbk *privptr;
3963         struct claw_env *p_env;
3964         struct net_device *dev;
3965         int ret;
3966         struct ccw_dev_id dev_id;
3967
3968         pr_debug("%s() called\n", __func__);
3969         printk(KERN_INFO "claw: add for %s\n",cgdev->cdev[READ]->dev.bus_id);
3970         CLAW_DBF_TEXT(2,setup,"new_dev");
3971         privptr = cgdev->dev.driver_data;
3972         cgdev->cdev[READ]->dev.driver_data = privptr;
3973         cgdev->cdev[WRITE]->dev.driver_data = privptr;
3974         if (!privptr)
3975                 return -ENODEV;
3976         p_env = privptr->p_env;
3977         ccw_device_get_id(cgdev->cdev[READ], &dev_id);
3978         p_env->devno[READ] = dev_id.devno;
3979         ccw_device_get_id(cgdev->cdev[WRITE], &dev_id);
3980         p_env->devno[WRITE] = dev_id.devno;
3981         ret = add_channel(cgdev->cdev[0],0,privptr);
3982         if (ret == 0)
3983                 ret = add_channel(cgdev->cdev[1],1,privptr);
3984         if (ret != 0) {
3985                         printk(KERN_WARNING
3986                         "add channel failed "
3987                                 "with ret = %d\n", ret);
3988                         goto out;
3989         }
3990         ret = ccw_device_set_online(cgdev->cdev[READ]);
3991         if (ret != 0) {
3992                 printk(KERN_WARNING
3993                  "claw: ccw_device_set_online %s READ failed "
3994                         "with ret = %d\n",cgdev->cdev[READ]->dev.bus_id,ret);
3995                 goto out;
3996         }
3997         ret = ccw_device_set_online(cgdev->cdev[WRITE]);
3998         if (ret != 0) {
3999                 printk(KERN_WARNING
4000                  "claw: ccw_device_set_online %s WRITE failed "
4001                         "with ret = %d\n",cgdev->cdev[WRITE]->dev.bus_id, ret);
4002                 goto out;
4003         }
4004         dev = alloc_netdev(0,"claw%d",claw_init_netdevice);
4005         if (!dev) {
4006                 printk(KERN_WARNING "%s:alloc_netdev failed\n",__func__);
4007                 goto out;
4008         }
4009         dev->priv = privptr;
4010         cgdev->dev.driver_data = privptr;
4011         cgdev->cdev[READ]->dev.driver_data = privptr;
4012         cgdev->cdev[WRITE]->dev.driver_data = privptr;
4013         /* sysfs magic */
4014         SET_NETDEV_DEV(dev, &cgdev->dev);
4015         if (register_netdev(dev) != 0) {
4016                 claw_free_netdevice(dev, 1);
4017                 CLAW_DBF_TEXT(2,trace,"regfail");
4018                 goto out;
4019         }
4020         dev->flags &=~IFF_RUNNING;
4021         if (privptr->buffs_alloc == 0) {
4022                 ret=init_ccw_bk(dev);
4023                 if (ret !=0) {
4024                         printk(KERN_WARNING
4025                          "claw: init_ccw_bk failed with ret=%d\n", ret);
4026                         unregister_netdev(dev);
4027                         claw_free_netdevice(dev,1);
4028                         CLAW_DBF_TEXT(2,trace,"ccwmem");
4029                         goto out;
4030                 }
4031         }
4032         privptr->channel[READ].ndev = dev;
4033         privptr->channel[WRITE].ndev = dev;
4034         privptr->p_env->ndev = dev;
4035
4036         printk(KERN_INFO "%s:readsize=%d  writesize=%d "
4037                 "readbuffer=%d writebuffer=%d read=0x%04x write=0x%04x\n",
4038                 dev->name, p_env->read_size,
4039                 p_env->write_size, p_env->read_buffers,
4040                 p_env->write_buffers, p_env->devno[READ],
4041                 p_env->devno[WRITE]);
4042         printk(KERN_INFO "%s:host_name:%.8s, adapter_name "
4043                 ":%.8s api_type: %.8s\n",
4044                 dev->name, p_env->host_name,
4045                 p_env->adapter_name , p_env->api_type);
4046         return 0;
4047 out:
4048         ccw_device_set_offline(cgdev->cdev[1]);
4049         ccw_device_set_offline(cgdev->cdev[0]);
4050
4051         return -ENODEV;
4052 }
4053
4054 static void
4055 claw_purge_skb_queue(struct sk_buff_head *q)
4056 {
4057         struct sk_buff *skb;
4058
4059         CLAW_DBF_TEXT(4,trace,"purgque");
4060
4061         while ((skb = skb_dequeue(q))) {
4062                 atomic_dec(&skb->users);
4063                 dev_kfree_skb_any(skb);
4064         }
4065 }
4066
4067 /**
4068  * Shutdown an interface.
4069  *
4070  * @param cgdev  Device to be shut down.
4071  *
4072  * @returns 0 on success, !0 on failure.
4073  */
4074 static int
4075 claw_shutdown_device(struct ccwgroup_device *cgdev)
4076 {
4077         struct claw_privbk *priv;
4078         struct net_device *ndev;
4079         int     ret;
4080
4081         pr_debug("%s() called\n", __func__);
4082         CLAW_DBF_TEXT_(2,setup,"%s",cgdev->dev.bus_id);
4083         priv = cgdev->dev.driver_data;
4084         if (!priv)
4085                 return -ENODEV;
4086         ndev = priv->channel[READ].ndev;
4087         if (ndev) {
4088                 /* Close the device */
4089                 printk(KERN_INFO
4090                         "%s: shuting down \n",ndev->name);
4091                 if (ndev->flags & IFF_RUNNING)
4092                         ret = claw_release(ndev);
4093                 ndev->flags &=~IFF_RUNNING;
4094                 unregister_netdev(ndev);
4095                 ndev->priv = NULL;  /* cgdev data, not ndev's to free */
4096                 claw_free_netdevice(ndev, 1);
4097                 priv->channel[READ].ndev = NULL;
4098                 priv->channel[WRITE].ndev = NULL;
4099                 priv->p_env->ndev = NULL;
4100         }
4101         ccw_device_set_offline(cgdev->cdev[1]);
4102         ccw_device_set_offline(cgdev->cdev[0]);
4103         return 0;
4104 }
4105
4106 static void
4107 claw_remove_device(struct ccwgroup_device *cgdev)
4108 {
4109         struct claw_privbk *priv;
4110
4111         pr_debug("%s() called\n", __func__);
4112         CLAW_DBF_TEXT_(2,setup,"%s",cgdev->dev.bus_id);
4113         priv = cgdev->dev.driver_data;
4114         if (!priv) {
4115                 printk(KERN_WARNING "claw: %s() no Priv exiting\n",__func__);
4116                 return;
4117         }
4118         printk(KERN_INFO "claw: %s() called %s will be removed.\n",
4119                         __func__,cgdev->cdev[0]->dev.bus_id);
4120         if (cgdev->state == CCWGROUP_ONLINE)
4121                 claw_shutdown_device(cgdev);
4122         claw_remove_files(&cgdev->dev);
4123         kfree(priv->p_mtc_envelope);
4124         priv->p_mtc_envelope=NULL;
4125         kfree(priv->p_env);
4126         priv->p_env=NULL;
4127         kfree(priv->channel[0].irb);
4128         priv->channel[0].irb=NULL;
4129         kfree(priv->channel[1].irb);
4130         priv->channel[1].irb=NULL;
4131         kfree(priv);
4132         cgdev->dev.driver_data=NULL;
4133         cgdev->cdev[READ]->dev.driver_data = NULL;
4134         cgdev->cdev[WRITE]->dev.driver_data = NULL;
4135         put_device(&cgdev->dev);
4136 }
4137
4138
4139 /*
4140  * sysfs attributes
4141  */
4142 static ssize_t
4143 claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf)
4144 {
4145         struct claw_privbk *priv;
4146         struct claw_env *  p_env;
4147
4148         priv = dev->driver_data;
4149         if (!priv)
4150                 return -ENODEV;
4151         p_env = priv->p_env;
4152         return sprintf(buf, "%s\n",p_env->host_name);
4153 }
4154
4155 static ssize_t
4156 claw_hname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4157 {
4158         struct claw_privbk *priv;
4159         struct claw_env *  p_env;
4160
4161         priv = dev->driver_data;
4162         if (!priv)
4163                 return -ENODEV;
4164         p_env = priv->p_env;
4165         if (count > MAX_NAME_LEN+1)
4166                 return -EINVAL;
4167         memset(p_env->host_name, 0x20, MAX_NAME_LEN);
4168         strncpy(p_env->host_name,buf, count);
4169         p_env->host_name[count-1] = 0x20;  /* clear extra 0x0a */
4170         p_env->host_name[MAX_NAME_LEN] = 0x00;
4171         CLAW_DBF_TEXT(2,setup,"HstnSet");
4172         CLAW_DBF_TEXT_(2,setup,"%s",p_env->host_name);
4173
4174         return count;
4175 }
4176
4177 static DEVICE_ATTR(host_name, 0644, claw_hname_show, claw_hname_write);
4178
4179 static ssize_t
4180 claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf)
4181 {
4182         struct claw_privbk *priv;
4183         struct claw_env *  p_env;
4184
4185         priv = dev->driver_data;
4186         if (!priv)
4187                 return -ENODEV;
4188         p_env = priv->p_env;
4189         return sprintf(buf, "%s\n",p_env->adapter_name);
4190 }
4191
4192 static ssize_t
4193 claw_adname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4194 {
4195         struct claw_privbk *priv;
4196         struct claw_env *  p_env;
4197
4198         priv = dev->driver_data;
4199         if (!priv)
4200                 return -ENODEV;
4201         p_env = priv->p_env;
4202         if (count > MAX_NAME_LEN+1)
4203                 return -EINVAL;
4204         memset(p_env->adapter_name, 0x20, MAX_NAME_LEN);
4205         strncpy(p_env->adapter_name,buf, count);
4206         p_env->adapter_name[count-1] = 0x20; /* clear extra 0x0a */
4207         p_env->adapter_name[MAX_NAME_LEN] = 0x00;
4208         CLAW_DBF_TEXT(2,setup,"AdnSet");
4209         CLAW_DBF_TEXT_(2,setup,"%s",p_env->adapter_name);
4210
4211         return count;
4212 }
4213
4214 static DEVICE_ATTR(adapter_name, 0644, claw_adname_show, claw_adname_write);
4215
4216 static ssize_t
4217 claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf)
4218 {
4219         struct claw_privbk *priv;
4220         struct claw_env *  p_env;
4221
4222         priv = dev->driver_data;
4223         if (!priv)
4224                 return -ENODEV;
4225         p_env = priv->p_env;
4226         return sprintf(buf, "%s\n",
4227                        p_env->api_type);
4228 }
4229
4230 static ssize_t
4231 claw_apname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4232 {
4233         struct claw_privbk *priv;
4234         struct claw_env *  p_env;
4235
4236         priv = dev->driver_data;
4237         if (!priv)
4238                 return -ENODEV;
4239         p_env = priv->p_env;
4240         if (count > MAX_NAME_LEN+1)
4241                 return -EINVAL;
4242         memset(p_env->api_type, 0x20, MAX_NAME_LEN);
4243         strncpy(p_env->api_type,buf, count);
4244         p_env->api_type[count-1] = 0x20;  /* we get a loose 0x0a */
4245         p_env->api_type[MAX_NAME_LEN] = 0x00;
4246         if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
4247                 p_env->read_size=DEF_PACK_BUFSIZE;
4248                 p_env->write_size=DEF_PACK_BUFSIZE;
4249                 p_env->packing=PACKING_ASK;
4250                 CLAW_DBF_TEXT(2,setup,"PACKING");
4251         }
4252         else {
4253                 p_env->packing=0;
4254                 p_env->read_size=CLAW_FRAME_SIZE;
4255                 p_env->write_size=CLAW_FRAME_SIZE;
4256                 CLAW_DBF_TEXT(2,setup,"ApiSet");
4257         }
4258         CLAW_DBF_TEXT_(2,setup,"%s",p_env->api_type);
4259         return count;
4260 }
4261
4262 static DEVICE_ATTR(api_type, 0644, claw_apname_show, claw_apname_write);
4263
4264 static ssize_t
4265 claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
4266 {
4267         struct claw_privbk *priv;
4268         struct claw_env * p_env;
4269
4270         priv = dev->driver_data;
4271         if (!priv)
4272                 return -ENODEV;
4273         p_env = priv->p_env;
4274         return sprintf(buf, "%d\n", p_env->write_buffers);
4275 }
4276
4277 static ssize_t
4278 claw_wbuff_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4279 {
4280         struct claw_privbk *priv;
4281         struct claw_env *  p_env;
4282         int nnn,max;
4283
4284         priv = dev->driver_data;
4285         if (!priv)
4286                 return -ENODEV;
4287         p_env = priv->p_env;
4288         sscanf(buf, "%i", &nnn);
4289         if (p_env->packing) {
4290                 max = 64;
4291         }
4292         else {
4293                 max = 512;
4294         }
4295         if ((nnn > max ) || (nnn < 2))
4296                 return -EINVAL;
4297         p_env->write_buffers = nnn;
4298         CLAW_DBF_TEXT(2,setup,"Wbufset");
4299         CLAW_DBF_TEXT_(2,setup,"WB=%d",p_env->write_buffers);
4300         return count;
4301 }
4302
4303 static DEVICE_ATTR(write_buffer, 0644, claw_wbuff_show, claw_wbuff_write);
4304
4305 static ssize_t
4306 claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
4307 {
4308         struct claw_privbk *priv;
4309         struct claw_env *  p_env;
4310
4311         priv = dev->driver_data;
4312         if (!priv)
4313                 return -ENODEV;
4314         p_env = priv->p_env;
4315         return sprintf(buf, "%d\n", p_env->read_buffers);
4316 }
4317
4318 static ssize_t
4319 claw_rbuff_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4320 {
4321         struct claw_privbk *priv;
4322         struct claw_env *p_env;
4323         int nnn,max;
4324
4325         priv = dev->driver_data;
4326         if (!priv)
4327                 return -ENODEV;
4328         p_env = priv->p_env;
4329         sscanf(buf, "%i", &nnn);
4330         if (p_env->packing) {
4331                 max = 64;
4332         }
4333         else {
4334                 max = 512;
4335         }
4336         if ((nnn > max ) || (nnn < 2))
4337                 return -EINVAL;
4338         p_env->read_buffers = nnn;
4339         CLAW_DBF_TEXT(2,setup,"Rbufset");
4340         CLAW_DBF_TEXT_(2,setup,"RB=%d",p_env->read_buffers);
4341         return count;
4342 }
4343
4344 static DEVICE_ATTR(read_buffer, 0644, claw_rbuff_show, claw_rbuff_write);
4345
4346 static struct attribute *claw_attr[] = {
4347         &dev_attr_read_buffer.attr,
4348         &dev_attr_write_buffer.attr,
4349         &dev_attr_adapter_name.attr,
4350         &dev_attr_api_type.attr,
4351         &dev_attr_host_name.attr,
4352         NULL,
4353 };
4354
4355 static struct attribute_group claw_attr_group = {
4356         .attrs = claw_attr,
4357 };
4358
4359 static int
4360 claw_add_files(struct device *dev)
4361 {
4362         pr_debug("%s() called\n", __func__);
4363         CLAW_DBF_TEXT(2,setup,"add_file");
4364         return sysfs_create_group(&dev->kobj, &claw_attr_group);
4365 }
4366
4367 static void
4368 claw_remove_files(struct device *dev)
4369 {
4370         pr_debug("%s() called\n", __func__);
4371         CLAW_DBF_TEXT(2,setup,"rem_file");
4372         sysfs_remove_group(&dev->kobj, &claw_attr_group);
4373 }
4374
4375 /*--------------------------------------------------------------------*
4376 *    claw_init  and cleanup                                           *
4377 *---------------------------------------------------------------------*/
4378
4379 static void __exit
4380 claw_cleanup(void)
4381 {
4382         unregister_cu3088_discipline(&claw_group_driver);
4383         claw_unregister_debug_facility();
4384         printk(KERN_INFO "claw: Driver unloaded\n");
4385
4386 }
4387
4388 /**
4389  * Initialize module.
4390  * This is called just after the module is loaded.
4391  *
4392  * @return 0 on success, !0 on error.
4393  */
4394 static int __init
4395 claw_init(void)
4396 {
4397         int ret = 0;
4398         printk(KERN_INFO "claw: starting driver\n");
4399
4400 #ifdef FUNCTRACE
4401         printk(KERN_INFO "claw: %s() enter \n",__func__);
4402 #endif
4403         ret = claw_register_debug_facility();
4404         if (ret) {
4405                 printk(KERN_WARNING "claw: %s() debug_register failed %d\n",
4406                         __func__,ret);
4407                 return ret;
4408         }
4409         CLAW_DBF_TEXT(2,setup,"init_mod");
4410         ret = register_cu3088_discipline(&claw_group_driver);
4411         if (ret) {
4412                 claw_unregister_debug_facility();
4413                 printk(KERN_WARNING "claw; %s() cu3088 register failed %d\n",
4414                         __func__,ret);
4415         }
4416 #ifdef FUNCTRACE
4417         printk(KERN_INFO "claw: %s() exit \n",__func__);
4418 #endif
4419         return ret;
4420 }
4421
4422 module_init(claw_init);
4423 module_exit(claw_cleanup);
4424
4425
4426
4427 /*--------------------------------------------------------------------*
4428 *    End of File                                                      *
4429 *---------------------------------------------------------------------*/
4430
4431