claw.c 115.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *  drivers/s390/net/claw.c
 *    ESCON CLAW network driver
 *
F
Frank Pavlic 已提交
5
 *  Linux for zSeries version
6
 *    Copyright IBM Corp. 2002, 2009
L
Linus Torvalds 已提交
7
 *  Author(s) Original code written by:
8
 *		Kazuo Iimura <iimura@jp.ibm.com>
L
Linus Torvalds 已提交
9
 *   	      Rewritten by
10 11
 *		Andy Richter <richtera@us.ibm.com>
 *		Marc Price <mwprice@us.ibm.com>
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 *    sysfs parms:
 *   group x.x.rrrr,x.x.wwww
 *   read_buffer nnnnnnn
 *   write_buffer nnnnnn
 *   host_name  aaaaaaaa
 *   adapter_name aaaaaaaa
 *   api_type    aaaaaaaa
 *
 *  eg.
 *   group  0.0.0200 0.0.0201
 *   read_buffer 25
 *   write_buffer 20
 *   host_name LINUX390
 *   adapter_name RS6K
 *   api_type     TCPIP
 *
 *  where
 *
 *   The device id is decided by the order entries
 *   are added to the group the first is claw0 the second claw1
 *   up to CLAW_MAX_DEV
 *
 *   rrrr     -	the first of 2 consecutive device addresses used for the
 *		CLAW protocol.
 *		The specified address is always used as the input (Read)
 *		channel and the next address is used as the output channel.
 *
 *   wwww     -	the second of 2 consecutive device addresses used for
 *		the CLAW protocol.
 *              The specified address is always used as the output
 *		channel and the previous address is used as the input channel.
 *
 *   read_buffer	-       specifies number of input buffers to allocate.
 *   write_buffer       -       specifies number of output buffers to allocate.
 *   host_name          -       host name
 *   adaptor_name       -       adaptor name
 *   api_type           -       API type TCPIP or API will be sent and expected
 *				as ws_name
 *
 *   Note the following requirements:
 *   1)  host_name must match the configured adapter_name on the remote side
 *   2)  adaptor_name must match the configured host name on the remote side
 *
 *  Change History
 *    1.00  Initial release shipped
 *    1.10  Changes for Buffer allocation
 *    1.15  Changed for 2.6 Kernel  No longer compiles on 2.4 or lower
 *    1.25  Added Packing support
61
 *    1.5
L
Linus Torvalds 已提交
62
 */
63 64 65

#define KMSG_COMPONENT "claw"

66
#include <linux/kernel_stat.h>
L
Linus Torvalds 已提交
67 68 69 70 71
#include <asm/ccwdev.h>
#include <asm/ccwgroup.h>
#include <asm/debug.h>
#include <asm/idals.h>
#include <asm/io.h>
J
Jiri Slaby 已提交
72
#include <linux/bitops.h>
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/tcp.h>
#include <linux/timer.h>
#include <linux/types.h>

#include "claw.h"

96 97
/*
   CLAW uses the s390dbf file system  see claw_trace and claw_setup
L
Linus Torvalds 已提交
98 99
*/

100
static char version[] __initdata = "CLAW driver";
101
static char debug_buffer[255];
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/**
 * Debug Facility Stuff
 */
static debug_info_t *claw_dbf_setup;
static debug_info_t *claw_dbf_trace;

/**
 *  CLAW Debug Facility functions
 */
static void
claw_unregister_debug_facility(void)
{
	if (claw_dbf_setup)
		debug_unregister(claw_dbf_setup);
	if (claw_dbf_trace)
		debug_unregister(claw_dbf_trace);
}

static int
claw_register_debug_facility(void)
{
123 124
	claw_dbf_setup = debug_register("claw_setup", 2, 1, 8);
	claw_dbf_trace = debug_register("claw_trace", 2, 2, 8);
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
	if (claw_dbf_setup == NULL || claw_dbf_trace == NULL) {
		claw_unregister_debug_facility();
		return -ENOMEM;
	}
	debug_register_view(claw_dbf_setup, &debug_hex_ascii_view);
	debug_set_level(claw_dbf_setup, 2);
	debug_register_view(claw_dbf_trace, &debug_hex_ascii_view);
	debug_set_level(claw_dbf_trace, 2);
	return 0;
}

static inline void
claw_set_busy(struct net_device *dev)
{
139
 ((struct claw_privbk *)dev->ml_priv)->tbusy = 1;
L
Linus Torvalds 已提交
140 141 142 143 144 145
 eieio();
}

static inline void
claw_clear_busy(struct net_device *dev)
{
146
	clear_bit(0, &(((struct claw_privbk *) dev->ml_priv)->tbusy));
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154
	netif_wake_queue(dev);
	eieio();
}

static inline int
claw_check_busy(struct net_device *dev)
{
	eieio();
155
	return ((struct claw_privbk *) dev->ml_priv)->tbusy;
L
Linus Torvalds 已提交
156 157 158 159 160 161
}

static inline void
claw_setbit_busy(int nr,struct net_device *dev)
{
	netif_stop_queue(dev);
162
	set_bit(nr, (void *)&(((struct claw_privbk *)dev->ml_priv)->tbusy));
L
Linus Torvalds 已提交
163 164 165 166 167
}

static inline void
claw_clearbit_busy(int nr,struct net_device *dev)
{
168
	clear_bit(nr, (void *)&(((struct claw_privbk *)dev->ml_priv)->tbusy));
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176
	netif_wake_queue(dev);
}

static inline int
claw_test_and_setbit_busy(int nr,struct net_device *dev)
{
	netif_stop_queue(dev);
	return test_and_set_bit(nr,
177
		(void *)&(((struct claw_privbk *) dev->ml_priv)->tbusy));
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}


/* Functions for the DEV methods */

static int claw_probe(struct ccwgroup_device *cgdev);
static void claw_remove_device(struct ccwgroup_device *cgdev);
static void claw_purge_skb_queue(struct sk_buff_head *q);
static int claw_new_device(struct ccwgroup_device *cgdev);
static int claw_shutdown_device(struct ccwgroup_device *cgdev);
static int claw_tx(struct sk_buff *skb, struct net_device *dev);
static int claw_change_mtu( struct net_device *dev, int new_mtu);
static int claw_open(struct net_device *dev);
static void claw_irq_handler(struct ccw_device *cdev,
	unsigned long intparm, struct irb *irb);
static void claw_irq_tasklet ( unsigned long data );
static int claw_release(struct net_device *dev);
static void claw_write_retry ( struct chbk * p_ch );
static void claw_write_next ( struct chbk * p_ch );
static void claw_timer ( struct chbk * p_ch );

/* Functions */
static int add_claw_reads(struct net_device *dev,
	struct ccwbk* p_first, struct ccwbk* p_last);
202 203
static void ccw_check_return_code (struct ccw_device *cdev, int return_code);
static void ccw_check_unit_check (struct chbk * p_ch, unsigned char sense );
L
Linus Torvalds 已提交
204 205 206 207 208
static int find_link(struct net_device *dev, char *host_name, char *ws_name );
static int claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid);
static int init_ccw_bk(struct net_device *dev);
static void probe_error( struct ccwgroup_device *cgdev);
static struct net_device_stats *claw_stats(struct net_device *dev);
209
static int pages_to_order_of_mag(int num_of_pages);
L
Linus Torvalds 已提交
210 211
static struct sk_buff *claw_pack_skb(struct claw_privbk *privptr);
/* sysfs Functions */
212 213 214 215
static ssize_t claw_hname_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t claw_hname_write(struct device *dev,
	struct device_attribute *attr,
L
Linus Torvalds 已提交
216
	const char *buf, size_t count);
217 218 219 220
static ssize_t claw_adname_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t claw_adname_write(struct device *dev,
	struct device_attribute *attr,
L
Linus Torvalds 已提交
221
	const char *buf, size_t count);
222 223 224 225
static ssize_t claw_apname_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t claw_apname_write(struct device *dev,
	struct device_attribute *attr,
L
Linus Torvalds 已提交
226
	const char *buf, size_t count);
227 228 229 230
static ssize_t claw_wbuff_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t claw_wbuff_write(struct device *dev,
	struct device_attribute *attr,
L
Linus Torvalds 已提交
231
	const char *buf, size_t count);
232 233 234 235
static ssize_t claw_rbuff_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t claw_rbuff_write(struct device *dev,
	struct device_attribute *attr,
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248
	const char *buf, size_t count);
static int claw_add_files(struct device *dev);
static void claw_remove_files(struct device *dev);

/*   Functions for System Validate  */
static int claw_process_control( struct net_device *dev, struct ccwbk * p_ccw);
static int claw_send_control(struct net_device *dev, __u8 type, __u8 link,
       __u8 correlator, __u8 rc , char *local_name, char *remote_name);
static int claw_snd_conn_req(struct net_device *dev, __u8 link);
static int claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl);
static int claw_snd_sys_validate_rsp(struct net_device *dev,
        struct clawctl * p_ctl, __u32 return_code);
static int claw_strt_conn_req(struct net_device *dev );
249 250 251
static void claw_strt_read(struct net_device *dev, int lock);
static void claw_strt_out_IO(struct net_device *dev);
static void claw_free_wrt_buf(struct net_device *dev);
L
Linus Torvalds 已提交
252 253

/* Functions for unpack reads   */
254
static void unpack_read(struct net_device *dev);
L
Linus Torvalds 已提交
255

256 257 258 259 260
static int claw_pm_prepare(struct ccwgroup_device *gdev)
{
	return -EPERM;
}

261 262 263
/* the root device for claw group devices */
static struct device *claw_root_dev;

L
Linus Torvalds 已提交
264 265 266
/* ccwgroup table  */

static struct ccwgroup_driver claw_group_driver = {
267 268 269 270
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "claw",
	},
L
Linus Torvalds 已提交
271 272 273 274 275 276
        .max_slaves  = 2,
        .driver_id   = 0xC3D3C1E6,
        .probe       = claw_probe,
        .remove      = claw_remove_device,
        .set_online  = claw_new_device,
        .set_offline = claw_shutdown_device,
277
	.prepare     = claw_pm_prepare,
L
Linus Torvalds 已提交
278 279
};

280 281 282 283 284 285 286
static struct ccw_device_id claw_ids[] = {
	{CCW_DEVICE(0x3088, 0x61), .driver_info = claw_channel_type_claw},
	{},
};
MODULE_DEVICE_TABLE(ccw, claw_ids);

static struct ccw_driver claw_ccw_driver = {
287 288 289 290
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "claw",
	},
291 292 293 294 295 296 297 298 299 300 301 302
	.ids	= claw_ids,
	.probe	= ccwgroup_probe_ccwdev,
	.remove	= ccwgroup_remove_ccwdev,
};

static ssize_t
claw_driver_group_store(struct device_driver *ddrv, const char *buf,
			size_t count)
{
	int err;
	err = ccwgroup_create_from_string(claw_root_dev,
					  claw_group_driver.driver_id,
303
					  &claw_ccw_driver, 2, buf);
304 305 306 307 308 309 310 311 312 313 314 315 316 317
	return err ? err : count;
}

static DRIVER_ATTR(group, 0200, NULL, claw_driver_group_store);

static struct attribute *claw_group_attrs[] = {
	&driver_attr_group.attr,
	NULL,
};

static struct attribute_group claw_group_attr_group = {
	.attrs = claw_group_attrs,
};

318
static const struct attribute_group *claw_group_attr_groups[] = {
319 320 321 322
	&claw_group_attr_group,
	NULL,
};

L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
*       Key functions
*/

/*----------------------------------------------------------------*
 *   claw_probe                                                   *
 *      this function is called for each CLAW device.             *
 *----------------------------------------------------------------*/
static int
claw_probe(struct ccwgroup_device *cgdev)
{
	int  		rc;
	struct claw_privbk *privptr=NULL;

337
	CLAW_DBF_TEXT(2, setup, "probe");
L
Linus Torvalds 已提交
338 339
	if (!get_device(&cgdev->dev))
		return -ENODEV;
340
	privptr = kzalloc(sizeof(struct claw_privbk), GFP_KERNEL);
341
	dev_set_drvdata(&cgdev->dev, privptr);
L
Linus Torvalds 已提交
342 343 344
	if (privptr == NULL) {
		probe_error(cgdev);
		put_device(&cgdev->dev);
345
		CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
L
Linus Torvalds 已提交
346 347
		return -ENOMEM;
	}
348 349
	privptr->p_mtc_envelope= kzalloc( MAX_ENVELOPE_SIZE, GFP_KERNEL);
	privptr->p_env = kzalloc(sizeof(struct claw_env), GFP_KERNEL);
L
Linus Torvalds 已提交
350 351 352
        if ((privptr->p_mtc_envelope==NULL) || (privptr->p_env==NULL)) {
                probe_error(cgdev);
		put_device(&cgdev->dev);
353
		CLAW_DBF_TEXT_(2, setup, "probex%d", -ENOMEM);
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
                return -ENOMEM;
        }
	memcpy(privptr->p_env->adapter_name,WS_NAME_NOT_DEF,8);
	memcpy(privptr->p_env->host_name,WS_NAME_NOT_DEF,8);
	memcpy(privptr->p_env->api_type,WS_NAME_NOT_DEF,8);
	privptr->p_env->packing = 0;
	privptr->p_env->write_buffers = 5;
	privptr->p_env->read_buffers = 5;
	privptr->p_env->read_size = CLAW_FRAME_SIZE;
	privptr->p_env->write_size = CLAW_FRAME_SIZE;
	rc = claw_add_files(&cgdev->dev);
	if (rc) {
		probe_error(cgdev);
		put_device(&cgdev->dev);
368 369
		dev_err(&cgdev->dev, "Creating the /proc files for a new"
		" CLAW device failed\n");
370
		CLAW_DBF_TEXT_(2, setup, "probex%d", rc);
L
Linus Torvalds 已提交
371 372 373 374 375
		return rc;
	}
	privptr->p_env->p_priv = privptr;
        cgdev->cdev[0]->handler = claw_irq_handler;
	cgdev->cdev[1]->handler = claw_irq_handler;
376
	CLAW_DBF_TEXT(2, setup, "prbext 0");
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385 386 387 388

        return 0;
}  /*  end of claw_probe       */

/*-------------------------------------------------------------------*
 *   claw_tx                                                         *
 *-------------------------------------------------------------------*/

static int
claw_tx(struct sk_buff *skb, struct net_device *dev)
{
        int             rc;
389
	struct claw_privbk *privptr = dev->ml_priv;
L
Linus Torvalds 已提交
390 391 392
	unsigned long saveflags;
        struct chbk *p_ch;

393
	CLAW_DBF_TEXT(4, trace, "claw_tx");
394
	p_ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
395 396 397
        spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
        rc=claw_hw_tx( skb, dev, 1 );
        spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
398
	CLAW_DBF_TEXT_(4, trace, "clawtx%d", rc);
399 400
	if (rc)
		rc = NETDEV_TX_BUSY;
401 402
	else
		rc = NETDEV_TX_OK;
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410 411 412 413 414
        return rc;
}   /*  end of claw_tx */

/*------------------------------------------------------------------*
 *  pack the collect queue into an skb and return it                *
 *   If not packing just return the top skb from the queue          *
 *------------------------------------------------------------------*/

static struct sk_buff *
claw_pack_skb(struct claw_privbk *privptr)
{
	struct sk_buff *new_skb,*held_skb;
415
	struct chbk *p_ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
416 417 418 419 420
	struct claw_env  *p_env = privptr->p_env;
	int	pkt_cnt,pk_ind,so_far;

	new_skb = NULL;		/* assume no dice */
	pkt_cnt = 0;
421
	CLAW_DBF_TEXT(4, trace, "PackSKBe");
422
	if (!skb_queue_empty(&p_ch->collect_queue)) {
L
Linus Torvalds 已提交
423 424 425
	/* some data */
		held_skb = skb_dequeue(&p_ch->collect_queue);
		if (held_skb)
F
Frank Pavlic 已提交
426
			dev_kfree_skb_any(held_skb);
L
Linus Torvalds 已提交
427 428
		else
			return NULL;
F
Frank Pavlic 已提交
429 430
		if (p_env->packing != DO_PACKED)
			return held_skb;
L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
		/* get a new SKB we will pack at least one */
		new_skb = dev_alloc_skb(p_env->write_size);
		if (new_skb == NULL) {
			atomic_inc(&held_skb->users);
			skb_queue_head(&p_ch->collect_queue,held_skb);
			return NULL;
		}
		/* we have packed packet and a place to put it  */
		pk_ind = 1;
		so_far = 0;
		new_skb->cb[1] = 'P'; /* every skb on queue has pack header */
		while ((pk_ind) && (held_skb != NULL)) {
			if (held_skb->len+so_far <= p_env->write_size-8) {
				memcpy(skb_put(new_skb,held_skb->len),
					held_skb->data,held_skb->len);
				privptr->stats.tx_packets++;
				so_far += held_skb->len;
				pkt_cnt++;
F
Frank Pavlic 已提交
449
				dev_kfree_skb_any(held_skb);
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459
				held_skb = skb_dequeue(&p_ch->collect_queue);
				if (held_skb)
					atomic_dec(&held_skb->users);
			} else {
				pk_ind = 0;
				atomic_inc(&held_skb->users);
				skb_queue_head(&p_ch->collect_queue,held_skb);
			}
		}
	}
460
	CLAW_DBF_TEXT(4, trace, "PackSKBx");
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469 470 471
	return new_skb;
}

