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