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