/*-------------------------------------------------------------------*
 *   claw_change_mtu                                                 *
 *                                                                   *
 *-------------------------------------------------------------------*/

static int
claw_change_mtu(struct net_device *dev, int new_mtu)
{
472
	struct claw_privbk *privptr = dev->ml_priv;
L
Linus Torvalds 已提交
473
	int buff_size;
474
	CLAW_DBF_TEXT(4, trace, "setmtu");
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	buff_size = privptr->p_env->write_size;
        if ((new_mtu < 60) || (new_mtu > buff_size)) {
                return -EINVAL;
        }
        dev->mtu = new_mtu;
        return 0;
}  /*   end of claw_change_mtu */


/*-------------------------------------------------------------------*
 *   claw_open                                                       *
 *                                                                   *
 *-------------------------------------------------------------------*/
static int
claw_open(struct net_device *dev)
{

        int     rc;
        int     i;
        unsigned long       saveflags=0;
        unsigned long       parm;
        struct claw_privbk  *privptr;
	DECLARE_WAITQUEUE(wait, current);
        struct timer_list  timer;
        struct ccwbk *p_buf;

501
	CLAW_DBF_TEXT(4, trace, "open");
502
	privptr = (struct claw_privbk *)dev->ml_priv;
L
Linus Torvalds 已提交
503 504 505 506
        /*   allocate and initialize CCW blocks */
	if (privptr->buffs_alloc == 0) {
	        rc=init_ccw_bk(dev);
        	if (rc) {
507
			CLAW_DBF_TEXT(2, trace, "openmem");
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
                	return -ENOMEM;
        	}
	}
        privptr->system_validate_comp=0;
        privptr->release_pend=0;
	if(strncmp(privptr->p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
		privptr->p_env->read_size=DEF_PACK_BUFSIZE;
		privptr->p_env->write_size=DEF_PACK_BUFSIZE;
		privptr->p_env->packing=PACKING_ASK;
	} else {
		privptr->p_env->packing=0;
		privptr->p_env->read_size=CLAW_FRAME_SIZE;
		privptr->p_env->write_size=CLAW_FRAME_SIZE;
	}
        claw_set_busy(dev);
523 524
	tasklet_init(&privptr->channel[READ_CHANNEL].tasklet, claw_irq_tasklet,
		(unsigned long) &privptr->channel[READ_CHANNEL]);
L
Linus Torvalds 已提交
525
        for ( i = 0; i < 2;  i++) {
526
		CLAW_DBF_TEXT_(2, trace, "opn_ch%d", i);
L
Linus Torvalds 已提交
527 528
                init_waitqueue_head(&privptr->channel[i].wait);
		/* skb_queue_head_init(&p_ch->io_queue); */
529
		if (i == WRITE_CHANNEL)
L
Linus Torvalds 已提交
530
			skb_queue_head_init(
531
				&privptr->channel[WRITE_CHANNEL].collect_queue);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
                privptr->channel[i].flag_a = 0;
                privptr->channel[i].IO_active = 0;
                privptr->channel[i].flag  &= ~CLAW_TIMER;
                init_timer(&timer);
                timer.function = (void *)claw_timer;
                timer.data = (unsigned long)(&privptr->channel[i]);
                timer.expires = jiffies + 15*HZ;
                add_timer(&timer);
                spin_lock_irqsave(get_ccwdev_lock(
			privptr->channel[i].cdev), saveflags);
                parm = (unsigned long) &privptr->channel[i];
                privptr->channel[i].claw_state = CLAW_START_HALT_IO;
		rc = 0;
		add_wait_queue(&privptr->channel[i].wait, &wait);
                rc = ccw_device_halt(
			(struct ccw_device *)privptr->channel[i].cdev,parm);
                set_current_state(TASK_INTERRUPTIBLE);
                spin_unlock_irqrestore(
			get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
                schedule();
		set_current_state(TASK_RUNNING);
                remove_wait_queue(&privptr->channel[i].wait, &wait);
                if(rc != 0)
                        ccw_check_return_code(privptr->channel[i].cdev, rc);
                if((privptr->channel[i].flag & CLAW_TIMER) == 0x00)
                        del_timer(&timer);
        }
559 560
	if ((((privptr->channel[READ_CHANNEL].last_dstat |
		privptr->channel[WRITE_CHANNEL].last_dstat) &
L
Linus Torvalds 已提交
561
           ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) ||
562 563 564
	   (((privptr->channel[READ_CHANNEL].flag |
		privptr->channel[WRITE_CHANNEL].flag) & CLAW_TIMER) != 0x00)) {
		dev_info(&privptr->channel[READ_CHANNEL].cdev->dev,
565
			"%s: remote side is not ready\n", dev->name);
566
		CLAW_DBF_TEXT(2, trace, "notrdy");
L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

                for ( i = 0; i < 2;  i++) {
                        spin_lock_irqsave(
				get_ccwdev_lock(privptr->channel[i].cdev),
				saveflags);
                        parm = (unsigned long) &privptr->channel[i];
                        privptr->channel[i].claw_state = CLAW_STOP;
                        rc = ccw_device_halt(
				(struct ccw_device *)&privptr->channel[i].cdev,
				parm);
                        spin_unlock_irqrestore(
				get_ccwdev_lock(privptr->channel[i].cdev),
				saveflags);
                        if (rc != 0) {
                                ccw_check_return_code(
					privptr->channel[i].cdev, rc);
                        }
                }
                free_pages((unsigned long)privptr->p_buff_ccw,
			(int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
                if (privptr->p_env->read_size < PAGE_SIZE) {
                        free_pages((unsigned long)privptr->p_buff_read,
			       (int)pages_to_order_of_mag(
			       		privptr->p_buff_read_num));
                }
                else {
                        p_buf=privptr->p_read_active_first;
                        while (p_buf!=NULL) {
                                free_pages((unsigned long)p_buf->p_buffer,
				      (int)pages_to_order_of_mag(
				      	privptr->p_buff_pages_perread ));
                                p_buf=p_buf->next;
                        }
                }
                if (privptr->p_env->write_size < PAGE_SIZE ) {
                        free_pages((unsigned long)privptr->p_buff_write,
			     (int)pages_to_order_of_mag(
			     	privptr->p_buff_write_num));
                }
                else {
                        p_buf=privptr->p_write_active_first;
                        while (p_buf!=NULL) {
                                free_pages((unsigned long)p_buf->p_buffer,
				     (int)pages_to_order_of_mag(
				     	privptr->p_buff_pages_perwrite ));
                                p_buf=p_buf->next;
                        }
                }
		privptr->buffs_alloc = 0;
616 617
		privptr->channel[READ_CHANNEL].flag = 0x00;
		privptr->channel[WRITE_CHANNEL].flag = 0x00;
L
Linus Torvalds 已提交
618 619 620 621
                privptr->p_buff_ccw=NULL;
                privptr->p_buff_read=NULL;
                privptr->p_buff_write=NULL;
                claw_clear_busy(dev);
622
		CLAW_DBF_TEXT(2, trace, "open EIO");
L
Linus Torvalds 已提交
623 624 625 626 627 628
                return -EIO;
        }

        /*   Send SystemValidate command */

        claw_clear_busy(dev);
629
	CLAW_DBF_TEXT(4, trace, "openok");
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        return 0;
}    /*     end of claw_open    */

/*-------------------------------------------------------------------*
*                                                                    *
*       claw_irq_handler                                             *
*                                                                    *
*--------------------------------------------------------------------*/
static void
claw_irq_handler(struct ccw_device *cdev,
	unsigned long intparm, struct irb *irb)
{
        struct chbk *p_ch = NULL;
        struct claw_privbk *privptr = NULL;
        struct net_device *dev = NULL;
        struct claw_env  *p_env;
        struct chbk *p_ch_r=NULL;

648
	kstat_cpu(smp_processor_id()).irqs[IOINT_CLW]++;
649
	CLAW_DBF_TEXT(4, trace, "clawirq");
L
Linus Torvalds 已提交
650
        /* Bypass all 'unsolicited interrupts' */
651 652
	privptr = dev_get_drvdata(&cdev->dev);
	if (!privptr) {
653 654 655
		dev_warn(&cdev->dev, "An uninitialized CLAW device received an"
			" IRQ, c-%02x d-%02x\n",
			irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
656
		CLAW_DBF_TEXT(2, trace, "badirq");
L
Linus Torvalds 已提交
657 658 659 660
                return;
        }

	/* Try to extract channel from driver data. */
661 662 663 664
	if (privptr->channel[READ_CHANNEL].cdev == cdev)
		p_ch = &privptr->channel[READ_CHANNEL];
	else if (privptr->channel[WRITE_CHANNEL].cdev == cdev)
		p_ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
665
	else {
666
		dev_warn(&cdev->dev, "The device is not a CLAW device\n");
667
		CLAW_DBF_TEXT(2, trace, "badchan");
L
Linus Torvalds 已提交
668 669
		return;
	}
670
	CLAW_DBF_TEXT_(4, trace, "IRQCH=%d", p_ch->flag);
L
Linus Torvalds 已提交
671 672 673 674 675 676 677

	dev = (struct net_device *) (p_ch->ndev);
        p_env=privptr->p_env;

	/* Copy interruption response block. */
	memcpy(p_ch->irb, irb, sizeof(struct irb));

678
	/* Check for good subchannel return code, otherwise info message */
679
	if (irb->scsw.cmd.cstat && !(irb->scsw.cmd.cstat & SCHN_STAT_PCI)) {
680 681
		dev_info(&cdev->dev,
			"%s: subchannel check for device: %04x -"
L
Linus Torvalds 已提交
682 683
			" Sch Stat %02x  Dev Stat %02x CPA - %04x\n",
                        dev->name, p_ch->devno,
684 685
			irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
			irb->scsw.cmd.cpa);
686
		CLAW_DBF_TEXT(2, trace, "chanchk");
L
Linus Torvalds 已提交
687 688 689 690
                /* return; */
        }

        /* Check the reason-code of a unit check */
691
	if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
L
Linus Torvalds 已提交
692 693 694
                ccw_check_unit_check(p_ch, irb->ecw[0]);

        /* State machine to bring the connection up, down and to restart */
695
	p_ch->last_dstat = irb->scsw.cmd.dstat;
L
Linus Torvalds 已提交
696 697

        switch (p_ch->claw_state) {
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	case CLAW_STOP:/* HALT_IO by claw_release (halt sequence) */
		if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
		(p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
		(p_ch->irb->scsw.cmd.stctl ==
		(SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))))
			return;
		wake_up(&p_ch->wait);   /* wake up claw_release */
		CLAW_DBF_TEXT(4, trace, "stop");
		return;
	case CLAW_START_HALT_IO: /* HALT_IO issued by claw_open  */
		if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
		(p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
		(p_ch->irb->scsw.cmd.stctl ==
		(SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
			CLAW_DBF_TEXT(4, trace, "haltio");
			return;
		}
		if (p_ch->flag == CLAW_READ) {
			p_ch->claw_state = CLAW_START_READ;
			wake_up(&p_ch->wait); /* wake claw_open (READ)*/
		} else if (p_ch->flag == CLAW_WRITE) {
			p_ch->claw_state = CLAW_START_WRITE;
720
			/*      send SYSTEM_VALIDATE                    */
721 722 723 724 725 726 727
			claw_strt_read(dev, LOCK_NO);
			claw_send_control(dev,
				SYSTEM_VALIDATE_REQUEST,
				0, 0, 0,
				p_env->host_name,
				p_env->adapter_name);
		} else {
728 729 730
			dev_warn(&cdev->dev, "The CLAW device received"
				" an unexpected IRQ, "
				"c-%02x d-%02x\n",
731 732 733 734 735 736 737 738 739 740 741 742 743 744
				irb->scsw.cmd.cstat,
				irb->scsw.cmd.dstat);
			return;
			}
		CLAW_DBF_TEXT(4, trace, "haltio");
		return;
	case CLAW_START_READ:
		CLAW_DBF_TEXT(4, trace, "ReadIRQ");
		if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
			clear_bit(0, (void *)&p_ch->IO_active);
			if ((p_ch->irb->ecw[0] & 0x41) == 0x41 ||
			    (p_ch->irb->ecw[0] & 0x40) == 0x40 ||
			    (p_ch->irb->ecw[0])        == 0) {
				privptr->stats.rx_errors++;
745 746
				dev_info(&cdev->dev,
					"%s: Restart is required after remote "
747 748 749 750 751 752 753 754 755 756 757
					"side recovers \n",
					dev->name);
			}
			CLAW_DBF_TEXT(4, trace, "notrdy");
			return;
		}
		if ((p_ch->irb->scsw.cmd.cstat & SCHN_STAT_PCI) &&
			(p_ch->irb->scsw.cmd.dstat == 0)) {
			if (test_and_set_bit(CLAW_BH_ACTIVE,
				(void *)&p_ch->flag_a) == 0)
				tasklet_schedule(&p_ch->tasklet);
L
Linus Torvalds 已提交
758
			else
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
				CLAW_DBF_TEXT(4, trace, "PCINoBH");
			CLAW_DBF_TEXT(4, trace, "PCI_read");
			return;
		}
		if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
		 (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
		 (p_ch->irb->scsw.cmd.stctl ==
		 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
			CLAW_DBF_TEXT(4, trace, "SPend_rd");
			return;
		}
		clear_bit(0, (void *)&p_ch->IO_active);
		claw_clearbit_busy(TB_RETRY, dev);
		if (test_and_set_bit(CLAW_BH_ACTIVE,
			(void *)&p_ch->flag_a) == 0)
			tasklet_schedule(&p_ch->tasklet);
		else
			CLAW_DBF_TEXT(4, trace, "RdBHAct");
		CLAW_DBF_TEXT(4, trace, "RdIRQXit");
		return;
	case CLAW_START_WRITE:
		if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
781 782
			dev_info(&cdev->dev,
				"%s: Unit Check Occured in "
783 784 785
				"write channel\n", dev->name);
			clear_bit(0, (void *)&p_ch->IO_active);
			if (p_ch->irb->ecw[0] & 0x80) {
786 787
				dev_info(&cdev->dev,
					"%s: Resetting Event "
788 789 790 791 792 793 794
					"occurred:\n", dev->name);
				init_timer(&p_ch->timer);
				p_ch->timer.function =
					(void *)claw_write_retry;
				p_ch->timer.data = (unsigned long)p_ch;
				p_ch->timer.expires = jiffies + 10*HZ;
				add_timer(&p_ch->timer);
795 796
				dev_info(&cdev->dev,
					"%s: write connection "
797 798 799 800 801 802 803
					"restarting\n", dev->name);
			}
			CLAW_DBF_TEXT(4, trace, "rstrtwrt");
			return;
		}
		if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
			clear_bit(0, (void *)&p_ch->IO_active);
804 805 806 807
			dev_info(&cdev->dev,
				"%s: Unit Exception "
				"occurred in write channel\n",
				dev->name);
808 809 810 811 812 813 814 815 816 817 818 819 820 821
		}
		if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
		(p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
		(p_ch->irb->scsw.cmd.stctl ==
		(SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) {
			CLAW_DBF_TEXT(4, trace, "writeUE");
			return;
		}
		clear_bit(0, (void *)&p_ch->IO_active);
		if (claw_test_and_setbit_busy(TB_TX, dev) == 0) {
			claw_write_next(p_ch);
			claw_clearbit_busy(TB_TX, dev);
			claw_clear_busy(dev);
		}
822
		p_ch_r = (struct chbk *)&privptr->channel[READ_CHANNEL];
823 824 825 826 827 828
		if (test_and_set_bit(CLAW_BH_ACTIVE,
			(void *)&p_ch_r->flag_a) == 0)
			tasklet_schedule(&p_ch_r->tasklet);
		CLAW_DBF_TEXT(4, trace, "StWtExit");
		return;
	default:
829 830 831
		dev_warn(&cdev->dev,
			"The CLAW device for %s received an unexpected IRQ\n",
			 dev->name);
832 833
		CLAW_DBF_TEXT(2, trace, "badIRQ");
		return;
L
Linus Torvalds 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
        }

}       /*   end of claw_irq_handler    */


/*-------------------------------------------------------------------*
*       claw_irq_tasklet                                             *
*                                                                    *
*--------------------------------------------------------------------*/
static void
claw_irq_tasklet ( unsigned long data )
{
	struct chbk * p_ch;
        struct net_device  *dev;
        struct claw_privbk *       privptr;

	p_ch = (struct chbk *) data;
        dev = (struct net_device *)p_ch->ndev;
852
	CLAW_DBF_TEXT(4, trace, "IRQtask");
853
	privptr = (struct claw_privbk *)dev->ml_priv;
L
Linus Torvalds 已提交
854 855
        unpack_read(dev);
        clear_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a);
856
	CLAW_DBF_TEXT(4, trace, "TskletXt");
L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
        return;
}       /*    end of claw_irq_bh    */

/*-------------------------------------------------------------------*
*       claw_release                                                 *
*                                                                    *
*--------------------------------------------------------------------*/
static int
claw_release(struct net_device *dev)
{
        int                rc;
        int                i;
        unsigned long      saveflags;
        unsigned long      parm;
        struct claw_privbk *privptr;
        DECLARE_WAITQUEUE(wait, current);
        struct ccwbk*             p_this_ccw;
        struct ccwbk*             p_buf;

	if (!dev)
                return 0;
878
	privptr = (struct claw_privbk *)dev->ml_priv;
L
Linus Torvalds 已提交
879 880
        if (!privptr)
                return 0;
881
	CLAW_DBF_TEXT(4, trace, "release");
L
Linus Torvalds 已提交
882 883 884 885 886
        privptr->release_pend=1;
        claw_setbit_busy(TB_STOP,dev);
        for ( i = 1; i >=0 ;  i--) {
                spin_lock_irqsave(
			get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
887
	     /*   del_timer(&privptr->channel[READ_CHANNEL].timer);  */
L
Linus Torvalds 已提交
888 889 890
 		privptr->channel[i].claw_state = CLAW_STOP;
                privptr->channel[i].IO_active = 0;
                parm = (unsigned long) &privptr->channel[i];
891
		if (i == WRITE_CHANNEL)
L
Linus Torvalds 已提交
892
			claw_purge_skb_queue(
893
				&privptr->channel[WRITE_CHANNEL].collect_queue);
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
                rc = ccw_device_halt (privptr->channel[i].cdev, parm);
	        if (privptr->system_validate_comp==0x00)  /* never opened? */
                   init_waitqueue_head(&privptr->channel[i].wait);
                add_wait_queue(&privptr->channel[i].wait, &wait);
                set_current_state(TASK_INTERRUPTIBLE);
	        spin_unlock_irqrestore(
			get_ccwdev_lock(privptr->channel[i].cdev), saveflags);
	        schedule();
		set_current_state(TASK_RUNNING);
	        remove_wait_queue(&privptr->channel[i].wait, &wait);
	        if (rc != 0) {
                        ccw_check_return_code(privptr->channel[i].cdev, rc);
                }
        }
	if (privptr->pk_skb != NULL) {
F
Frank Pavlic 已提交
909
		dev_kfree_skb_any(privptr->pk_skb);
L
Linus Torvalds 已提交
910 911 912
		privptr->pk_skb = NULL;
	}
	if(privptr->buffs_alloc != 1) {
913
		CLAW_DBF_TEXT(4, trace, "none2fre");
L
Linus Torvalds 已提交
914 915
		return 0;
	}
916
	CLAW_DBF_TEXT(4, trace, "freebufs");
L
Linus Torvalds 已提交
917 918 919 920
	if (privptr->p_buff_ccw != NULL) {
        	free_pages((unsigned long)privptr->p_buff_ccw,
	        	(int)pages_to_order_of_mag(privptr->p_buff_ccw_num));
	}
921
	CLAW_DBF_TEXT(4, trace, "freeread");
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
        if (privptr->p_env->read_size < PAGE_SIZE) {
	    if (privptr->p_buff_read != NULL) {
                free_pages((unsigned long)privptr->p_buff_read,
		      (int)pages_to_order_of_mag(privptr->p_buff_read_num));
		}
        }
        else {
                p_buf=privptr->p_read_active_first;
                while (p_buf!=NULL) {
                        free_pages((unsigned long)p_buf->p_buffer,
			     (int)pages_to_order_of_mag(
			     	privptr->p_buff_pages_perread ));
                        p_buf=p_buf->next;
                }
        }
937
	 CLAW_DBF_TEXT(4, trace, "freewrit");
L
Linus Torvalds 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950
        if (privptr->p_env->write_size < PAGE_SIZE ) {
                free_pages((unsigned long)privptr->p_buff_write,
		      (int)pages_to_order_of_mag(privptr->p_buff_write_num));
        }
        else {
                p_buf=privptr->p_write_active_first;
                while (p_buf!=NULL) {
                        free_pages((unsigned long)p_buf->p_buffer,
			      (int)pages_to_order_of_mag(
			      privptr->p_buff_pages_perwrite ));
                        p_buf=p_buf->next;
                }
        }
951
	 CLAW_DBF_TEXT(4, trace, "clearptr");
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	privptr->buffs_alloc = 0;
        privptr->p_buff_ccw=NULL;
        privptr->p_buff_read=NULL;
        privptr->p_buff_write=NULL;
        privptr->system_validate_comp=0;
        privptr->release_pend=0;
        /*      Remove any writes that were pending and reset all reads   */
        p_this_ccw=privptr->p_read_active_first;
        while (p_this_ccw!=NULL) {
                p_this_ccw->header.length=0xffff;
                p_this_ccw->header.opcode=0xff;
                p_this_ccw->header.flag=0x00;
                p_this_ccw=p_this_ccw->next;
        }

        while (privptr->p_write_active_first!=NULL) {
                p_this_ccw=privptr->p_write_active_first;
                p_this_ccw->header.flag=CLAW_PENDING;
                privptr->p_write_active_first=p_this_ccw->next;
                p_this_ccw->next=privptr->p_write_free_chain;
                privptr->p_write_free_chain=p_this_ccw;
                ++privptr->write_free_count;
        }
        privptr->p_write_active_last=NULL;
        privptr->mtc_logical_link = -1;
        privptr->mtc_skipping = 1;
        privptr->mtc_offset=0;

980 981
	if (((privptr->channel[READ_CHANNEL].last_dstat |
		privptr->channel[WRITE_CHANNEL].last_dstat) &
L
Linus Torvalds 已提交
982
		~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) {
983
		dev_warn(&privptr->channel[READ_CHANNEL].cdev->dev,
984 985 986
			"Deactivating %s completed with incorrect"
			" subchannel status "
			"(read %02x, write %02x)\n",
L
Linus Torvalds 已提交
987
                dev->name,
988 989
		privptr->channel[READ_CHANNEL].last_dstat,
		privptr->channel[WRITE_CHANNEL].last_dstat);
990
		 CLAW_DBF_TEXT(2, trace, "badclose");
L
Linus Torvalds 已提交
991
        }
992
	CLAW_DBF_TEXT(4, trace, "rlsexit");
L
Linus Torvalds 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        return 0;
}      /* end of claw_release     */

/*-------------------------------------------------------------------*
*       claw_write_retry                                             *
*                                                                    *
*--------------------------------------------------------------------*/

static void
claw_write_retry ( struct chbk *p_ch )
{

        struct net_device  *dev=p_ch->ndev;

1007
	CLAW_DBF_TEXT(4, trace, "w_retry");
L
Linus Torvalds 已提交
1008 1009 1010 1011
        if (p_ch->claw_state == CLAW_STOP) {
        	return;
        }
	claw_strt_out_IO( dev );
1012
	CLAW_DBF_TEXT(4, trace, "rtry_xit");
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
        return;
}      /* end of claw_write_retry      */


/*-------------------------------------------------------------------*
*       claw_write_next                                              *
*                                                                    *
*--------------------------------------------------------------------*/

static void
claw_write_next ( struct chbk * p_ch )
{

        struct net_device  *dev;
        struct claw_privbk *privptr=NULL;
	struct sk_buff *pk_skb;
	int	rc;

1031
	CLAW_DBF_TEXT(4, trace, "claw_wrt");
L
Linus Torvalds 已提交
1032 1033 1034
        if (p_ch->claw_state == CLAW_STOP)
                return;
        dev = (struct net_device *) p_ch->ndev;
1035
	privptr = (struct claw_privbk *) dev->ml_priv;
L
Linus Torvalds 已提交
1036 1037
        claw_free_wrt_buf( dev );
	if ((privptr->write_free_count > 0) &&
1038
	    !skb_queue_empty(&p_ch->collect_queue)) {
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	  	pk_skb = claw_pack_skb(privptr);
		while (pk_skb != NULL) {
			rc = claw_hw_tx( pk_skb, dev,1);
			if (privptr->write_free_count > 0) {
	   			pk_skb = claw_pack_skb(privptr);
			} else
				pk_skb = NULL;
		}
	}
        if (privptr->p_write_active_first!=NULL) {
                claw_strt_out_IO(dev);
        }
        return;
}      /* end of claw_write_next      */

/*-------------------------------------------------------------------*
*                                                                    *
*       claw_timer                                                   *
*--------------------------------------------------------------------*/

static void
claw_timer ( struct chbk * p_ch )
{
1062
	CLAW_DBF_TEXT(4, trace, "timer");
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        p_ch->flag |= CLAW_TIMER;
        wake_up(&p_ch->wait);
        return;
}      /* end of claw_timer  */

/*
*
*       functions
*/


/*-------------------------------------------------------------------*
*                                                                    *
*     pages_to_order_of_mag                                          *
*                                                                    *
*    takes a number of pages from 1 to 512 and returns the           *
*    log(num_pages)/log(2) get_free_pages() needs a base 2 order     *
*    of magnitude get_free_pages() has an upper order of 9           *
*--------------------------------------------------------------------*/

1083
static int
L
Linus Torvalds 已提交
1084 1085 1086
pages_to_order_of_mag(int num_of_pages)
{
	int	order_of_mag=1;		/* assume 2 pages */
1087
	int	nump;
1088 1089

	CLAW_DBF_TEXT_(5, trace, "pages%d", num_of_pages);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	if (num_of_pages == 1)   {return 0; }  /* magnitude of 0 = 1 page */
	/* 512 pages = 2Meg on 4k page systems */
	if (num_of_pages >= 512) {return 9; }
	/* we have two or more pages order is at least 1 */
	for (nump=2 ;nump <= 512;nump*=2) {
	  if (num_of_pages <= nump)
		  break;
	  order_of_mag +=1;
	}
	if (order_of_mag > 9) { order_of_mag = 9; }  /* I know it's paranoid */
1100
	CLAW_DBF_TEXT_(5, trace, "mag%d", order_of_mag);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	return order_of_mag;
}

/*-------------------------------------------------------------------*
*                                                                    *
*     add_claw_reads                                                 *
*                                                                    *
*--------------------------------------------------------------------*/
static int
add_claw_reads(struct net_device *dev, struct ccwbk* p_first,
	struct ccwbk* p_last)
{
        struct claw_privbk *privptr;
        struct ccw1  temp_ccw;
        struct endccw * p_end;
1116
	CLAW_DBF_TEXT(4, trace, "addreads");
1117
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123
        p_end = privptr->p_end_ccw;

        /* first CCW and last CCW contains a new set of read channel programs
        *       to apend the running channel programs
        */
        if ( p_first==NULL) {
1124
		CLAW_DBF_TEXT(4, trace, "addexit");
L
Linus Torvalds 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
                return 0;
        }

        /* set up ending CCW sequence for this segment */
        if (p_end->read1) {
                p_end->read1=0x00;    /*  second ending CCW is now active */
                /*      reset ending CCWs and setup TIC CCWs              */
                p_end->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
                p_end->read2_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
                p_last->r_TIC_1.cda =(__u32)__pa(&p_end->read2_nop1);
                p_last->r_TIC_2.cda =(__u32)__pa(&p_end->read2_nop1);
                p_end->read2_nop2.cda=0;
                p_end->read2_nop2.count=1;
        }
        else {
                p_end->read1=0x01;  /* first ending CCW is now active */
                /*      reset ending CCWs and setup TIC CCWs          */
                p_end->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
                p_end->read1_nop2.flags  = CCW_FLAG_SLI | CCW_FLAG_SKIP;
                p_last->r_TIC_1.cda = (__u32)__pa(&p_end->read1_nop1);
                p_last->r_TIC_2.cda = (__u32)__pa(&p_end->read1_nop1);
                p_end->read1_nop2.cda=0;
                p_end->read1_nop2.count=1;
        }

        if ( privptr-> p_read_active_first ==NULL ) {
1151 1152
		privptr->p_read_active_first = p_first;  /*  set new first */
		privptr->p_read_active_last  = p_last;   /*  set new last  */
L
Linus Torvalds 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        }
        else {

                /* set up TIC ccw  */
                temp_ccw.cda= (__u32)__pa(&p_first->read);
                temp_ccw.count=0;
                temp_ccw.flags=0;
                temp_ccw.cmd_code = CCW_CLAW_CMD_TIC;


                if (p_end->read1) {

               /* first set of CCW's is chained to the new read              */
               /* chain, so the second set is chained to the active chain.   */
               /* Therefore modify the second set to point to the new        */
               /* read chain set up TIC CCWs                                 */
               /* make sure we update the CCW so channel doesn't fetch it    */
               /* when it's only half done                                   */
                        memcpy( &p_end->read2_nop2, &temp_ccw ,
				sizeof(struct ccw1));
                        privptr->p_read_active_last->r_TIC_1.cda=
				(__u32)__pa(&p_first->read);
                        privptr->p_read_active_last->r_TIC_2.cda=
				(__u32)__pa(&p_first->read);
                }
                else {
                        /* make sure we update the CCW so channel doesn't   */
			/* fetch it when it is only half done               */
                        memcpy( &p_end->read1_nop2, &temp_ccw ,
				sizeof(struct ccw1));
                        privptr->p_read_active_last->r_TIC_1.cda=
				(__u32)__pa(&p_first->read);
                        privptr->p_read_active_last->r_TIC_2.cda=
				(__u32)__pa(&p_first->read);
                }
1188
		/*      chain in new set of blocks                         */
L
Linus Torvalds 已提交
1189 1190 1191
                privptr->p_read_active_last->next = p_first;
                privptr->p_read_active_last=p_last;
        } /* end of if ( privptr-> p_read_active_first ==NULL)  */
1192
	CLAW_DBF_TEXT(4, trace, "addexit");
L
Linus Torvalds 已提交
1193 1194 1195 1196 1197 1198 1199 1200
        return 0;
}    /*     end of add_claw_reads   */

/*-------------------------------------------------------------------*
 *   ccw_check_return_code                                           *
 *                                                                   *
 *-------------------------------------------------------------------*/

1201
static void
L
Linus Torvalds 已提交
1202 1203
ccw_check_return_code(struct ccw_device *cdev, int return_code)
{
1204
	CLAW_DBF_TEXT(4, trace, "ccwret");
L
Linus Torvalds 已提交
1205 1206
        if (return_code != 0) {
                switch (return_code) {
1207 1208 1209
		case -EBUSY: /* BUSY is a transient state no action needed */
			break;
		case -ENODEV:
1210 1211
			dev_err(&cdev->dev, "The remote channel adapter is not"
				" available\n");
1212 1213
			break;
		case -EINVAL:
1214 1215 1216
			dev_err(&cdev->dev,
				"The status of the remote channel adapter"
				" is not valid\n");
1217 1218
			break;
		default:
1219 1220 1221
			dev_err(&cdev->dev, "The common device layer"
				" returned error code %d\n",
				  return_code);
1222 1223 1224
		}
	}
	CLAW_DBF_TEXT(4, trace, "ccwret");
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230
}    /*    end of ccw_check_return_code   */

/*-------------------------------------------------------------------*
*       ccw_check_unit_check                                         *
*--------------------------------------------------------------------*/

1231
static void
L
Linus Torvalds 已提交
1232 1233
ccw_check_unit_check(struct chbk * p_ch, unsigned char sense )
{
1234
	struct net_device *ndev = p_ch->ndev;
1235
	struct device *dev = &p_ch->cdev->dev;
L
Linus Torvalds 已提交
1236

1237
	CLAW_DBF_TEXT(4, trace, "unitchek");
1238 1239
	dev_warn(dev, "The communication peer of %s disconnected\n",
		ndev->name);
L
Linus Torvalds 已提交
1240

1241 1242
	if (sense & 0x40) {
		if (sense & 0x01) {
1243 1244 1245
			dev_warn(dev, "The remote channel adapter for"
				" %s has been reset\n",
				ndev->name);
1246 1247 1248
		}
	} else if (sense & 0x20) {
		if (sense & 0x04) {
1249 1250 1251
			dev_warn(dev, "A data streaming timeout occurred"
				" for %s\n",
				ndev->name);
1252
		} else if (sense & 0x10) {
1253 1254 1255
			dev_warn(dev, "The remote channel adapter for %s"
				" is faulty\n",
				ndev->name);
1256 1257
		} else {
			dev_warn(dev, "A data transfer parity error occurred"
1258 1259
				" for %s\n",
				ndev->name);
1260 1261 1262 1263 1264 1265
		}
	} else if (sense & 0x10) {
		dev_warn(dev, "A read data parity error occurred"
			" for %s\n",
			ndev->name);
	}
L
Linus Torvalds 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

}   /*    end of ccw_check_unit_check    */

/*-------------------------------------------------------------------*
*               find_link                                            *
*--------------------------------------------------------------------*/
static int
find_link(struct net_device *dev, char *host_name, char *ws_name )
{
	struct claw_privbk *privptr;
	struct claw_env *p_env;
	int    rc=0;

1279
	CLAW_DBF_TEXT(2, setup, "findlink");
1280
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
        p_env=privptr->p_env;
	switch (p_env->packing)
	{
		case  PACKING_ASK:
			if ((memcmp(WS_APPL_NAME_PACKED, host_name, 8)!=0) ||
			    (memcmp(WS_APPL_NAME_PACKED, ws_name, 8)!=0 ))
        	             rc = EINVAL;
			break;
		case  DO_PACKED:
		case  PACK_SEND:
			if ((memcmp(WS_APPL_NAME_IP_NAME, host_name, 8)!=0) ||
			    (memcmp(WS_APPL_NAME_IP_NAME, ws_name, 8)!=0 ))
        	        	rc = EINVAL;
			break;
		default:
	       		if ((memcmp(HOST_APPL_NAME, host_name, 8)!=0) ||
		    	    (memcmp(p_env->api_type , ws_name, 8)!=0))
        	        	rc = EINVAL;
			break;
	}

1302
	return rc;
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
}    /*    end of find_link    */

/*-------------------------------------------------------------------*
 *   claw_hw_tx                                                      *
 *                                                                   *
 *                                                                   *
 *-------------------------------------------------------------------*/

static int
claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid)
{
        int                             rc=0;
        struct claw_privbk 		*privptr;
        struct ccwbk           *p_this_ccw;
        struct ccwbk           *p_first_ccw;
        struct ccwbk           *p_last_ccw;
        __u32                           numBuffers;
        signed long                     len_of_data;
        unsigned long                   bytesInThisBuffer;
        unsigned char                   *pDataAddress;
        struct endccw                   *pEnd;
        struct ccw1                     tempCCW;
        struct chbk                     *p_ch;
	struct claw_env			*p_env;
        int                             lock;
	struct clawph			*pk_head;
	struct chbk			*ch;
1330 1331

	CLAW_DBF_TEXT(4, trace, "hw_tx");
1332
	privptr = (struct claw_privbk *)(dev->ml_priv);
1333
	p_ch = (struct chbk *)&privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	p_env =privptr->p_env;
	claw_free_wrt_buf(dev);	/* Clean up free chain if posible */
        /*  scan the write queue to free any completed write packets   */
        p_first_ccw=NULL;
        p_last_ccw=NULL;
	if ((p_env->packing >= PACK_SEND) &&
       	    (skb->cb[1] != 'P')) {
		skb_push(skb,sizeof(struct clawph));
		pk_head=(struct clawph *)skb->data;
		pk_head->len=skb->len-sizeof(struct clawph);
		if (pk_head->len%4)  {
			pk_head->len+= 4-(pk_head->len%4);
			skb_pad(skb,4-(pk_head->len%4));
			skb_put(skb,4-(pk_head->len%4));
		}
		if (p_env->packing == DO_PACKED)
			pk_head->link_num = linkid;
		else
			pk_head->link_num = 0;
		pk_head->flag = 0x00;
		skb_pad(skb,4);
		skb->cb[1] = 'P';
	}
        if (linkid == 0) {
        	if (claw_check_busy(dev)) {
                	if (privptr->write_free_count!=0) {
                                claw_clear_busy(dev);
                        }
                        else {
                                claw_strt_out_IO(dev );
                                claw_free_wrt_buf( dev );
                                if (privptr->write_free_count==0) {
1366
					ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
					atomic_inc(&skb->users);
					skb_queue_tail(&ch->collect_queue, skb);
                                	goto Done;
                                }
                                else {
                                	claw_clear_busy(dev);
                                }
                        }
                }
                /*  tx lock  */
                if (claw_test_and_setbit_busy(TB_TX,dev)) { /* set to busy */
1378
			ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
1379 1380 1381 1382 1383 1384 1385 1386
			atomic_inc(&skb->users);
			skb_queue_tail(&ch->collect_queue, skb);
                        claw_strt_out_IO(dev );
                        rc=-EBUSY;
                        goto Done2;
                }
        }
        /*      See how many write buffers are required to hold this data */
J
Julia Lawall 已提交
1387
	numBuffers = DIV_ROUND_UP(skb->len, privptr->p_env->write_size);
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393

        /*      If that number of buffers isn't available, give up for now */
        if (privptr->write_free_count < numBuffers ||
            privptr->p_write_free_chain == NULL ) {

                claw_setbit_busy(TB_NOBUFFER,dev);
1394
		ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
1395 1396
		atomic_inc(&skb->users);
		skb_queue_tail(&ch->collect_queue, skb);
1397
		CLAW_DBF_TEXT(2, trace, "clawbusy");
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403 1404 1405
                goto Done2;
        }
        pDataAddress=skb->data;
        len_of_data=skb->len;

        while (len_of_data > 0) {
                p_this_ccw=privptr->p_write_free_chain;  /* get a block */
		if (p_this_ccw == NULL) { /* lost the race */
1406
			ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
1407 1408 1409 1410 1411 1412 1413
			atomic_inc(&skb->users);
			skb_queue_tail(&ch->collect_queue, skb);
			goto Done2;
		}
                privptr->p_write_free_chain=p_this_ccw->next;
                p_this_ccw->next=NULL;
                --privptr->write_free_count; /* -1 */
1414 1415 1416 1417
		if (len_of_data >= privptr->p_env->write_size)
			bytesInThisBuffer = privptr->p_env->write_size;
		else
			bytesInThisBuffer = len_of_data;
L
Linus Torvalds 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
                memcpy( p_this_ccw->p_buffer,pDataAddress, bytesInThisBuffer);
                len_of_data-=bytesInThisBuffer;
                pDataAddress+=(unsigned long)bytesInThisBuffer;
                /*      setup write CCW         */
                p_this_ccw->write.cmd_code = (linkid * 8) +1;
                if (len_of_data>0) {
                        p_this_ccw->write.cmd_code+=MORE_to_COME_FLAG;
                }
                p_this_ccw->write.count=bytesInThisBuffer;
                /*      now add to end of this chain    */
                if (p_first_ccw==NULL)    {
                        p_first_ccw=p_this_ccw;
                }
                if (p_last_ccw!=NULL) {
                        p_last_ccw->next=p_this_ccw;
                        /*      set up TIC ccws         */
                        p_last_ccw->w_TIC_1.cda=
				(__u32)__pa(&p_this_ccw->write);
                }
                p_last_ccw=p_this_ccw;      /* save new last block */
        }

        /*      FirstCCW and LastCCW now contain a new set of write channel
        *       programs to append to the running channel program
        */

        if (p_first_ccw!=NULL) {
1445
		/*      setup ending ccw sequence for this segment           */
L
Linus Torvalds 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
                pEnd=privptr->p_end_ccw;
                if (pEnd->write1) {
                        pEnd->write1=0x00;   /* second end ccw is now active */
                        /*      set up Tic CCWs         */
                        p_last_ccw->w_TIC_1.cda=
				(__u32)__pa(&pEnd->write2_nop1);
                        pEnd->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
                        pEnd->write2_nop2.flags    =
				CCW_FLAG_SLI | CCW_FLAG_SKIP;
                        pEnd->write2_nop2.cda=0;
                        pEnd->write2_nop2.count=1;
                }
                else {  /*  end of if (pEnd->write1)*/
                        pEnd->write1=0x01;   /* first end ccw is now active */
                        /*      set up Tic CCWs         */
                        p_last_ccw->w_TIC_1.cda=
				(__u32)__pa(&pEnd->write1_nop1);
                        pEnd->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
                        pEnd->write1_nop2.flags    =
				CCW_FLAG_SLI | CCW_FLAG_SKIP;
                        pEnd->write1_nop2.cda=0;
                        pEnd->write1_nop2.count=1;
                }  /* end if if (pEnd->write1) */

                if (privptr->p_write_active_first==NULL ) {
                        privptr->p_write_active_first=p_first_ccw;
                        privptr->p_write_active_last=p_last_ccw;
                }
                else {
                        /*      set up Tic CCWs         */

                        tempCCW.cda=(__u32)__pa(&p_first_ccw->write);
                        tempCCW.count=0;
                        tempCCW.flags=0;
                        tempCCW.cmd_code=CCW_CLAW_CMD_TIC;

                        if (pEnd->write1) {

                 /*
                 * first set of ending CCW's is chained to the new write
                 * chain, so the second set is chained to the active chain
                 * Therefore modify the second set to point the new write chain.
                 * make sure we update the CCW atomically
                 * so channel does not fetch it when it's only half done
                 */
                                memcpy( &pEnd->write2_nop2, &tempCCW ,
					sizeof(struct ccw1));
                                privptr->p_write_active_last->w_TIC_1.cda=
					(__u32)__pa(&p_first_ccw->write);
                        }
                        else {

                        /*make sure we update the CCW atomically
                         *so channel does not fetch it when it's only half done
                         */
                                memcpy(&pEnd->write1_nop2, &tempCCW ,
					sizeof(struct ccw1));
                                privptr->p_write_active_last->w_TIC_1.cda=
				        (__u32)__pa(&p_first_ccw->write);

                        } /* end if if (pEnd->write1) */

                        privptr->p_write_active_last->next=p_first_ccw;
                        privptr->p_write_active_last=p_last_ccw;
                }

        } /* endif (p_first_ccw!=NULL)  */
F
Frank Pavlic 已提交
1513
        dev_kfree_skb_any(skb);
L
Linus Torvalds 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
	if (linkid==0) {
        	lock=LOCK_NO;
        }
        else  {
                lock=LOCK_YES;
        }
        claw_strt_out_IO(dev );
        /*      if write free count is zero , set NOBUFFER       */
	if (privptr->write_free_count==0) {
		claw_setbit_busy(TB_NOBUFFER,dev);
        }
Done2:
	claw_clearbit_busy(TB_TX,dev);
Done:
	return(rc);
}    /*    end of claw_hw_tx    */

/*-------------------------------------------------------------------*
*                                                                    *
*     init_ccw_bk                                                    *
*                                                                    *
*--------------------------------------------------------------------*/

static int
init_ccw_bk(struct net_device *dev)
{

        __u32   ccw_blocks_required;
        __u32   ccw_blocks_perpage;
        __u32   ccw_pages_required;
        __u32   claw_reads_perpage=1;
        __u32   claw_read_pages;
        __u32   claw_writes_perpage=1;
        __u32   claw_write_pages;
        void    *p_buff=NULL;
        struct ccwbk*p_free_chain;
	struct ccwbk*p_buf;
	struct ccwbk*p_last_CCWB;
	struct ccwbk*p_first_CCWB;
        struct endccw *p_endccw=NULL;
1554 1555
	addr_t  real_address;
	struct claw_privbk *privptr = dev->ml_priv;
L
Linus Torvalds 已提交
1556 1557 1558
        struct clawh *pClawH=NULL;
        addr_t   real_TIC_address;
        int i,j;
1559
	CLAW_DBF_TEXT(4, trace, "init_ccw");
L
Linus Torvalds 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588

        /*  initialize  statistics field */
        privptr->active_link_ID=0;
        /*  initialize  ccwbk pointers  */
        privptr->p_write_free_chain=NULL;   /* pointer to free ccw chain*/
        privptr->p_write_active_first=NULL; /* pointer to the first write ccw*/
        privptr->p_write_active_last=NULL;  /* pointer to the last write ccw*/
        privptr->p_read_active_first=NULL;  /* pointer to the first read ccw*/
        privptr->p_read_active_last=NULL;   /* pointer to the last read ccw */
        privptr->p_end_ccw=NULL;            /* pointer to ending ccw        */
        privptr->p_claw_signal_blk=NULL;    /* pointer to signal block      */
	privptr->buffs_alloc = 0;
        memset(&privptr->end_ccw, 0x00, sizeof(struct endccw));
        memset(&privptr->ctl_bk, 0x00, sizeof(struct clawctl));
        /*  initialize  free write ccwbk counter  */
        privptr->write_free_count=0;  /* number of free bufs on write chain */
        p_last_CCWB = NULL;
        p_first_CCWB= NULL;
        /*
        *  We need 1 CCW block for each read buffer, 1 for each
        *  write buffer, plus 1 for ClawSignalBlock
        */
        ccw_blocks_required =
		privptr->p_env->read_buffers+privptr->p_env->write_buffers+1;
        /*
        * compute number of CCW blocks that will fit in a page
        */
        ccw_blocks_perpage= PAGE_SIZE /  CCWBK_SIZE;
        ccw_pages_required=
J
Julia Lawall 已提交
1589
		DIV_ROUND_UP(ccw_blocks_required, ccw_blocks_perpage);
L
Linus Torvalds 已提交
1590 1591 1592 1593 1594 1595 1596

        /*
         *  read and write sizes are set by 2 constants in claw.h
	 *  4k and 32k.  Unpacked values other than 4k are not going to
	 * provide good performance. With packing buffers support 32k
	 * buffers are used.
         */
J
Julia Lawall 已提交
1597 1598 1599 1600
	if (privptr->p_env->read_size < PAGE_SIZE) {
		claw_reads_perpage = PAGE_SIZE / privptr->p_env->read_size;
		claw_read_pages = DIV_ROUND_UP(privptr->p_env->read_buffers,
						claw_reads_perpage);
L
Linus Torvalds 已提交
1601 1602
         }
         else {       /* > or equal  */
J
Julia Lawall 已提交
1603 1604 1605 1606
		privptr->p_buff_pages_perread =
			DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
		claw_read_pages = privptr->p_env->read_buffers *
					privptr->p_buff_pages_perread;
L
Linus Torvalds 已提交
1607 1608
         }
        if (privptr->p_env->write_size < PAGE_SIZE) {
J
Julia Lawall 已提交
1609 1610 1611 1612
		claw_writes_perpage =
			PAGE_SIZE / privptr->p_env->write_size;
		claw_write_pages = DIV_ROUND_UP(privptr->p_env->write_buffers,
						claw_writes_perpage);
L
Linus Torvalds 已提交
1613 1614 1615

        }
        else {      /* >  or equal  */
J
Julia Lawall 已提交
1616 1617 1618 1619
		privptr->p_buff_pages_perwrite =
			DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE);
		claw_write_pages = privptr->p_env->write_buffers *
					privptr->p_buff_pages_perwrite;
L
Linus Torvalds 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
        }
        /*
        *               allocate ccw_pages_required
        */
        if (privptr->p_buff_ccw==NULL) {
                privptr->p_buff_ccw=
			(void *)__get_free_pages(__GFP_DMA,
		        (int)pages_to_order_of_mag(ccw_pages_required ));
                if (privptr->p_buff_ccw==NULL) {
                        return -ENOMEM;
                }
                privptr->p_buff_ccw_num=ccw_pages_required;
        }
        memset(privptr->p_buff_ccw, 0x00,
		privptr->p_buff_ccw_num * PAGE_SIZE);

        /*
        *               obtain ending ccw block address
        *
        */
        privptr->p_end_ccw = (struct endccw *)&privptr->end_ccw;
        real_address  = (__u32)__pa(privptr->p_end_ccw);
        /*                              Initialize ending CCW block       */
        p_endccw=privptr->p_end_ccw;
        p_endccw->real=real_address;
        p_endccw->write1=0x00;
        p_endccw->read1=0x00;

        /*      write1_nop1                                     */
        p_endccw->write1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
        p_endccw->write1_nop1.flags       = CCW_FLAG_SLI | CCW_FLAG_CC;
        p_endccw->write1_nop1.count       = 1;
        p_endccw->write1_nop1.cda         = 0;

        /*      write1_nop2                                     */
        p_endccw->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
        p_endccw->write1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
        p_endccw->write1_nop2.count      = 1;
        p_endccw->write1_nop2.cda        = 0;

        /*      write2_nop1                                     */
        p_endccw->write2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
        p_endccw->write2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
        p_endccw->write2_nop1.count        = 1;
        p_endccw->write2_nop1.cda          = 0;

        /*      write2_nop2                                     */
        p_endccw->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
        p_endccw->write2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
        p_endccw->write2_nop2.count        = 1;
        p_endccw->write2_nop2.cda          = 0;

        /*      read1_nop1                                      */
        p_endccw->read1_nop1.cmd_code = CCW_CLAW_CMD_NOP;
        p_endccw->read1_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
        p_endccw->read1_nop1.count        = 1;
        p_endccw->read1_nop1.cda          = 0;

        /*      read1_nop2                                      */
        p_endccw->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF;
        p_endccw->read1_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
        p_endccw->read1_nop2.count        = 1;
        p_endccw->read1_nop2.cda          = 0;

        /*      read2_nop1                                      */
        p_endccw->read2_nop1.cmd_code = CCW_CLAW_CMD_NOP;
        p_endccw->read2_nop1.flags        = CCW_FLAG_SLI | CCW_FLAG_CC;
        p_endccw->read2_nop1.count        = 1;
        p_endccw->read2_nop1.cda          = 0;

        /*      read2_nop2                                      */
        p_endccw->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF;
        p_endccw->read2_nop2.flags        = CCW_FLAG_SLI | CCW_FLAG_SKIP;
        p_endccw->read2_nop2.count        = 1;
        p_endccw->read2_nop2.cda          = 0;

        /*
        *                               Build a chain of CCWs
        *
        */
        p_buff=privptr->p_buff_ccw;

        p_free_chain=NULL;
        for (i=0 ; i < ccw_pages_required; i++ ) {
                real_address  = (__u32)__pa(p_buff);
                p_buf=p_buff;
                for (j=0 ; j < ccw_blocks_perpage ; j++) {
                        p_buf->next  = p_free_chain;
                        p_free_chain = p_buf;
                        p_buf->real=(__u32)__pa(p_buf);
                        ++p_buf;
                }
                p_buff+=PAGE_SIZE;
        }
        /*
        *                               Initialize ClawSignalBlock
        *
        */
        if (privptr->p_claw_signal_blk==NULL) {
                privptr->p_claw_signal_blk=p_free_chain;
                p_free_chain=p_free_chain->next;
                pClawH=(struct clawh *)privptr->p_claw_signal_blk;
                pClawH->length=0xffff;
                pClawH->opcode=0xff;
                pClawH->flag=CLAW_BUSY;
        }

        /*
        *               allocate write_pages_required and add to free chain
        */
        if (privptr->p_buff_write==NULL) {
            if (privptr->p_env->write_size < PAGE_SIZE) {
                privptr->p_buff_write=
			(void *)__get_free_pages(__GFP_DMA,
			(int)pages_to_order_of_mag(claw_write_pages ));
                if (privptr->p_buff_write==NULL) {
                        privptr->p_buff_ccw=NULL;
                        return -ENOMEM;
                }
                /*
                *                               Build CLAW write free chain
                *
                */

                memset(privptr->p_buff_write, 0x00,
			ccw_pages_required * PAGE_SIZE);
                privptr->p_write_free_chain=NULL;

                p_buff=privptr->p_buff_write;

                for (i=0 ; i< privptr->p_env->write_buffers ; i++) {
                        p_buf        = p_free_chain;      /*  get a CCW */
                        p_free_chain = p_buf->next;
                        p_buf->next  =privptr->p_write_free_chain;
                        privptr->p_write_free_chain = p_buf;
                        p_buf-> p_buffer	= (struct clawbuf *)p_buff;
                        p_buf-> write.cda       = (__u32)__pa(p_buff);
                        p_buf-> write.flags     = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
                        p_buf-> w_read_FF.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> w_read_FF.count   = 1;
                        p_buf-> w_read_FF.cda     =
				(__u32)__pa(&p_buf-> header.flag);
                        p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
                        p_buf-> w_TIC_1.flags      = 0;
                        p_buf-> w_TIC_1.count      = 0;

1767 1768
			if (((unsigned long)p_buff +
					    privptr->p_env->write_size) >=
L
Linus Torvalds 已提交
1769
			   ((unsigned long)(p_buff+2*
1770 1771
			    (privptr->p_env->write_size) - 1) & PAGE_MASK)) {
				p_buff = p_buff+privptr->p_env->write_size;
L
Linus Torvalds 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
                        }
                }
           }
           else      /*  Buffers are => PAGE_SIZE. 1 buff per get_free_pages */
           {
               privptr->p_write_free_chain=NULL;
               for (i = 0; i< privptr->p_env->write_buffers ; i++) {
                   p_buff=(void *)__get_free_pages(__GFP_DMA,
		        (int)pages_to_order_of_mag(
			privptr->p_buff_pages_perwrite) );
                   if (p_buff==NULL) {
                        free_pages((unsigned long)privptr->p_buff_ccw,
			      (int)pages_to_order_of_mag(
			      		privptr->p_buff_ccw_num));
                        privptr->p_buff_ccw=NULL;
			p_buf=privptr->p_buff_write;
                        while (p_buf!=NULL) {
                                free_pages((unsigned long)
					p_buf->p_buffer,
					(int)pages_to_order_of_mag(
					privptr->p_buff_pages_perwrite));
                                p_buf=p_buf->next;
                        }
                        return -ENOMEM;
                   }  /* Error on get_pages   */
                   memset(p_buff, 0x00, privptr->p_env->write_size );
                   p_buf         = p_free_chain;
                   p_free_chain  = p_buf->next;
                   p_buf->next   = privptr->p_write_free_chain;
                   privptr->p_write_free_chain = p_buf;
                   privptr->p_buff_write = p_buf;
                   p_buf->p_buffer=(struct clawbuf *)p_buff;
                   p_buf-> write.cda     = (__u32)__pa(p_buff);
                   p_buf-> write.flags   = CCW_FLAG_SLI | CCW_FLAG_CC;
                   p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
                   p_buf-> w_read_FF.flags    = CCW_FLAG_SLI | CCW_FLAG_CC;
                   p_buf-> w_read_FF.count    = 1;
                   p_buf-> w_read_FF.cda      =
			(__u32)__pa(&p_buf-> header.flag);
                   p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
                   p_buf-> w_TIC_1.flags   = 0;
                   p_buf-> w_TIC_1.count   = 0;
               }  /* for all write_buffers   */

           }    /* else buffers are PAGE_SIZE or bigger */

        }
        privptr->p_buff_write_num=claw_write_pages;
        privptr->write_free_count=privptr->p_env->write_buffers;


        /*
        *               allocate read_pages_required and chain to free chain
        */
        if (privptr->p_buff_read==NULL) {
            if (privptr->p_env->read_size < PAGE_SIZE)  {
                privptr->p_buff_read=
			(void *)__get_free_pages(__GFP_DMA,
			(int)pages_to_order_of_mag(claw_read_pages) );
                if (privptr->p_buff_read==NULL) {
                        free_pages((unsigned long)privptr->p_buff_ccw,
				(int)pages_to_order_of_mag(
					privptr->p_buff_ccw_num));
			/* free the write pages size is < page size  */
                        free_pages((unsigned long)privptr->p_buff_write,
				(int)pages_to_order_of_mag(
				privptr->p_buff_write_num));
                        privptr->p_buff_ccw=NULL;
                        privptr->p_buff_write=NULL;
                        return -ENOMEM;
                }
                memset(privptr->p_buff_read, 0x00, claw_read_pages * PAGE_SIZE);
                privptr->p_buff_read_num=claw_read_pages;
                /*
                *                               Build CLAW read free chain
                *
                */
                p_buff=privptr->p_buff_read;
                for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
                        p_buf        = p_free_chain;
                        p_free_chain = p_buf->next;

                        if (p_last_CCWB==NULL) {
                                p_buf->next=NULL;
                                real_TIC_address=0;
                                p_last_CCWB=p_buf;
                        }
                        else {
                                p_buf->next=p_first_CCWB;
                                real_TIC_address=
				(__u32)__pa(&p_first_CCWB -> read );
                        }

                        p_first_CCWB=p_buf;

                        p_buf->p_buffer=(struct clawbuf *)p_buff;
                        /*  initialize read command */
                        p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
                        p_buf-> read.cda = (__u32)__pa(p_buff);
                        p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> read.count       = privptr->p_env->read_size;

                        /*  initialize read_h command */
                        p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
                        p_buf-> read_h.cda =
				(__u32)__pa(&(p_buf->header));
                        p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> read_h.count      = sizeof(struct clawh);

                        /*  initialize Signal command */
                        p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
                        p_buf-> signal.cda =
				(__u32)__pa(&(pClawH->flag));
                        p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> signal.count     = 1;

                        /*  initialize r_TIC_1 command */
                        p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
                        p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
                        p_buf-> r_TIC_1.flags = 0;
                        p_buf-> r_TIC_1.count      = 0;

                        /*  initialize r_read_FF command */
                        p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
                        p_buf-> r_read_FF.cda =
				(__u32)__pa(&(pClawH->flag));
                        p_buf-> r_read_FF.flags =
				CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
                        p_buf-> r_read_FF.count    = 1;

                        /*    initialize r_TIC_2          */
                        memcpy(&p_buf->r_TIC_2,
				&p_buf->r_TIC_1, sizeof(struct ccw1));

                        /*     initialize Header     */
                        p_buf->header.length=0xffff;
                        p_buf->header.opcode=0xff;
                        p_buf->header.flag=CLAW_PENDING;

1911 1912 1913 1914
			if (((unsigned long)p_buff+privptr->p_env->read_size) >=
			  ((unsigned long)(p_buff+2*(privptr->p_env->read_size)
				 -1)
			   & PAGE_MASK)) {
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919
                                p_buff= p_buff+privptr->p_env->read_size;
                        }
                        else {
                                p_buff=
				(void *)((unsigned long)
1920
					(p_buff+2*(privptr->p_env->read_size)-1)
L
Linus Torvalds 已提交
1921 1922 1923 1924 1925 1926 1927
					 & PAGE_MASK) ;
                        }
                }   /* for read_buffers   */
          }         /* read_size < PAGE_SIZE  */
          else {  /* read Size >= PAGE_SIZE  */
                for (i=0 ; i< privptr->p_env->read_buffers ; i++) {
                        p_buff = (void *)__get_free_pages(__GFP_DMA,
1928 1929
				(int)pages_to_order_of_mag(
					privptr->p_buff_pages_perread));
L
Linus Torvalds 已提交
1930 1931
                        if (p_buff==NULL) {
                                free_pages((unsigned long)privptr->p_buff_ccw,
1932 1933
					(int)pages_to_order_of_mag(privptr->
					p_buff_ccw_num));
L
Linus Torvalds 已提交
1934 1935 1936
				/* free the write pages  */
	                        p_buf=privptr->p_buff_write;
                                while (p_buf!=NULL) {
1937 1938 1939 1940
					free_pages(
					    (unsigned long)p_buf->p_buffer,
					    (int)pages_to_order_of_mag(
					    privptr->p_buff_pages_perwrite));
L
Linus Torvalds 已提交
1941 1942 1943 1944 1945
                                        p_buf=p_buf->next;
                                }
				/* free any read pages already alloc  */
	                        p_buf=privptr->p_buff_read;
                                while (p_buf!=NULL) {
1946 1947 1948 1949
					free_pages(
					    (unsigned long)p_buf->p_buffer,
					    (int)pages_to_order_of_mag(
					     privptr->p_buff_pages_perread));
L
Linus Torvalds 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
                                        p_buf=p_buf->next;
                                }
                                privptr->p_buff_ccw=NULL;
                                privptr->p_buff_write=NULL;
                                return -ENOMEM;
                        }
                        memset(p_buff, 0x00, privptr->p_env->read_size);
                        p_buf        = p_free_chain;
                        privptr->p_buff_read = p_buf;
                        p_free_chain = p_buf->next;

                        if (p_last_CCWB==NULL) {
                                p_buf->next=NULL;
                                real_TIC_address=0;
                                p_last_CCWB=p_buf;
                        }
                        else {
                                p_buf->next=p_first_CCWB;
                                real_TIC_address=
					(addr_t)__pa(
						&p_first_CCWB -> read );
                        }

                        p_first_CCWB=p_buf;
				/* save buff address */
                        p_buf->p_buffer=(struct clawbuf *)p_buff;
                        /*  initialize read command */
                        p_buf-> read.cmd_code = CCW_CLAW_CMD_READ;
                        p_buf-> read.cda = (__u32)__pa(p_buff);
                        p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> read.count       = privptr->p_env->read_size;

                        /*  initialize read_h command */
                        p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER;
                        p_buf-> read_h.cda =
				(__u32)__pa(&(p_buf->header));
                        p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> read_h.count      = sizeof(struct clawh);

                        /*  initialize Signal command */
                        p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD;
                        p_buf-> signal.cda =
				(__u32)__pa(&(pClawH->flag));
                        p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC;
                        p_buf-> signal.count     = 1;

                        /*  initialize r_TIC_1 command */
                        p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC;
                        p_buf-> r_TIC_1.cda = (__u32)real_TIC_address;
                        p_buf-> r_TIC_1.flags = 0;
                        p_buf-> r_TIC_1.count      = 0;

                        /*  initialize r_read_FF command */
                        p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF;
                        p_buf-> r_read_FF.cda =
				(__u32)__pa(&(pClawH->flag));
                        p_buf-> r_read_FF.flags =
				CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI;
                        p_buf-> r_read_FF.count    = 1;

                        /*    initialize r_TIC_2          */
                        memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1,
				sizeof(struct ccw1));

                        /*     initialize Header     */
                        p_buf->header.length=0xffff;
                        p_buf->header.opcode=0xff;
                        p_buf->header.flag=CLAW_PENDING;

                }    /* For read_buffers   */
          }     /*  read_size >= PAGE_SIZE   */
        }       /*  pBuffread = NULL */
        add_claw_reads( dev  ,p_first_CCWB , p_last_CCWB);
	privptr->buffs_alloc = 1;
2024

L
Linus Torvalds 已提交
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
        return 0;
}    /*    end of init_ccw_bk */

/*-------------------------------------------------------------------*
*                                                                    *
*       probe_error                                                  *
*                                                                    *
*--------------------------------------------------------------------*/

static void
probe_error( struct ccwgroup_device *cgdev)
{
2037
	struct claw_privbk *privptr;
2038 2039

	CLAW_DBF_TEXT(4, trace, "proberr");
2040
	privptr = dev_get_drvdata(&cgdev->dev);
2041
	if (privptr != NULL) {
2042
		dev_set_drvdata(&cgdev->dev, NULL);
J
Jesper Juhl 已提交
2043
		kfree(privptr->p_env);
2044 2045 2046
		kfree(privptr->p_mtc_envelope);
		kfree(privptr);
	}
L
Linus Torvalds 已提交
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
}    /*    probe_error    */

/*-------------------------------------------------------------------*
*    claw_process_control                                            *
*                                                                    *
*                                                                    *
*--------------------------------------------------------------------*/

static int
claw_process_control( struct net_device *dev, struct ccwbk * p_ccw)
{

        struct clawbuf *p_buf;
        struct clawctl  ctlbk;
        struct clawctl *p_ctlbk;
        char    temp_host_name[8];
        char    temp_ws_name[8];
        struct claw_privbk *privptr;
        struct claw_env *p_env;
        struct sysval *p_sysval;
        struct conncmd *p_connect=NULL;
        int rc;
        struct chbk *p_ch = NULL;
2070 2071
	struct device *tdev;
	CLAW_DBF_TEXT(2, setup, "clw_cntl");
L
Linus Torvalds 已提交
2072 2073
        udelay(1000);  /* Wait a ms for the control packets to
			*catch up to each other */
2074
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2075
        p_env=privptr->p_env;
2076
	tdev = &privptr->channel[READ_CHANNEL].cdev->dev;
L
Linus Torvalds 已提交
2077 2078
	memcpy( &temp_host_name, p_env->host_name, 8);
        memcpy( &temp_ws_name, p_env->adapter_name , 8);
2079
	dev_info(tdev, "%s: CLAW device %.8s: "
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
		"Received Control Packet\n",
		dev->name, temp_ws_name);
        if (privptr->release_pend==1) {
                return 0;
        }
        p_buf=p_ccw->p_buffer;
        p_ctlbk=&ctlbk;
	if (p_env->packing == DO_PACKED) { /* packing in progress?*/
		memcpy(p_ctlbk, &p_buf->buffer[4], sizeof(struct clawctl));
	} else {
		memcpy(p_ctlbk, p_buf, sizeof(struct clawctl));
	}
        switch (p_ctlbk->command)
        {
2094 2095 2096 2097
	case SYSTEM_VALIDATE_REQUEST:
		if (p_ctlbk->version != CLAW_VERSION_ID) {
			claw_snd_sys_validate_rsp(dev, p_ctlbk,
				CLAW_RC_WRONG_VERSION);
2098 2099 2100
			dev_warn(tdev, "The communication peer of %s"
				" uses an incorrect API version %d\n",
				dev->name, p_ctlbk->version);
2101 2102
		}
		p_sysval = (struct sysval *)&(p_ctlbk->data);
2103 2104 2105 2106 2107 2108 2109 2110
		dev_info(tdev, "%s: Recv Sys Validate Request: "
			"Vers=%d,link_id=%d,Corr=%d,WS name=%.8s,"
			"Host name=%.8s\n",
			dev->name, p_ctlbk->version,
			p_ctlbk->linkid,
			p_ctlbk->correlator,
			p_sysval->WS_name,
			p_sysval->host_name);
2111 2112 2113 2114 2115 2116
		if (memcmp(temp_host_name, p_sysval->host_name, 8)) {
			claw_snd_sys_validate_rsp(dev, p_ctlbk,
				CLAW_RC_NAME_MISMATCH);
			CLAW_DBF_TEXT(2, setup, "HSTBAD");
			CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->host_name);
			CLAW_DBF_TEXT_(2, setup, "%s", temp_host_name);
2117 2118 2119
			dev_warn(tdev,
				"Host name %s for %s does not match the"
				" remote adapter name %s\n",
2120
				p_sysval->host_name,
2121
				dev->name,
2122 2123 2124 2125 2126 2127 2128 2129
				temp_host_name);
		}
		if (memcmp(temp_ws_name, p_sysval->WS_name, 8)) {
			claw_snd_sys_validate_rsp(dev, p_ctlbk,
				CLAW_RC_NAME_MISMATCH);
			CLAW_DBF_TEXT(2, setup, "WSNBAD");
			CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->WS_name);
			CLAW_DBF_TEXT_(2, setup, "%s", temp_ws_name);
2130 2131 2132 2133 2134
			dev_warn(tdev, "Adapter name %s for %s does not match"
				" the remote host name %s\n",
				p_sysval->WS_name,
				dev->name,
				temp_ws_name);
2135 2136 2137 2138 2139
		}
		if ((p_sysval->write_frame_size < p_env->write_size) &&
		    (p_env->packing == 0)) {
			claw_snd_sys_validate_rsp(dev, p_ctlbk,
				CLAW_RC_HOST_RCV_TOO_SMALL);
2140 2141 2142
			dev_warn(tdev,
				"The local write buffer is smaller than the"
				" remote read buffer\n");
2143 2144 2145 2146 2147 2148
			CLAW_DBF_TEXT(2, setup, "wrtszbad");
		}
		if ((p_sysval->read_frame_size < p_env->read_size) &&
		    (p_env->packing == 0)) {
			claw_snd_sys_validate_rsp(dev, p_ctlbk,
				CLAW_RC_HOST_RCV_TOO_SMALL);
2149 2150 2151
			dev_warn(tdev,
				"The local read buffer is smaller than the"
				" remote write buffer\n");
2152 2153 2154
			CLAW_DBF_TEXT(2, setup, "rdsizbad");
		}
		claw_snd_sys_validate_rsp(dev, p_ctlbk, 0);
2155 2156 2157 2158 2159 2160 2161
		dev_info(tdev,
			"CLAW device %.8s: System validate"
			" completed.\n", temp_ws_name);
		dev_info(tdev,
			"%s: sys Validate Rsize:%d Wsize:%d\n",
			dev->name, p_sysval->read_frame_size,
			p_sysval->write_frame_size);
2162 2163 2164 2165 2166 2167 2168
		privptr->system_validate_comp = 1;
		if (strncmp(p_env->api_type, WS_APPL_NAME_PACKED, 6) == 0)
			p_env->packing = PACKING_ASK;
		claw_strt_conn_req(dev);
		break;
	case SYSTEM_VALIDATE_RESPONSE:
		p_sysval = (struct sysval *)&(p_ctlbk->data);
2169 2170 2171 2172
		dev_info(tdev,
			"Settings for %s validated (version=%d, "
			"remote device=%d, rc=%d, adapter name=%.8s, "
			"host name=%.8s)\n",
2173 2174 2175 2176 2177 2178 2179 2180
			dev->name,
			p_ctlbk->version,
			p_ctlbk->correlator,
			p_ctlbk->rc,
			p_sysval->WS_name,
			p_sysval->host_name);
		switch (p_ctlbk->rc) {
		case 0:
2181 2182 2183
			dev_info(tdev, "%s: CLAW device "
				"%.8s: System validate completed.\n",
				dev->name, temp_ws_name);
2184 2185 2186 2187 2188
			if (privptr->system_validate_comp == 0)
				claw_strt_conn_req(dev);
			privptr->system_validate_comp = 1;
			break;
		case CLAW_RC_NAME_MISMATCH:
2189 2190 2191
			dev_warn(tdev, "Validating %s failed because of"
				" a host or adapter name mismatch\n",
				dev->name);
2192 2193
			break;
		case CLAW_RC_WRONG_VERSION:
2194 2195
			dev_warn(tdev, "Validating %s failed because of a"
				" version conflict\n",
2196 2197 2198
				dev->name);
			break;
		case CLAW_RC_HOST_RCV_TOO_SMALL:
2199 2200
			dev_warn(tdev, "Validating %s failed because of a"
				" frame size conflict\n",
2201 2202 2203
				dev->name);
			break;
		default:
2204 2205 2206
			dev_warn(tdev, "The communication peer of %s rejected"
				" the connection\n",
				 dev->name);
2207 2208 2209
			break;
		}
		break;
L
Linus Torvalds 已提交
2210

2211 2212
	case CONNECTION_REQUEST:
		p_connect = (struct conncmd *)&(p_ctlbk->data);
2213
		dev_info(tdev, "%s: Recv Conn Req: Vers=%d,link_id=%d,"
2214 2215 2216 2217 2218 2219 2220 2221 2222
			"Corr=%d,HOST appl=%.8s,WS appl=%.8s\n",
			dev->name,
			p_ctlbk->version,
			p_ctlbk->linkid,
			p_ctlbk->correlator,
			p_connect->host_name,
			p_connect->WS_name);
		if (privptr->active_link_ID != 0) {
			claw_snd_disc(dev, p_ctlbk);
2223 2224
			dev_info(tdev, "%s rejected a connection request"
				" because it is already active\n",
2225 2226 2227 2228
				dev->name);
		}
		if (p_ctlbk->linkid != 1) {
			claw_snd_disc(dev, p_ctlbk);
2229 2230
			dev_info(tdev, "%s rejected a request to open multiple"
				" connections\n",
2231 2232 2233 2234 2235
				dev->name);
		}
		rc = find_link(dev, p_connect->host_name, p_connect->WS_name);
		if (rc != 0) {
			claw_snd_disc(dev, p_ctlbk);
2236 2237
			dev_info(tdev, "%s rejected a connection request"
				" because of a type mismatch\n",
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
				dev->name);
		}
		claw_send_control(dev,
			CONNECTION_CONFIRM, p_ctlbk->linkid,
			p_ctlbk->correlator,
			0, p_connect->host_name,
			p_connect->WS_name);
		if (p_env->packing == PACKING_ASK) {
			p_env->packing = PACK_SEND;
			claw_snd_conn_req(dev, 0);
		}
2249
		dev_info(tdev, "%s: CLAW device %.8s: Connection "
2250 2251 2252 2253
			"completed link_id=%d.\n",
			dev->name, temp_ws_name,
			p_ctlbk->linkid);
			privptr->active_link_ID = p_ctlbk->linkid;
2254
			p_ch = &privptr->channel[WRITE_CHANNEL];
2255 2256 2257 2258
			wake_up(&p_ch->wait);  /* wake up claw_open ( WRITE) */
		break;
	case CONNECTION_RESPONSE:
		p_connect = (struct conncmd *)&(p_ctlbk->data);
2259
		dev_info(tdev, "%s: Recv Conn Resp: Vers=%d,link_id=%d,"
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
			"Corr=%d,RC=%d,Host appl=%.8s, WS appl=%.8s\n",
			dev->name,
			p_ctlbk->version,
			p_ctlbk->linkid,
			p_ctlbk->correlator,
			p_ctlbk->rc,
			p_connect->host_name,
			p_connect->WS_name);

		if (p_ctlbk->rc != 0) {
2270 2271 2272
			dev_warn(tdev, "The communication peer of %s rejected"
				" a connection request\n",
				dev->name);
2273 2274 2275 2276 2277 2278
			return 1;
		}
		rc = find_link(dev,
			p_connect->host_name, p_connect->WS_name);
		if (rc != 0) {
			claw_snd_disc(dev, p_ctlbk);
2279 2280 2281
			dev_warn(tdev, "The communication peer of %s"
				" rejected a connection "
				"request because of a type mismatch\n",
2282 2283 2284 2285 2286 2287 2288
				 dev->name);
		}
		/* should be until CONNECTION_CONFIRM */
		privptr->active_link_ID = -(p_ctlbk->linkid);
		break;
	case CONNECTION_CONFIRM:
		p_connect = (struct conncmd *)&(p_ctlbk->data);
2289 2290
		dev_info(tdev,
			"%s: Recv Conn Confirm:Vers=%d,link_id=%d,"
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
			"Corr=%d,Host appl=%.8s,WS appl=%.8s\n",
			dev->name,
			p_ctlbk->version,
			p_ctlbk->linkid,
			p_ctlbk->correlator,
			p_connect->host_name,
			p_connect->WS_name);
		if (p_ctlbk->linkid == -(privptr->active_link_ID)) {
			privptr->active_link_ID = p_ctlbk->linkid;
			if (p_env->packing > PACKING_ASK) {
2301 2302
				dev_info(tdev,
				"%s: Confirmed Now packing\n", dev->name);
L
Linus Torvalds 已提交
2303 2304
				p_env->packing = DO_PACKED;
			}
2305
			p_ch = &privptr->channel[WRITE_CHANNEL];
2306 2307
			wake_up(&p_ch->wait);
		} else {
2308 2309
			dev_warn(tdev, "Activating %s failed because of"
				" an incorrect link ID=%d\n",
2310 2311 2312 2313 2314
				dev->name, p_ctlbk->linkid);
			claw_snd_disc(dev, p_ctlbk);
		}
		break;
	case DISCONNECT:
2315
		dev_info(tdev, "%s: Disconnect: "
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
			"Vers=%d,link_id=%d,Corr=%d\n",
			dev->name, p_ctlbk->version,
			p_ctlbk->linkid, p_ctlbk->correlator);
		if ((p_ctlbk->linkid == 2) &&
		    (p_env->packing == PACK_SEND)) {
			privptr->active_link_ID = 1;
			p_env->packing = DO_PACKED;
		} else
			privptr->active_link_ID = 0;
		break;
	case CLAW_ERROR:
2327
		dev_warn(tdev, "The communication peer of %s failed\n",
2328 2329 2330
			dev->name);
		break;
	default:
2331 2332 2333
		dev_warn(tdev, "The communication peer of %s sent"
			" an unknown command code\n",
			dev->name);
2334
		break;
L
Linus Torvalds 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
        }

        return 0;
}   /*    end of claw_process_control    */


/*-------------------------------------------------------------------*
*               claw_send_control                                    *
*                                                                    *
*--------------------------------------------------------------------*/

static int
claw_send_control(struct net_device *dev, __u8 type, __u8 link,
	 __u8 correlator, __u8 rc, char *local_name, char *remote_name)
{
        struct claw_privbk 		*privptr;
        struct clawctl                  *p_ctl;
        struct sysval                   *p_sysval;
        struct conncmd                  *p_connect;
        struct sk_buff 			*skb;

2356
	CLAW_DBF_TEXT(2, setup, "sndcntl");
2357
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
        p_ctl=(struct clawctl *)&privptr->ctl_bk;

        p_ctl->command=type;
        p_ctl->version=CLAW_VERSION_ID;
        p_ctl->linkid=link;
        p_ctl->correlator=correlator;
        p_ctl->rc=rc;

        p_sysval=(struct sysval *)&p_ctl->data;
        p_connect=(struct conncmd *)&p_ctl->data;

        switch (p_ctl->command) {
                case SYSTEM_VALIDATE_REQUEST:
                case SYSTEM_VALIDATE_RESPONSE:
                        memcpy(&p_sysval->host_name, local_name, 8);
                        memcpy(&p_sysval->WS_name, remote_name, 8);
			if (privptr->p_env->packing > 0) {
2375 2376
				p_sysval->read_frame_size = DEF_PACK_BUFSIZE;
				p_sysval->write_frame_size = DEF_PACK_BUFSIZE;
L
Linus Torvalds 已提交
2377
			} else {
2378
				/* how big is the biggest group of packets */
2379 2380 2381 2382
			   p_sysval->read_frame_size =
				privptr->p_env->read_size;
			   p_sysval->write_frame_size =
				privptr->p_env->write_size;
L
Linus Torvalds 已提交
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
			}
                        memset(&p_sysval->reserved, 0x00, 4);
                        break;
                case CONNECTION_REQUEST:
                case CONNECTION_RESPONSE:
                case CONNECTION_CONFIRM:
                case DISCONNECT:
                        memcpy(&p_sysval->host_name, local_name, 8);
                        memcpy(&p_sysval->WS_name, remote_name, 8);
			if (privptr->p_env->packing > 0) {
			/* How big is the biggest packet */
				p_connect->reserved1[0]=CLAW_FRAME_SIZE;
                        	p_connect->reserved1[1]=CLAW_FRAME_SIZE;
			} else {
	                        memset(&p_connect->reserved1, 0x00, 4);
        	                memset(&p_connect->reserved2, 0x00, 4);
			}
                        break;
                default:
                        break;
        }

        /*      write Control Record to the device                   */


        skb = dev_alloc_skb(sizeof(struct clawctl));
        if (!skb) {
                return -ENOMEM;
        }
	memcpy(skb_put(skb, sizeof(struct clawctl)),
		p_ctl, sizeof(struct clawctl));
	if (privptr->p_env->packing >= PACK_SEND)
		claw_hw_tx(skb, dev, 1);
	else
        	claw_hw_tx(skb, dev, 0);
        return 0;
}  /*   end of claw_send_control  */

/*-------------------------------------------------------------------*
*               claw_snd_conn_req                                    *
*                                                                    *
*--------------------------------------------------------------------*/
static int
claw_snd_conn_req(struct net_device *dev, __u8 link)
{
        int                rc;
2429
	struct claw_privbk *privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2430 2431
        struct clawctl 	   *p_ctl;

2432
	CLAW_DBF_TEXT(2, setup, "snd_conn");
L
Linus Torvalds 已提交
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
	rc = 1;
        p_ctl=(struct clawctl *)&privptr->ctl_bk;
	p_ctl->linkid = link;
        if ( privptr->system_validate_comp==0x00 ) {
                return rc;
        }
	if (privptr->p_env->packing == PACKING_ASK )
		rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
        		WS_APPL_NAME_PACKED, WS_APPL_NAME_PACKED);
	if (privptr->p_env->packing == PACK_SEND)  {
		rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
        		WS_APPL_NAME_IP_NAME, WS_APPL_NAME_IP_NAME);
	}
	if (privptr->p_env->packing == 0)
        	rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0,
       			HOST_APPL_NAME, privptr->p_env->api_type);
        return rc;

}  /*  end of claw_snd_conn_req */


/*-------------------------------------------------------------------*
*               claw_snd_disc                                        *
*                                                                    *
*--------------------------------------------------------------------*/

static int
claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl)
{
        int rc;
        struct conncmd *  p_connect;

2465
	CLAW_DBF_TEXT(2, setup, "snd_dsc");
L
Linus Torvalds 已提交
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
        p_connect=(struct conncmd *)&p_ctl->data;

        rc=claw_send_control(dev, DISCONNECT, p_ctl->linkid,
		p_ctl->correlator, 0,
                p_connect->host_name, p_connect->WS_name);
        return rc;
}     /*   end of claw_snd_disc    */


/*-------------------------------------------------------------------*
*               claw_snd_sys_validate_rsp                            *
*                                                                    *
*--------------------------------------------------------------------*/

static int
claw_snd_sys_validate_rsp(struct net_device *dev,
	struct clawctl *p_ctl, __u32 return_code)
{
        struct claw_env *  p_env;
        struct claw_privbk *privptr;
        int    rc;

2488
	CLAW_DBF_TEXT(2, setup, "chkresp");
2489
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
        p_env=privptr->p_env;
        rc=claw_send_control(dev, SYSTEM_VALIDATE_RESPONSE,
		p_ctl->linkid,
		p_ctl->correlator,
                return_code,
		p_env->host_name,
		p_env->adapter_name  );
        return rc;
}     /*    end of claw_snd_sys_validate_rsp    */

/*-------------------------------------------------------------------*
*               claw_strt_conn_req                                   *
*                                                                    *
*--------------------------------------------------------------------*/

static int
claw_strt_conn_req(struct net_device *dev )
{
        int rc;

2510
	CLAW_DBF_TEXT(2, setup, "conn_req");
L
Linus Torvalds 已提交
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
        rc=claw_snd_conn_req(dev, 1);
        return rc;
}    /*   end of claw_strt_conn_req   */



/*-------------------------------------------------------------------*
 *   claw_stats                                                      *
 *-------------------------------------------------------------------*/

static struct
net_device_stats *claw_stats(struct net_device *dev)
{
        struct claw_privbk *privptr;
2525 2526

	CLAW_DBF_TEXT(4, trace, "stats");
2527
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
        return &privptr->stats;
}     /*   end of claw_stats   */


/*-------------------------------------------------------------------*
*       unpack_read                                                  *
*                                                                    *
*--------------------------------------------------------------------*/
static void
unpack_read(struct net_device *dev )
{
        struct sk_buff *skb;
        struct claw_privbk *privptr;
	struct claw_env    *p_env;
        struct ccwbk 	*p_this_ccw;
        struct ccwbk 	*p_first_ccw;
        struct ccwbk 	*p_last_ccw;
	struct clawph 	*p_packh;
	void		*p_packd;
	struct clawctl 	*p_ctlrec=NULL;
2548
	struct device	*p_dev;
L
Linus Torvalds 已提交
2549 2550 2551 2552 2553 2554 2555 2556 2557

        __u32	len_of_data;
	__u32	pack_off;
        __u8	link_num;
        __u8 	mtc_this_frm=0;
        __u32	bytes_to_mov;
        int	i=0;
	int     p=0;

2558
	CLAW_DBF_TEXT(4, trace, "unpkread");
L
Linus Torvalds 已提交
2559 2560 2561 2562
        p_first_ccw=NULL;
        p_last_ccw=NULL;
	p_packh=NULL;
	p_packd=NULL;
2563
	privptr = dev->ml_priv;
2564

2565
	p_dev = &privptr->channel[READ_CHANNEL].cdev->dev;
L
Linus Torvalds 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
	p_env = privptr->p_env;
        p_this_ccw=privptr->p_read_active_first;
	while (p_this_ccw!=NULL && p_this_ccw->header.flag!=CLAW_PENDING) {
		pack_off = 0;
		p = 0;
		p_this_ccw->header.flag=CLAW_PENDING;
		privptr->p_read_active_first=p_this_ccw->next;
                p_this_ccw->next=NULL;
		p_packh = (struct clawph *)p_this_ccw->p_buffer;
		if ((p_env->packing == PACK_SEND) &&
		    (p_packh->len == 32)           &&
		    (p_packh->link_num == 0)) {   /* is it a packed ctl rec? */
			p_packh++;  /* peek past pack header */
			p_ctlrec = (struct clawctl *)p_packh;
			p_packh--;  /* un peek */
			if ((p_ctlrec->command == CONNECTION_RESPONSE) ||
		            (p_ctlrec->command == CONNECTION_CONFIRM))
				p_env->packing = DO_PACKED;
		}
		if (p_env->packing == DO_PACKED)
			link_num=p_packh->link_num;
		else
	                link_num=p_this_ccw->header.opcode / 8;
                if ((p_this_ccw->header.opcode & MORE_to_COME_FLAG)!=0) {
                        mtc_this_frm=1;
                        if (p_this_ccw->header.length!=
				privptr->p_env->read_size ) {
2593 2594 2595 2596
				dev_warn(p_dev,
					"The communication peer of %s"
					" sent a faulty"
					" frame of length %02x\n",
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
                                        dev->name, p_this_ccw->header.length);
                        }
                }

                if (privptr->mtc_skipping) {
                        /*
                        *   We're in the mode of skipping past a
			*   multi-frame message
                        *   that we can't process for some reason or other.
                        *   The first frame without the More-To-Come flag is
			*   the last frame of the skipped message.
                        */
                        /*  in case of More-To-Come not set in this frame */
                        if (mtc_this_frm==0) {
                                privptr->mtc_skipping=0; /* Ok, the end */
                                privptr->mtc_logical_link=-1;
                        }
                        goto NextFrame;
                }

                if (link_num==0) {
                        claw_process_control(dev, p_this_ccw);
2619
			CLAW_DBF_TEXT(4, trace, "UnpkCntl");
L
Linus Torvalds 已提交
2620 2621 2622 2623 2624 2625 2626 2627
                        goto NextFrame;
                }
unpack_next:
		if (p_env->packing == DO_PACKED) {
			if (pack_off > p_env->read_size)
				goto NextFrame;
			p_packd = p_this_ccw->p_buffer+pack_off;
			p_packh = (struct clawph *) p_packd;
2628
			if ((p_packh->len == 0) || /* done with this frame? */
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
			    (p_packh->flag != 0))
				goto NextFrame;
			bytes_to_mov = p_packh->len;
			pack_off += bytes_to_mov+sizeof(struct clawph);
			p++;
		} else {
                	bytes_to_mov=p_this_ccw->header.length;
		}
                if (privptr->mtc_logical_link<0) {

                /*
                *  if More-To-Come is set in this frame then we don't know
                *  length of entire message, and hence have to allocate
		*  large buffer   */

                /*      We are starting a new envelope  */
                privptr->mtc_offset=0;
                        privptr->mtc_logical_link=link_num;
                }

                if (bytes_to_mov > (MAX_ENVELOPE_SIZE- privptr->mtc_offset) ) {
                        /*      error     */
                        privptr->stats.rx_frame_errors++;
                        goto NextFrame;
                }
		if (p_env->packing == DO_PACKED) {
			memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
				p_packd+sizeof(struct clawph), bytes_to_mov);

		} else	{
                	memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset,
                        	p_this_ccw->p_buffer, bytes_to_mov);
		}
                if (mtc_this_frm==0) {
                        len_of_data=privptr->mtc_offset+bytes_to_mov;
                        skb=dev_alloc_skb(len_of_data);
                        if (skb) {
                                memcpy(skb_put(skb,len_of_data),
					privptr->p_mtc_envelope,
					len_of_data);
                                skb->dev=dev;
2670
				skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675 2676 2677
                                skb->protocol=htons(ETH_P_IP);
                                skb->ip_summed=CHECKSUM_UNNECESSARY;
                                privptr->stats.rx_packets++;
				privptr->stats.rx_bytes+=len_of_data;
                                netif_rx(skb);
                        }
                        else {
2678 2679
				dev_info(p_dev, "Allocating a buffer for"
					" incoming data failed\n");
L
Linus Torvalds 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
                                privptr->stats.rx_dropped++;
                        }
                        privptr->mtc_offset=0;
                        privptr->mtc_logical_link=-1;
                }
                else {
                        privptr->mtc_offset+=bytes_to_mov;
                }
		if (p_env->packing == DO_PACKED)
			goto unpack_next;
NextFrame:
                /*
                *   Remove ThisCCWblock from active read queue, and add it
                *   to queue of free blocks to be reused.
                */
                i++;
                p_this_ccw->header.length=0xffff;
                p_this_ccw->header.opcode=0xff;
                /*
                *       add this one to the free queue for later reuse
                */
                if (p_first_ccw==NULL) {
                        p_first_ccw = p_this_ccw;
                }
                else {
                        p_last_ccw->next = p_this_ccw;
                }
                p_last_ccw = p_this_ccw;
                /*
                *       chain to next block on active read queue
                */
                p_this_ccw = privptr->p_read_active_first;
2712
		CLAW_DBF_TEXT_(4, trace, "rxpkt %d", p);
L
Linus Torvalds 已提交
2713 2714 2715 2716
        } /* end of while */

        /*      check validity                  */

2717
	CLAW_DBF_TEXT_(4, trace, "rxfrm %d", i);
L
Linus Torvalds 已提交
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
        add_claw_reads(dev, p_first_ccw, p_last_ccw);
        claw_strt_read(dev, LOCK_YES);
        return;
}     /*  end of unpack_read   */

/*-------------------------------------------------------------------*
*       claw_strt_read                                               *
*                                                                    *
*--------------------------------------------------------------------*/
static void
claw_strt_read (struct net_device *dev, int lock )
{
        int        rc = 0;
        __u32      parm;
        unsigned long  saveflags = 0;
2733
	struct claw_privbk *privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2734 2735 2736
        struct ccwbk*p_ccwbk;
        struct chbk *p_ch;
        struct clawh *p_clawh;
2737
	p_ch = &privptr->channel[READ_CHANNEL];
L
Linus Torvalds 已提交
2738

2739
	CLAW_DBF_TEXT(4, trace, "StRdNter");
L
Linus Torvalds 已提交
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
        p_clawh=(struct clawh *)privptr->p_claw_signal_blk;
        p_clawh->flag=CLAW_IDLE;    /* 0x00 */

        if ((privptr->p_write_active_first!=NULL &&
             privptr->p_write_active_first->header.flag!=CLAW_PENDING) ||
            (privptr->p_read_active_first!=NULL &&
             privptr->p_read_active_first->header.flag!=CLAW_PENDING )) {
                p_clawh->flag=CLAW_BUSY;    /* 0xff */
        }
        if (lock==LOCK_YES) {
                spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags);
        }
        if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
2753
		CLAW_DBF_TEXT(4, trace, "HotRead");
L
Linus Torvalds 已提交
2754 2755 2756 2757 2758 2759 2760 2761 2762
                p_ccwbk=privptr->p_read_active_first;
                parm = (unsigned long) p_ch;
                rc = ccw_device_start (p_ch->cdev, &p_ccwbk->read, parm,
				       0xff, 0);
                if (rc != 0) {
                        ccw_check_return_code(p_ch->cdev, rc);
                }
        }
	else {
2763
		CLAW_DBF_TEXT(2, trace, "ReadAct");
L
Linus Torvalds 已提交
2764 2765 2766 2767 2768
	}

        if (lock==LOCK_YES) {
                spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags);
        }
2769
	CLAW_DBF_TEXT(4, trace, "StRdExit");
L
Linus Torvalds 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
        return;
}       /*    end of claw_strt_read    */

/*-------------------------------------------------------------------*
*       claw_strt_out_IO                                             *
*                                                                    *
*--------------------------------------------------------------------*/

static void
claw_strt_out_IO( struct net_device *dev )
{
        int             	rc = 0;
        unsigned long   	parm;
        struct claw_privbk 	*privptr;
        struct chbk     	*p_ch;
        struct ccwbk   	*p_first_ccw;

	if (!dev) {
		return;
	}
2790
	privptr = (struct claw_privbk *)dev->ml_priv;
2791
	p_ch = &privptr->channel[WRITE_CHANNEL];
L
Linus Torvalds 已提交
2792

2793
	CLAW_DBF_TEXT(4, trace, "strt_io");
L
Linus Torvalds 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802
        p_first_ccw=privptr->p_write_active_first;

        if (p_ch->claw_state == CLAW_STOP)
                return;
        if (p_first_ccw == NULL) {
                return;
        }
        if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) {
                parm = (unsigned long) p_ch;
2803
		CLAW_DBF_TEXT(2, trace, "StWrtIO");
2804 2805
		rc = ccw_device_start(p_ch->cdev, &p_first_ccw->write, parm,
				      0xff, 0);
L
Linus Torvalds 已提交
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
                if (rc != 0) {
                        ccw_check_return_code(p_ch->cdev, rc);
                }
        }
        dev->trans_start = jiffies;
        return;
}       /*    end of claw_strt_out_IO    */

/*-------------------------------------------------------------------*
*       Free write buffers                                           *
*                                                                    *
*--------------------------------------------------------------------*/

static void
claw_free_wrt_buf( struct net_device *dev )
{

2823
	struct claw_privbk *privptr = (struct claw_privbk *)dev->ml_priv;
L
Linus Torvalds 已提交
2824 2825 2826 2827
        struct ccwbk*p_first_ccw;
	struct ccwbk*p_last_ccw;
	struct ccwbk*p_this_ccw;
	struct ccwbk*p_next_ccw;
2828 2829

	CLAW_DBF_TEXT(4, trace, "freewrtb");
L
Linus Torvalds 已提交
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
        /*  scan the write queue to free any completed write packets   */
        p_first_ccw=NULL;
        p_last_ccw=NULL;
        p_this_ccw=privptr->p_write_active_first;
        while ( (p_this_ccw!=NULL) && (p_this_ccw->header.flag!=CLAW_PENDING))
        {
                p_next_ccw = p_this_ccw->next;
                if (((p_next_ccw!=NULL) &&
		     (p_next_ccw->header.flag!=CLAW_PENDING)) ||
                    ((p_this_ccw == privptr->p_write_active_last) &&
                     (p_this_ccw->header.flag!=CLAW_PENDING))) {
                        /* The next CCW is OK or this is  */
			/* the last CCW...free it   @A1A  */
                        privptr->p_write_active_first=p_this_ccw->next;
			p_this_ccw->header.flag=CLAW_PENDING;
                        p_this_ccw->next=privptr->p_write_free_chain;
			privptr->p_write_free_chain=p_this_ccw;
                        ++privptr->write_free_count;
			privptr->stats.tx_bytes+= p_this_ccw->write.count;
			p_this_ccw=privptr->p_write_active_first;
                        privptr->stats.tx_packets++;
                }
                else {
			break;
                }
        }
        if (privptr->write_free_count!=0) {
                claw_clearbit_busy(TB_NOBUFFER,dev);
        }
        /*   whole chain removed?   */
        if (privptr->p_write_active_first==NULL) {
                privptr->p_write_active_last=NULL;
        }
2863
	CLAW_DBF_TEXT_(4, trace, "FWC=%d", privptr->write_free_count);
L
Linus Torvalds 已提交
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
        return;
}

/*-------------------------------------------------------------------*
*       claw free netdevice                                          *
*                                                                    *
*--------------------------------------------------------------------*/
static void
claw_free_netdevice(struct net_device * dev, int free_dev)
{
	struct claw_privbk *privptr;

2876
	CLAW_DBF_TEXT(2, setup, "free_dev");
L
Linus Torvalds 已提交
2877 2878
	if (!dev)
		return;
2879
	CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
2880
	privptr = dev->ml_priv;
L
Linus Torvalds 已提交
2881 2882 2883
	if (dev->flags & IFF_RUNNING)
		claw_release(dev);
	if (privptr) {
2884
		privptr->channel[READ_CHANNEL].ndev = NULL;  /* say it's free */
L
Linus Torvalds 已提交
2885
	}
2886
	dev->ml_priv = NULL;
L
Linus Torvalds 已提交
2887 2888 2889 2890 2891
#ifdef MODULE
	if (free_dev) {
		free_netdev(dev);
	}
#endif
2892
	CLAW_DBF_TEXT(2, setup, "free_ok");
L
Linus Torvalds 已提交
2893 2894 2895 2896 2897 2898 2899
}

/**
 * Claw init netdevice
 * Initialize everything of the net device except the name and the
 * channel structs.
 */
F
Frank Blaschka 已提交
2900 2901 2902 2903 2904 2905 2906 2907
static const struct net_device_ops claw_netdev_ops = {
	.ndo_open		= claw_open,
	.ndo_stop		= claw_release,
	.ndo_get_stats		= claw_stats,
	.ndo_start_xmit		= claw_tx,
	.ndo_change_mtu		= claw_change_mtu,
};

L
Linus Torvalds 已提交
2908 2909 2910
static void
claw_init_netdevice(struct net_device * dev)
{
2911 2912
	CLAW_DBF_TEXT(2, setup, "init_dev");
	CLAW_DBF_TEXT_(2, setup, "%s", dev->name);
L
Linus Torvalds 已提交
2913 2914 2915 2916 2917 2918
	dev->mtu = CLAW_DEFAULT_MTU_SIZE;
	dev->hard_header_len = 0;
	dev->addr_len = 0;
	dev->type = ARPHRD_SLIP;
	dev->tx_queue_len = 1300;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
F
Frank Blaschka 已提交
2919
	dev->netdev_ops = &claw_netdev_ops;
2920
	CLAW_DBF_TEXT(2, setup, "initok");
L
Linus Torvalds 已提交
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
	return;
}

/**
 * Init a new channel in the privptr->channel[i].
 *
 * @param cdev  The ccw_device to be added.
 *
 * @return 0 on success, !0 on error.
 */
static int
add_channel(struct ccw_device *cdev,int i,struct claw_privbk *privptr)
{
	struct chbk *p_ch;
2935
	struct ccw_dev_id dev_id;
L
Linus Torvalds 已提交
2936

2937
	CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cdev->dev));
L
Linus Torvalds 已提交
2938 2939 2940
	privptr->channel[i].flag  = i+1;   /* Read is 1 Write is 2 */
	p_ch = &privptr->channel[i];
	p_ch->cdev = cdev;
2941
	snprintf(p_ch->id, CLAW_ID_SIZE, "cl-%s", dev_name(&cdev->dev));
2942 2943
	ccw_device_get_id(cdev, &dev_id);
	p_ch->devno = dev_id.devno;
2944
	if ((p_ch->irb = kzalloc(sizeof (struct irb),GFP_KERNEL)) == NULL) {
L
Linus Torvalds 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
		return -ENOMEM;
	}
	return 0;
}


/**
 *
 * Setup an interface.
 *
 * @param cgdev  Device to be setup.
 *
 * @returns 0 on success, !0 on failure.
 */
static int
claw_new_device(struct ccwgroup_device *cgdev)
{
	struct claw_privbk *privptr;
	struct claw_env *p_env;
	struct net_device *dev;
	int ret;
2966
	struct ccw_dev_id dev_id;
L
Linus Torvalds 已提交
2967

2968
	dev_info(&cgdev->dev, "add for %s\n",
2969
		 dev_name(&cgdev->cdev[READ_CHANNEL]->dev));
2970
	CLAW_DBF_TEXT(2, setup, "new_dev");
2971
	privptr = dev_get_drvdata(&cgdev->dev);
2972 2973
	dev_set_drvdata(&cgdev->cdev[READ_CHANNEL]->dev, privptr);
	dev_set_drvdata(&cgdev->cdev[WRITE_CHANNEL]->dev, privptr);
L
Linus Torvalds 已提交
2974 2975 2976
	if (!privptr)
		return -ENODEV;
	p_env = privptr->p_env;
2977 2978 2979 2980
	ccw_device_get_id(cgdev->cdev[READ_CHANNEL], &dev_id);
	p_env->devno[READ_CHANNEL] = dev_id.devno;
	ccw_device_get_id(cgdev->cdev[WRITE_CHANNEL], &dev_id);
	p_env->devno[WRITE_CHANNEL] = dev_id.devno;
L
Linus Torvalds 已提交
2981 2982 2983 2984
	ret = add_channel(cgdev->cdev[0],0,privptr);
	if (ret == 0)
		ret = add_channel(cgdev->cdev[1],1,privptr);
	if (ret != 0) {
2985 2986
		dev_warn(&cgdev->dev, "Creating a CLAW group device"
			" failed with error code %d\n", ret);
2987
		goto out;
L
Linus Torvalds 已提交
2988
	}
2989
	ret = ccw_device_set_online(cgdev->cdev[READ_CHANNEL]);
L
Linus Torvalds 已提交
2990
	if (ret != 0) {
2991 2992 2993
		dev_warn(&cgdev->dev,
			"Setting the read subchannel online"
			" failed with error code %d\n", ret);
L
Linus Torvalds 已提交
2994 2995
		goto out;
	}
2996
	ret = ccw_device_set_online(cgdev->cdev[WRITE_CHANNEL]);
L
Linus Torvalds 已提交
2997
	if (ret != 0) {
2998 2999 3000
		dev_warn(&cgdev->dev,
			"Setting the write subchannel online "
			"failed with error code %d\n", ret);
L
Linus Torvalds 已提交
3001 3002 3003 3004
		goto out;
	}
	dev = alloc_netdev(0,"claw%d",claw_init_netdevice);
	if (!dev) {
3005 3006
		dev_warn(&cgdev->dev,
			"Activating the CLAW device failed\n");
L
Linus Torvalds 已提交
3007 3008
		goto out;
	}
3009
	dev->ml_priv = privptr;
3010
	dev_set_drvdata(&cgdev->dev, privptr);
3011 3012
	dev_set_drvdata(&cgdev->cdev[READ_CHANNEL]->dev, privptr);
	dev_set_drvdata(&cgdev->cdev[WRITE_CHANNEL]->dev, privptr);
L
Linus Torvalds 已提交
3013 3014 3015 3016
	/* sysfs magic */
        SET_NETDEV_DEV(dev, &cgdev->dev);
	if (register_netdev(dev) != 0) {
		claw_free_netdevice(dev, 1);
3017
		CLAW_DBF_TEXT(2, trace, "regfail");
L
Linus Torvalds 已提交
3018 3019 3020 3021 3022 3023 3024 3025
		goto out;
	}
	dev->flags &=~IFF_RUNNING;
	if (privptr->buffs_alloc == 0) {
	        ret=init_ccw_bk(dev);
		if (ret !=0) {
			unregister_netdev(dev);
			claw_free_netdevice(dev,1);
3026
			CLAW_DBF_TEXT(2, trace, "ccwmem");
L
Linus Torvalds 已提交
3027 3028 3029
			goto out;
		}
	}
3030 3031
	privptr->channel[READ_CHANNEL].ndev = dev;
	privptr->channel[WRITE_CHANNEL].ndev = dev;
L
Linus Torvalds 已提交
3032 3033
	privptr->p_env->ndev = dev;

3034
	dev_info(&cgdev->dev, "%s:readsize=%d  writesize=%d "
L
Linus Torvalds 已提交
3035 3036 3037
		"readbuffer=%d writebuffer=%d read=0x%04x write=0x%04x\n",
                dev->name, p_env->read_size,
		p_env->write_size, p_env->read_buffers,
3038 3039
		p_env->write_buffers, p_env->devno[READ_CHANNEL],
		p_env->devno[WRITE_CHANNEL]);
3040
	dev_info(&cgdev->dev, "%s:host_name:%.8s, adapter_name "
L
Linus Torvalds 已提交
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
		":%.8s api_type: %.8s\n",
                dev->name, p_env->host_name,
		p_env->adapter_name , p_env->api_type);
	return 0;
out:
	ccw_device_set_offline(cgdev->cdev[1]);
	ccw_device_set_offline(cgdev->cdev[0]);
	return -ENODEV;
}

static void
claw_purge_skb_queue(struct sk_buff_head *q)
{
        struct sk_buff *skb;

3056
	CLAW_DBF_TEXT(4, trace, "purgque");
L
Linus Torvalds 已提交
3057 3058
        while ((skb = skb_dequeue(q))) {
                atomic_dec(&skb->users);
F
Frank Pavlic 已提交
3059
                dev_kfree_skb_any(skb);
L
Linus Torvalds 已提交
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
        }
}

/**
 * Shutdown an interface.
 *
 * @param cgdev  Device to be shut down.
 *
 * @returns 0 on success, !0 on failure.
 */
static int
claw_shutdown_device(struct ccwgroup_device *cgdev)
{
	struct claw_privbk *priv;
	struct net_device *ndev;
	int	ret;

3077
	CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3078
	priv = dev_get_drvdata(&cgdev->dev);
L
Linus Torvalds 已提交
3079 3080
	if (!priv)
		return -ENODEV;
3081
	ndev = priv->channel[READ_CHANNEL].ndev;
L
Linus Torvalds 已提交
3082 3083
	if (ndev) {
		/* Close the device */
3084
		dev_info(&cgdev->dev, "%s: shutting down\n",
3085
			ndev->name);
L
Linus Torvalds 已提交
3086 3087 3088 3089
		if (ndev->flags & IFF_RUNNING)
			ret = claw_release(ndev);
		ndev->flags &=~IFF_RUNNING;
		unregister_netdev(ndev);
3090
		ndev->ml_priv = NULL;  /* cgdev data, not ndev's to free */
L
Linus Torvalds 已提交
3091
		claw_free_netdevice(ndev, 1);
3092 3093
		priv->channel[READ_CHANNEL].ndev = NULL;
		priv->channel[WRITE_CHANNEL].ndev = NULL;
L
Linus Torvalds 已提交
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
		priv->p_env->ndev = NULL;
	}
	ccw_device_set_offline(cgdev->cdev[1]);
	ccw_device_set_offline(cgdev->cdev[0]);
	return 0;
}

static void
claw_remove_device(struct ccwgroup_device *cgdev)
{
	struct claw_privbk *priv;

3106
	BUG_ON(!cgdev);
3107
	CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev));
3108
	priv = dev_get_drvdata(&cgdev->dev);
3109
	BUG_ON(!priv);
3110
	dev_info(&cgdev->dev, " will be removed.\n");
L
Linus Torvalds 已提交
3111 3112 3113
	if (cgdev->state == CCWGROUP_ONLINE)
		claw_shutdown_device(cgdev);
	claw_remove_files(&cgdev->dev);
J
Jesper Juhl 已提交
3114 3115 3116 3117 3118 3119 3120 3121
	kfree(priv->p_mtc_envelope);
	priv->p_mtc_envelope=NULL;
	kfree(priv->p_env);
	priv->p_env=NULL;
	kfree(priv->channel[0].irb);
	priv->channel[0].irb=NULL;
	kfree(priv->channel[1].irb);
	priv->channel[1].irb=NULL;
L
Linus Torvalds 已提交
3122
	kfree(priv);
3123
	dev_set_drvdata(&cgdev->dev, NULL);
3124 3125
	dev_set_drvdata(&cgdev->cdev[READ_CHANNEL]->dev, NULL);
	dev_set_drvdata(&cgdev->cdev[WRITE_CHANNEL]->dev, NULL);
L
Linus Torvalds 已提交
3126
	put_device(&cgdev->dev);
3127 3128

	return;
L
Linus Torvalds 已提交
3129 3130 3131 3132 3133 3134 3135
}


/*
 * sysfs attributes
 */
static ssize_t
3136
claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3137 3138 3139 3140
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3141
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3142 3143 3144 3145 3146 3147 3148
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	return sprintf(buf, "%s\n",p_env->host_name);
}

static ssize_t
3149 3150
claw_hname_write(struct device *dev, struct device_attribute *attr,
	 const char *buf, size_t count)
L
Linus Torvalds 已提交
3151 3152 3153 3154
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3155
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3156 3157 3158 3159 3160 3161 3162 3163 3164
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	if (count > MAX_NAME_LEN+1)
		return -EINVAL;
	memset(p_env->host_name, 0x20, MAX_NAME_LEN);
	strncpy(p_env->host_name,buf, count);
	p_env->host_name[count-1] = 0x20;  /* clear extra 0x0a */
	p_env->host_name[MAX_NAME_LEN] = 0x00;
3165 3166
	CLAW_DBF_TEXT(2, setup, "HstnSet");
	CLAW_DBF_TEXT_(2, setup, "%s", p_env->host_name);
L
Linus Torvalds 已提交
3167 3168 3169 3170 3171 3172 3173

	return count;
}

static DEVICE_ATTR(host_name, 0644, claw_hname_show, claw_hname_write);

static ssize_t
3174
claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3175 3176 3177 3178
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3179
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3180 3181 3182
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
3183
	return sprintf(buf, "%s\n", p_env->adapter_name);
L
Linus Torvalds 已提交
3184 3185 3186
}

static ssize_t
3187 3188
claw_adname_write(struct device *dev, struct device_attribute *attr,
	 const char *buf, size_t count)
L
Linus Torvalds 已提交
3189 3190 3191 3192
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3193
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3194 3195 3196 3197 3198 3199 3200 3201 3202
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	if (count > MAX_NAME_LEN+1)
		return -EINVAL;
	memset(p_env->adapter_name, 0x20, MAX_NAME_LEN);
	strncpy(p_env->adapter_name,buf, count);
	p_env->adapter_name[count-1] = 0x20; /* clear extra 0x0a */
	p_env->adapter_name[MAX_NAME_LEN] = 0x00;
3203 3204
	CLAW_DBF_TEXT(2, setup, "AdnSet");
	CLAW_DBF_TEXT_(2, setup, "%s", p_env->adapter_name);
L
Linus Torvalds 已提交
3205 3206 3207 3208 3209 3210 3211

	return count;
}

static DEVICE_ATTR(adapter_name, 0644, claw_adname_show, claw_adname_write);

static ssize_t
3212
claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3213 3214 3215 3216
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3217
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3218 3219 3220 3221 3222 3223 3224 3225
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	return sprintf(buf, "%s\n",
		       p_env->api_type);
}

static ssize_t
3226 3227
claw_apname_write(struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
L
Linus Torvalds 已提交
3228 3229 3230 3231
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3232
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	if (count > MAX_NAME_LEN+1)
		return -EINVAL;
	memset(p_env->api_type, 0x20, MAX_NAME_LEN);
	strncpy(p_env->api_type,buf, count);
	p_env->api_type[count-1] = 0x20;  /* we get a loose 0x0a */
	p_env->api_type[MAX_NAME_LEN] = 0x00;
	if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) {
		p_env->read_size=DEF_PACK_BUFSIZE;
		p_env->write_size=DEF_PACK_BUFSIZE;
		p_env->packing=PACKING_ASK;
3246
		CLAW_DBF_TEXT(2, setup, "PACKING");
L
Linus Torvalds 已提交
3247 3248 3249 3250 3251
	}
	else {
		p_env->packing=0;
		p_env->read_size=CLAW_FRAME_SIZE;
		p_env->write_size=CLAW_FRAME_SIZE;
3252
		CLAW_DBF_TEXT(2, setup, "ApiSet");
L
Linus Torvalds 已提交
3253
	}
3254
	CLAW_DBF_TEXT_(2, setup, "%s", p_env->api_type);
L
Linus Torvalds 已提交
3255 3256 3257 3258 3259 3260
	return count;
}

static DEVICE_ATTR(api_type, 0644, claw_apname_show, claw_apname_write);

static ssize_t
3261
claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3262 3263 3264 3265
{
	struct claw_privbk *priv;
	struct claw_env * p_env;

3266
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3267 3268 3269 3270 3271 3272 3273
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	return sprintf(buf, "%d\n", p_env->write_buffers);
}

static ssize_t
3274 3275
claw_wbuff_write(struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
L
Linus Torvalds 已提交
3276 3277 3278 3279 3280
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;
	int nnn,max;

3281
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	sscanf(buf, "%i", &nnn);
	if (p_env->packing) {
		max = 64;
	}
	else {
		max = 512;
	}
	if ((nnn > max ) || (nnn < 2))
		return -EINVAL;
	p_env->write_buffers = nnn;
3295 3296
	CLAW_DBF_TEXT(2, setup, "Wbufset");
	CLAW_DBF_TEXT_(2, setup, "WB=%d", p_env->write_buffers);
L
Linus Torvalds 已提交
3297 3298 3299 3300 3301 3302
	return count;
}

static DEVICE_ATTR(write_buffer, 0644, claw_wbuff_show, claw_wbuff_write);

static ssize_t
3303
claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3304 3305 3306 3307
{
	struct claw_privbk *priv;
	struct claw_env *  p_env;

3308
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3309 3310 3311 3312 3313 3314 3315
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	return sprintf(buf, "%d\n", p_env->read_buffers);
}

static ssize_t
3316 3317
claw_rbuff_write(struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
L
Linus Torvalds 已提交
3318 3319 3320 3321 3322
{
	struct claw_privbk *priv;
	struct claw_env *p_env;
	int nnn,max;

3323
	priv = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
	if (!priv)
		return -ENODEV;
	p_env = priv->p_env;
	sscanf(buf, "%i", &nnn);
	if (p_env->packing) {
		max = 64;
	}
	else {
		max = 512;
	}
	if ((nnn > max ) || (nnn < 2))
		return -EINVAL;
	p_env->read_buffers = nnn;
3337 3338
	CLAW_DBF_TEXT(2, setup, "Rbufset");
	CLAW_DBF_TEXT_(2, setup, "RB=%d", p_env->read_buffers);
L
Linus Torvalds 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
	return count;
}

static DEVICE_ATTR(read_buffer, 0644, claw_rbuff_show, claw_rbuff_write);

static struct attribute *claw_attr[] = {
	&dev_attr_read_buffer.attr,
	&dev_attr_write_buffer.attr,
	&dev_attr_adapter_name.attr,
	&dev_attr_api_type.attr,
	&dev_attr_host_name.attr,
	NULL,
};

static struct attribute_group claw_attr_group = {
	.attrs = claw_attr,
};

static int
claw_add_files(struct device *dev)
{
3360
	CLAW_DBF_TEXT(2, setup, "add_file");
L
Linus Torvalds 已提交
3361 3362 3363 3364 3365 3366
	return sysfs_create_group(&dev->kobj, &claw_attr_group);
}

static void
claw_remove_files(struct device *dev)
{
3367
	CLAW_DBF_TEXT(2, setup, "rem_file");
L
Linus Torvalds 已提交
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
	sysfs_remove_group(&dev->kobj, &claw_attr_group);
}

/*--------------------------------------------------------------------*
*    claw_init  and cleanup                                           *
*---------------------------------------------------------------------*/

static void __exit
claw_cleanup(void)
{
3378 3379 3380 3381 3382
	driver_remove_file(&claw_group_driver.driver,
			   &driver_attr_group);
	ccwgroup_driver_unregister(&claw_group_driver);
	ccw_driver_unregister(&claw_ccw_driver);
	root_device_unregister(claw_root_dev);
L
Linus Torvalds 已提交
3383
	claw_unregister_debug_facility();
3384
	pr_info("Driver unloaded\n");
L
Linus Torvalds 已提交
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398

}

/**
 * Initialize module.
 * This is called just after the module is loaded.
 *
 * @return 0 on success, !0 on error.
 */
static int __init
claw_init(void)
{
	int ret = 0;

3399
	pr_info("Loading %s\n", version);
L
Linus Torvalds 已提交
3400 3401
	ret = claw_register_debug_facility();
	if (ret) {
3402 3403
		pr_err("Registering with the S/390 debug feature"
			" failed with error code %d\n", ret);
3404
		goto out_err;
L
Linus Torvalds 已提交
3405
	}
3406
	CLAW_DBF_TEXT(2, setup, "init_mod");
3407
	claw_root_dev = root_device_register("claw");
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
	ret = IS_ERR(claw_root_dev) ? PTR_ERR(claw_root_dev) : 0;
	if (ret)
		goto register_err;
	ret = ccw_driver_register(&claw_ccw_driver);
	if (ret)
		goto ccw_err;
	claw_group_driver.driver.groups = claw_group_attr_groups;
	ret = ccwgroup_driver_register(&claw_group_driver);
	if (ret)
		goto ccwgroup_err;
	return 0;

ccwgroup_err:
	ccw_driver_unregister(&claw_ccw_driver);
ccw_err:
	root_device_unregister(claw_root_dev);
register_err:
	CLAW_DBF_TEXT(2, setup, "init_bad");
	claw_unregister_debug_facility();
out_err:
	pr_err("Initializing the claw device driver failed\n");
L
Linus Torvalds 已提交
3429 3430 3431 3432 3433 3434
	return ret;
}

module_init(claw_init);
module_exit(claw_cleanup);

3435 3436 3437 3438
MODULE_AUTHOR("Andy Richter <richtera@us.ibm.com>");
MODULE_DESCRIPTION("Linux for System z CLAW Driver\n" \
			"Copyright 2000,2008 IBM Corporation\n");
MODULE_LICENSE("GPL");