1 /*
2 	Cisco/7000 driver -- Copyright (C) 2000 UTS Global LLC.
3 	Author: Bob Scardapane (UTS Global LLC).
4 	Version: 3.
5 
6 	This program is free software; you can redistribute it and/or
7 	modify it under the terms of the GNU General Public License as
8 	published by the Free Software Foundation; either version 2, or
9 	(at your option) any later version.
10 
11 	This program is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 	GNU General Public License for more details.
15 
16 	You should have received a copy of the GNU General Public License
17 	along with this program; if not, write to the Free Software
18 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 
20 	To use this driver, run the LINUX command:
21 
22 	insmod c7000 base0=0xYYYY lhost0=s1 uhost0=s2 lappl0=s3 uappl0=s4 dbg=x
23 
24 	base0=0xYYYY defines the base unit address of the interface.
25 	lhost0=s1 defines the local host name.
26 	uhost0=s2 defines the unit host name.
27 	lappl0=s3 defines the local application name.
28 	uappl0=s4 defines the unit application name.
29 	dbg=x defines the message level.  Higher values will result in
30 	additional diagnostic messages.
31 
32 	Additional interfaces are defined on insmod by using the variable
33 	name groups "base1,lhost1,lappl1,uhost1,uappl1", etc... up to three
34 	additional groups.
35 
36 	In addition, the module will automatically detect the unit base
37 	addresses by scanning all active irq's for a control unit type
38 	of 0x3088 and a model of 0x61 (CLAW mode). The noauto parameter
39 	can be used to suppress automatic detection.
40 
41 	The values of lhostx, lapplx, uhostx and uapplx default to:
42 
43 	lapplx - TCPIP
44 	lhostx - UTS
45 	uapplx - TCPIP
46 	uhostx - C7011
47 
48 	Note that the values passed in the insmod command will always
49 	override the automatic detection of the unit base addreeses and
50 	the default values of lapplx, lhostx, uapplx and uhostx.
51 
52 	The parameter noauto can be used to disable automatic detection of
53 	devices:
54 
55 	noauto=1 (disable automatic detection)
56 	noauto=0 (Enable automatic detectio.  This is the default value.)
57 
58 	The values in base0 - base3 will be copied to the bases array when
59 	the module is loaded.
60 
61 	To configure the interface(s), run the LINUX command(s):
62 
63 	ifconfig ci0 ...
64 	ifconfig ci1 ...
65 	ifconfig ci2 ...
66 	ifconfig ci3 ...
67 
68 	There is one device structure for each controller in the c7000_devices
69 	array.  The base address of each controller is in the bases array.
70 	These arrays parallel each other.  There is also one c7000_controller
71 	structure for each controller.  This structure is pointed to by field
72 	priv in the individual device structure.
73 
74 	In each c7000_controller, there are embedded c7000_unit structures.
75 	There is one c7000_unit structure per device number that makes up
76 	a controller (currently 2 units per controller).
77 */
78 
79 #include <linux/config.h>
80 #include <linux/module.h>
81 #include <linux/init.h>
82 #include <linux/kernel.h>
83 #include <linux/slab.h>
84 #include <linux/errno.h>
85 #include <linux/types.h>
86 #include <linux/interrupt.h>
87 #include <linux/timer.h>
88 #include <linux/sched.h>
89 #include <linux/signal.h>
90 #include <linux/string.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/if_arp.h>
94 #include <linux/tcp.h>
95 #include <linux/skbuff.h>
96 #include <asm/io.h>
97 #include <asm/bitops.h>
98 #include <asm/irq.h>
99 
100 /*
101 	Global defines
102 */
103 
104 /*
105 	Maximum number of controllers.
106 */
107 
108 #define	MAX_C7000	4
109 
110 /*
111 	Number of units per controller.
112 */
113 
114 #define	NUNITS		2
115 
116 /*
117 	Define indexes of read and write units in the cunits array.
118 */
119 
120 #define	C7000_RD	0
121 #define	C7000_WR	1
122 
123 /*
124 	Number of device buffers.
125 */
126 
127 #define	C7000_MAXBUF	40
128 
129 /*
130 	Transmission queue length.
131 */
132 
133 #define	C7000_TXQUEUE_LEN	100
134 /*
135 	Size of the IP packet data.
136 */
137 
138 #define	C7000_DATAL	4096
139 
140 /*
141 	Size of the read header data.
142 */
143 
144 #define	C7000_READHDRL	4
145 
146 /*
147 	Size of read flag byte.
148 */
149 
150 #define	C7000_READFFL	1
151 
152 /*
153 	Size of a device buffer.  This is how it is arranged in memory:
154 	4096 (IP packet data) + 4 (read header) + 1 (read flag) = 4101.
155 */
156 
157 #define	C7000_BUFSIZE	C7000_DATAL + C7000_READHDRL + C7000_READFFL
158 
159 /*
160 	Size of sigsmod data.
161 */
162 
163 #define	C7000_SIGSMODL	1
164 
165 /*
166 	Flag value that indicates a read was completed in the flag
167 	field of a c7000_rd_header.
168 */
169 
170 #define	FLAG_FF		0xff
171 /*
172 	Size of C7000 sense id data.
173 */
174 
175 #define SIDL		32
176 
177 /*
178 	Maximum number of read and write retries.
179 */
180 
181 #define	C7000_MAX_RETRIES	3
182 
183 /*
184 	Define sense byte0 value for a box reset.
185 */
186 
187 #define	C7000_BOX_RESET		0x41
188 
189 /*
190 	CCW commands.
191 */
192 
193 #define	C7000_WRITE_CCW		0x01	/* normal write */
194 #define	C7000_READ_CCW		0x02	/* normal read */
195 #define	C7000_NOOP_CCW		0x03	/* no operation */
196 #define	C7000_SIGSMOD_CCW	0x05	/* signal status modifier */
197 #define	C7000_TIC_CCW		0x08	/* transfer in channel */
198 #define	C7000_READHDR_CCW	0x12	/* read header  */
199 #define	C7000_READFF_CCW	0x22	/* read FF flag */
200 #define	C7000_SID_CCW		0xe4	/* sense identification */
201 
202 /*
203 	Control commands.
204 */
205 
206 #define	C7000_SYS_VALIDATE		1
207 #define C7000_SYS_VALIDATE_RESP		2
208 #define	C7000_CONN_REQ			33
209 #define C7000_CONN_RESP			34
210 #define C7000_CONN_CONFRM		35
211 #define C7000_DISCONN			36
212 #define C7000_BOXERROR			65
213 
214 /*
215 	State machine values.
216 */
217 
218 #define	C7000_INIT	1
219 #define C7000_HALT	2
220 #define C7000_SID	3
221 #define C7000_SYSVAL	4
222 #define	C7000_CONNECT	5
223 #define C7000_READY	6
224 #define C7000_READ	7
225 #define C7000_WRITE	8
226 #define	C7000_DISC	9
227 #define C7000_STOP	10
228 #define	C7000_STOPPED	11
229 #define C7000_ERROR	12
230 
231 /*
232 	The lower subchannel is used for read operations and the one that is
233 	one higher is used for write operations.
234 
235 	Both subchannels are initially in state C7000_INIT.  A transition to
236 	state C7000_HALT occurs when halt_IO is issued on each.  When the
237 	halts completes a transition to state C7000_SID occurs and a channel
238 	program is issued to do a sense identification on both subchannels.
239 
240 	When the sense identification completes, the state C7000_SYSVAL is
241 	entered on the read subchannel.  A read channel program is issued.
242 
243 	When the sense identification completes, the write subchannel enters
244 	state C7000_SYSVAL and a system validation request is written.  The
245 	read subchannel is also put into this state.
246 
247 	When both the system validation response is read and an inbound system
248 	validation request is read, the inbound system validation request is
249 	responded to and both subchannels enter the C7000_CONNECT state.
250 
251 	A read channel program is posted to look for the inbound connect
252 	request.  When that is received a connection confirmation is written.
253 	The state of both subchannels is then changed to C7000_READY.  A
254 	read channel program is then posted and the state is changed to
255 	C7000_READ.  When a read completes, the packet is sent to the higher
256 	layers and the read channel program is restarted.
257 
258 	When there is a packet to be written, state C7000_WRITE is entered
259 	and a channel program is issued to write the data.  The subchannel
260 	is in state C7000_READY when there is nothing to be written.
261 
262 	When the stop method is executed, a disconnect message is sent and
263 	the state is changed to C7000_DISC in both subchannels.  A halt_IO
264 	will be issued to both subchannels and state C7000_STOP will be entered.
265 	When the halt IO completes, state C7000_STOPPED will be set.
266 
267 	State C7000_ERROR is set when an error occurs in the interrupt
268 	routine.  Recycle the interface (ifconfig down / ifconfig up)
269 	to reset this state.
270 */
271 
272 /*
273 	Results from c7000_check_csw.
274 */
275 
276 enum	c7000_rupt {
277 	C7000_NORMAL,
278 	C7000_CHANERR,
279 	C7000_UCK,
280 	C7000_UCK_RESET,
281 	C7000_UE,
282 	C7000_ATTN,
283 	C7000_BUSY,
284 	C7000_OTHER
285 };
286 
287 /*
288 	Bits set in device structure tbusy field.
289 */
290 
291 #define	TB_TX		0	/* sk buffer handling in progress */
292 #define	TB_STOP		1	/* network device stop in progress */
293 #define	TB_RETRY	2	/* retry in progress */
294 #define	TB_NOBUFFER	3	/* no buffer on free queue */
295 
296 /*
297 	Bit in c7000_unit.flag_a that indicates the bh routine is busy.
298 */
299 
300 #define	C7000_BH_ACTIVE	0
301 
302 #define CPrintk(level, args...) \
303 	if (level <= dbg) \
304 		printk(args)
305 
306 /*
307 	Maximum length of a system validation string.
308 */
309 
310 #define	NAMLEN	8
311 
312 #define	Err_Conn_Confirm	1
313 #define	Err_Names_not_Matched	166
314 #define	Err_C7000_NOT_READY	167
315 #define	Err_Duplicate		170
316 #define	Err_Closing		171
317 #define	Err_No_Such_App		172
318 #define	Err_Host_Not_Ready	173
319 #define	Err_CLOSING		174
320 #define	Err_Dup_Link		175
321 #define	Err_Wrong_Version	179
322 #define	Err_Wrong_Frame_Size	180
323 
324 /*
325 	Define a macro to extract the logical link identifier from
326 	the c7000 read header command field.
327 */
328 
329 #define	C7000_LINKID(cmd)	((unsigned char)cmd >> 3)
330 
331 /*
332 	Define the control unit type for a Cisco 7000.
333 */
334 
335 #define	C7000_CU_TYPE		0x3088
336 
337 /*
338 	Define the control unit model for a Cisco 7000.
339 */
340 
341 #define	C7000_CU_MODEL		0x61
342 
343 /*
344 	Define the default system validate parameters (lapplx,
345 	lhostx, uapplx, uhostx).
346 */
347 
348 #define	C7000_DFLT_LAPPL	"TCPIP"
349 #define	C7000_DFLT_LHOST	"UTS"
350 #define	C7000_DFLT_UAPPL	"TCPIP"
351 #define	C7000_DFLT_UHOST	"C7011"
352 
353 /*
354 	Global variables.
355 */
356 
357 /*
358 	Base device addresses of the controllers.
359 */
360 
361 static int	base0 = -1;
362 static int	base1 = -1;
363 static int	base2 = -1;
364 static int	base3 = -1;
365 
366 static int	bases[MAX_C7000];
367 
368 /*
369 	Local application names.
370 */
371 
372 static char	*lappl0;
373 static char	*lappl1;
374 static char	*lappl2;
375 static char	*lappl3;
376 
377 /*
378 	Local host names.
379 */
380 
381 static char	*lhost0;
382 static char	*lhost1;
383 static char	*lhost2;
384 static char	*lhost3;
385 
386 /*
387 	Unit application names.
388 */
389 
390 static char	*uappl0;
391 static char	*uappl1;
392 static char	*uappl2;
393 static char	*uappl3;
394 
395 /*
396 	Unit hosts names.
397 */
398 
399 static char	*uhost0;
400 static char	*uhost1;
401 static char	*uhost2;
402 static char	*uhost3;
403 
404 /*
405 	Debugging level (higher numbers emit lower priority messages).
406 */
407 
408 static unsigned int	dbg = 0;
409 
410 /*
411 	Parameter that controls auto detection.
412 */
413 
414 static int	noauto = 0;
415 
416 /*
417 	Interface names.
418 */
419 
420 static char	ifnames[MAX_C7000][8] = {"ci0", "ci1", "ci2", "ci3"};
421 
422 /*
423 	One device structure per controller.
424 */
425 
426 struct net_device	c7000_devices[MAX_C7000];
427 
428 /*
429 	Scratch variable filled in with controller name.
430 */
431 
432 static char	*controller;
433 
434 /*
435 	Identify parameters that can be passed on the LINUX insmod command.
436 */
437 
438 MODULE_AUTHOR("Robert Scardapane (UTS Global)");
439 MODULE_DESCRIPTION("Network module for Cisco 7000 box.");
440 MODULE_LICENSE("GPL");
441 
442 MODULE_PARM(base0, "1i");
443 MODULE_PARM_DESC(base0, "Base unit address for 1st C7000 box.");
444 MODULE_PARM(base1, "1i");
445 MODULE_PARM_DESC(base1, "Base unit address for 2nd C7000 box.");
446 MODULE_PARM(base2, "1i");
447 MODULE_PARM_DESC(base2, "Base unit address for 3rd C7000 box.");
448 MODULE_PARM(base3, "1i");
449 MODULE_PARM_DESC(base3, "Base unit address for 4th C7000 box.");
450 
451 MODULE_PARM(lappl0, "s");
452 MODULE_PARM_DESC(lappl0, "Application name for 1st C7000 box.");
453 MODULE_PARM(lappl1, "s");
454 MODULE_PARM_DESC(lappl1, "Application name for 2nd C7000 box.");
455 MODULE_PARM(lappl2, "s");
456 MODULE_PARM_DESC(lappl2, "Application name for 3rd C7000 box.");
457 MODULE_PARM(lappl3, "s");
458 MODULE_PARM_DESC(lappl3, "Application name for 4th C7000 box.");
459 
460 MODULE_PARM(lhost0, "s");
461 MODULE_PARM_DESC(lhost0, "Host name for 1st C7000 box.");
462 MODULE_PARM(lhost1, "s");
463 MODULE_PARM_DESC(lhost1, "Host name for 2nd C7000 box.");
464 MODULE_PARM(lhost2, "s");
465 MODULE_PARM_DESC(lhost2, "Host name for 3rd C7000 box.");
466 MODULE_PARM(lhost3, "s");
467 MODULE_PARM_DESC(lhost3, "Host name for 4th C7000 box.");
468 
469 MODULE_PARM(uhost0, "s");
470 MODULE_PARM_DESC(uhost0, "Unit name for 1st C7000 box.");
471 MODULE_PARM(uhost1, "s");
472 MODULE_PARM_DESC(uhost1, "Unit name for 2nd C7000 box.");
473 MODULE_PARM(uhost2, "s");
474 MODULE_PARM_DESC(uhost2, "Unit name for 3rd C7000 box.");
475 MODULE_PARM(uhost3, "s");
476 MODULE_PARM_DESC(uhost3, "Unit name for 4th C7000 box.");
477 
478 MODULE_PARM(uappl0, "s");
479 MODULE_PARM_DESC(uappl0, "Unit application name for 1st C7000 box.");
480 MODULE_PARM(uappl1, "s");
481 MODULE_PARM_DESC(uappl1, "Unit application name for 2nd C7000 box.");
482 MODULE_PARM(uappl2, "s");
483 MODULE_PARM_DESC(uappl2, "Unit application name for 3rd C7000 box.");
484 MODULE_PARM(uappl3, "s");
485 MODULE_PARM_DESC(uappl3, "Unit application name for 4th C7000 box.");
486 
487 MODULE_PARM(dbg, "1i");
488 MODULE_PARM_DESC(dbg, "Message level for debugging.");
489 
490 MODULE_PARM(noauto, "1i");
491 MODULE_PARM_DESC(noauto, "Control automatic detection of unit base addresses.");
492 
493 /*
494 	Structure used to manage unit buffers.
495 */
496 
497 struct	c7000_buffer {
498 	ccw1_t			ccws[7];	/* channel program */
499 	struct	c7000_buffer	*next;		/* list pointer */
500 	char			*data;		/* pointer to actual data */
501 	int			len;		/* length of the data */
502 };
503 
504 /*
505 	C7000 Control Block.
506  */
507 
508 struct	c7000_control_blk {
509 	unsigned char	cmd;
510 	unsigned char	ver;
511 	unsigned char	link_id;
512 	unsigned char	correlator;
513 	unsigned char	ret_code;
514 	unsigned char	resvd1[3];
515 	unsigned char	unitname[NAMLEN];
516 	unsigned char	hostname[NAMLEN];
517 	unsigned short	rdsize;		/* read frame size   */
518 	unsigned short	wrtsize;	/* write frame size  */
519 	unsigned char	resvd2[4];
520 };
521 
522 /*
523 	Per unit structure contained within the c7000_controller structure.
524 */
525 
526 struct	c7000_unit {
527 	ccw1_t				ccws[5];	/* control ccws */
528 	int				devno;		/* device number */
529 	int				irq;		/* subchannel number */
530 	unsigned long			IO_active;	/* IO activity flag */
531 	int				state;		/* fsm state */
532 	int				retries;	/* retry counter */
533 	unsigned long			flag_a;		/* bh activity flag */
534 	devstat_t			devstat;	/* device status */
535 	wait_queue_head_t		wait;		/* sleep q head */
536 	struct c7000_controller		*cntlp;		/* controller pointer */
537 	struct c7000_buffer		*free;		/* free buffer anchor */
538 	struct c7000_buffer		*proc_head;	/* proc head */
539 	struct c7000_buffer		*proc_tail;	/* proc tail */
540 	struct c7000_buffer		*bh_head;	/* bh head */
541 	struct c7000_buffer		*bh_tail;	/* bh tail */
542 	struct tq_struct		tq;		/* bh scheduling */
543 	char				senseid[SIDL];	/* sense id data */
544 	struct c7000_control_blk	control_blk;	/* control block */
545 	unsigned char			sigsmod;	/* sigsmod flag */
546 	unsigned char			readhdr[4];	/* read header */
547 	unsigned char			readff;		/* readff flag */
548 };
549 
550 /*
551 	Private structure pointed to by dev->priv.
552 */
553 
554 struct c7000_controller {
555 	struct	net_device_stats	stats;		/* statistics */
556 	struct net_device		*dev;		/* -> device struct */
557 	unsigned int			base_addr;	/* base address */
558 	char				lappl[NAMLEN];	/* local appl */
559 	char				lhost[NAMLEN];	/* local host */
560 	char				uappl[NAMLEN];	/* unit appl */
561 	char				uhost[NAMLEN];	/* unit host */
562 	unsigned char			version;	/* version = 2 */
563 	unsigned char			linkid;		/* link id */
564 	struct	c7000_unit		cunits[NUNITS];	/* embedded units */
565 	unsigned long			tbusy;
566 };
567 
568 /*
569 	This is the structure returned by the C7000_READHDR_CCW.
570 */
571 
572 struct	c7000_rd_header {
573 	unsigned short	len;	/* packet length */
574 	unsigned char	cmd;	/* command code */
575 	unsigned char	flag;	/* flag */
576 };
577 
578 /*
579 	Set the device structure transmission busy flag.
580 */
581 
582 #define c7000_set_busy(dev) netif_stop_queue(dev)
583 
584 /*
585 	Clear the device structure transmission busy flag.
586 */
587 
588 #define c7000_clear_busy(dev) netif_wake_queue(dev)
589 
590 /*
591 	Extract the device structure transmission busy flag.
592 */
593 
594 #define c7000_check_busy(dev) netif_queue_stopped(dev)
595 
596 /*
597 	Set a bit in the device structure transmission busy flag.
598 */
599 
600 static __inline__ void
c7000_setbit_busy(int nr,struct net_device * dev)601 c7000_setbit_busy(int nr, struct net_device *dev)
602 {
603 	netif_stop_queue(dev);
604 	test_and_set_bit(nr, &((struct c7000_controller *)dev->priv)->tbusy);
605 	return;
606 }
607 
608 /*
609 	Clear a bit in the device structure transmission busy flag.
610 */
611 
612 static __inline__ void
c7000_clearbit_busy(int nr,struct net_device * dev)613 c7000_clearbit_busy(int nr, struct net_device *dev)
614 {
615 	clear_bit(nr, &((struct c7000_controller *)dev->priv)->tbusy);
616 	netif_wake_queue(dev);
617 	return;
618 }
619 
620 /*
621 	Test and set a bit in the device structure transmission busy flag.
622 */
623 
624 static __inline__ int
c7000_ts_busy(int nr,struct net_device * dev)625 c7000_ts_busy(int nr, struct net_device *dev)
626 {
627 	netif_stop_queue(dev);
628 	return test_and_set_bit(nr, &((struct c7000_controller *)dev->priv)->tbusy);
629 }
630 
631 /*
632 	Set the C7000 controller in the error state.
633 */
634 
635 static void
c7000_error(struct c7000_controller * ccp)636 c7000_error(struct c7000_controller *ccp)
637 {
638 	int			i;
639 	struct	c7000_unit	*cup;
640 	struct net_device	*dev = ccp->dev;
641 
642 	for (i = 0; i < NUNITS; i++) {
643 		cup = &ccp->cunits[i];
644 		cup->state = C7000_ERROR;
645 	}
646 
647 	if (dev != NULL)
648 		/* RBH XXX Should we be doing this? */
649 		dev->state &= ~__LINK_STATE_START;
650 
651 	CPrintk(0, "c7000: c7000_error: base unit 0x%x is down\n", ccp->base_addr);
652 	return;
653 }
654 
655 /*
656 	Based on the SENSE ID information, fill in the
657 	controller name.  Note that this is the SENSE ID
658 	information saved by LINUX/390 at boot time.
659 */
660 
661 static int
c7000_check_type(senseid_t * id)662 c7000_check_type(senseid_t *id)
663 {
664 
665 	switch (id->cu_type) {
666 
667 		case C7000_CU_TYPE:
668 
669 			if (id->cu_model == C7000_CU_MODEL) {
670 				controller = "C7000  ";
671  				return(0);
672 			}
673 
674 			break;
675 
676                 default:
677 			break;
678 	}
679 
680 	return(-1);
681 }
682 
683 /*
684 	Check the device information for the controller.
685 */
686 
687 static int
c7000_check_devices(int devno)688 c7000_check_devices(int devno)
689 {
690 	int		i;
691 	s390_dev_info_t	temp;
692 
693 	/*
694 		Get the SENSE ID information for each device.
695 	*/
696 
697 	for (i = devno; i < (devno + NUNITS); i++) {
698 
699 		if (get_dev_info_by_devno(devno, &temp) != 0)
700 			return(-1);
701 
702 		if (c7000_check_type(&temp.sid_data) == -1)
703 			return(-1);
704 	}
705 
706 	CPrintk(1, "c7000: c7000_check_devices: device type is %s\n", controller);
707 	return(0);
708 }
709 
710 /*
711 	Issue a halt I/O to device pointed to by cup.
712 */
713 
714 static int
c7000_haltio(struct c7000_unit * cup)715 c7000_haltio(struct c7000_unit *cup)
716 {
717 	unsigned long		parm;
718 	__u8			flags = 0x00;
719 	unsigned long		saveflags;
720 	DECLARE_WAITQUEUE(wait, current);
721 	int			rc;
722 
723 	s390irq_spin_lock_irqsave(cup->irq, saveflags);
724 	parm = (unsigned long)cup;
725 
726 	if ((rc = halt_IO(cup->irq, parm, flags)) != 0) {
727 		s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
728 		return(rc);
729 	}
730 
731 	/*
732 		Wait for the halt I/O to finish.
733 	*/
734 
735 	add_wait_queue(&cup->wait, &wait);
736 	current->state = TASK_UNINTERRUPTIBLE;
737 	s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
738 	schedule();
739 	remove_wait_queue(&cup->wait, &wait);
740 	return(0);
741 }
742 
743 /*
744 	Issue a start I/O to device pointed to by cup.
745 */
746 
747 static int
c7000_doio(struct c7000_unit * cup)748 c7000_doio(struct c7000_unit *cup)
749 {
750 	unsigned long		parm;
751 	__u8			flags = 0x00;
752 	unsigned long		saveflags;
753 	DECLARE_WAITQUEUE(wait, current);
754 	int			rc;
755 
756 	/*
757 		Do no further I/O while the device is in the ERROR, STOP
758 		or STOPPED state.
759 	*/
760 
761 	if (cup->state == C7000_ERROR || cup->state == C7000_STOP || cup->state == C7000_STOPPED)
762 		return(-1);
763 
764 	s390irq_spin_lock_irqsave(cup->irq, saveflags);
765 	parm = (unsigned long)cup;
766 
767 	if ((rc = do_IO(cup->irq, &cup->ccws[0], parm, 0xff, flags)) != 0) {
768 		s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
769 		return(rc);
770 	}
771 
772 	/*
773 		Wait for the I/O to complete.
774 	*/
775 
776 	add_wait_queue(&cup->wait, &wait);
777 	current->state = TASK_UNINTERRUPTIBLE;
778 	s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
779 	schedule();
780 	remove_wait_queue(&cup->wait, &wait);
781 
782 	/*
783 		Interrupt handling may have marked the device in ERROR.
784 	*/
785 
786 	if (cup->state == C7000_ERROR)
787 		return(-1);
788 
789 	return(0);
790 }
791 
792 /*
793 	Build a channel program to do a sense id channel program.
794 */
795 
796 static void
c7000_bld_senseid_chpgm(struct c7000_unit * cup)797 c7000_bld_senseid_chpgm(struct c7000_unit *cup)
798 {
799 	ccw1_t	*ccwp;
800 
801 	ccwp = &cup->ccws[0];
802 	ccwp->cmd_code = C7000_SID_CCW;
803 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
804 	ccwp->cda = (__u32)virt_to_phys(&cup->senseid);
805 	ccwp->count = SIDL;
806 	ccwp++;
807 	ccwp->cmd_code = C7000_NOOP_CCW;
808 	ccwp->flags = CCW_FLAG_SLI;
809 	ccwp->cda = (__u32)NULL;
810 	ccwp->count = 1;
811 	return;
812 }
813 
814 /*
815 	Build a channel program to write a control message.
816 */
817 
818 static void
c7000_bld_wrtctl_chpgm(struct c7000_unit * cup)819 c7000_bld_wrtctl_chpgm(struct c7000_unit *cup)
820 {
821 	ccw1_t	*ccwp;
822 
823 	ccwp = &cup->ccws[0];
824 	ccwp->cmd_code = C7000_WRITE_CCW;
825 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
826 	ccwp->cda = (__u32)virt_to_phys(&cup->control_blk);
827 	ccwp->count = sizeof(struct c7000_control_blk);
828 	ccwp++;
829 	ccwp->cmd_code = C7000_READFF_CCW;
830 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
831 	ccwp->cda = (__u32)virt_to_phys(&cup->readff);
832 	ccwp->count = C7000_READFFL;
833 	ccwp++;
834 	ccwp->cmd_code = C7000_TIC_CCW;
835 	ccwp->flags = 0;
836 	ccwp->cda = (__u32)virt_to_phys(ccwp + 1);
837 	ccwp->count = 0;
838 	ccwp++;
839 	ccwp->cmd_code = C7000_NOOP_CCW;
840 	ccwp->flags = CCW_FLAG_SLI;
841 	ccwp->cda = (__u32)NULL;
842 	ccwp->count = 1;
843 	return;
844 }
845 
846 /*
847 	Build a write channel program to write the indicated buffer.
848 */
849 
850 static void
c7000_bld_wrt_chpgm(struct c7000_unit * cup,struct c7000_buffer * buf)851 c7000_bld_wrt_chpgm(struct c7000_unit *cup, struct c7000_buffer *buf)
852 {
853 	ccw1_t				*ccwp;
854 	struct	c7000_controller	*ccp = cup->cntlp;
855 
856 	ccwp = &buf->ccws[0];
857 	ccwp->cmd_code = C7000_WRITE_CCW | (ccp->linkid << 3);
858 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
859 	ccwp->cda = (__u32)virt_to_phys(buf->data);
860 	ccwp->count = buf->len;
861 	ccwp++;
862 	ccwp->cmd_code = C7000_READFF_CCW;
863 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
864 	ccwp->cda = (__u32)virt_to_phys(buf->data + C7000_DATAL + C7000_READHDRL);
865 	ccwp->count = C7000_READFFL;
866 	ccwp++;
867 	ccwp->cmd_code = C7000_TIC_CCW;
868 	ccwp->flags = 0;
869 	ccwp->cda = (__u32)virt_to_phys(ccwp + 1);
870 	ccwp->count = 0;
871 	ccwp++;
872 	ccwp->cmd_code = C7000_NOOP_CCW;
873 	ccwp->flags = (CCW_FLAG_SLI);
874 	ccwp->cda = (__u32)NULL;
875 	ccwp->count = 1;
876 	return;
877 }
878 
879 /*
880 	Build a channel program to read a control message.
881 */
882 
883 static void
c7000_bld_readctl_chpgm(struct c7000_unit * cup)884 c7000_bld_readctl_chpgm(struct c7000_unit *cup)
885 {
886 	ccw1_t	*ccwp;
887 
888 	ccwp = &cup->ccws[0];
889 	ccwp->cmd_code = C7000_READ_CCW;
890 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
891 	ccwp->cda = (__u32)virt_to_phys(&cup->control_blk);
892 	ccwp->count = sizeof(struct c7000_control_blk);
893 	ccwp++;
894 	ccwp->cmd_code = C7000_READHDR_CCW;
895 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
896 	ccwp->cda = (__u32)virt_to_phys(&cup->readhdr);
897 	ccwp->count = C7000_READHDRL;
898 	ccwp++;
899 	ccwp->cmd_code = C7000_SIGSMOD_CCW;
900 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
901 	ccwp->cda = (__u32)virt_to_phys(&cup->sigsmod);
902 	ccwp->count = C7000_SIGSMODL;
903 	ccwp++;
904 	ccwp->cmd_code = C7000_TIC_CCW;
905 	ccwp->flags = 0;
906 	ccwp->cda = (__u32)virt_to_phys(ccwp + 1);
907 	ccwp->count = 0;
908 	ccwp++;
909 	ccwp->cmd_code = C7000_NOOP_CCW;
910 	ccwp->flags = (CCW_FLAG_SLI);
911 	ccwp->cda = (__u32)NULL;
912 	ccwp->count = 1;
913 	return;
914 }
915 
916 /*
917 	Build a channel program to read the indicated buffer.
918 */
919 
920 static void
c7000_bld_read_chpgm(struct c7000_unit * cup,struct c7000_buffer * buf)921 c7000_bld_read_chpgm(struct c7000_unit *cup, struct c7000_buffer *buf)
922 {
923 	ccw1_t	*ccwp;
924 
925 	ccwp = &buf->ccws[0];
926 	ccwp->cmd_code = C7000_READ_CCW;
927 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
928 	ccwp->cda = (__u32)virt_to_phys(buf->data);
929 	ccwp->count = C7000_DATAL;
930 	ccwp++;
931 	ccwp->cmd_code = C7000_READHDR_CCW;
932 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
933 	ccwp->cda = (__u32)virt_to_phys(buf->data + C7000_DATAL);
934 	ccwp->count = C7000_READHDRL;
935 	ccwp++;
936 	ccwp->cmd_code = C7000_SIGSMOD_CCW;
937 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC);
938 	ccwp->cda = (__u32)virt_to_phys(&cup->sigsmod);
939 	ccwp->count = C7000_SIGSMODL;
940 	ccwp++;
941 	ccwp->cmd_code = C7000_TIC_CCW;
942 	ccwp->flags = 0;
943 	ccwp->cda = (__u32)virt_to_phys(ccwp + 3);
944 	ccwp->count = 0;
945 	ccwp++;
946 	ccwp->cmd_code = C7000_READFF_CCW;
947 	ccwp->flags = (CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI);
948 	ccwp->cda = (__u32)virt_to_phys(&cup->readff);
949 	ccwp->count = C7000_READFFL;
950 	ccwp++;
951 	ccwp->cmd_code = C7000_TIC_CCW;
952 	ccwp->flags = 0;
953 	ccwp->cda = (__u32)virt_to_phys(ccwp + 1);
954 	ccwp->count = 0;
955 	ccwp++;
956 	ccwp->cmd_code = C7000_NOOP_CCW;
957 	ccwp->flags = (CCW_FLAG_SLI);
958 	ccwp->cda = (__u32)NULL;
959 	ccwp->count = 1;
960 	return;
961 }
962 
963 /*
964 	Allocate buffer structure headers and buffers for all units
965 	A return value of 0 means that all allocations worked.  A -1
966 	means that an allocation failed.  It is expected that the caller
967 	will call c7000_free_buffers when -1 is returned.
968 */
969 
970 static int
c7000_alloc_buffers(struct net_device * dev)971 c7000_alloc_buffers(struct net_device *dev)
972 {
973 	int				i;
974 	int				j;
975 	char				*data;
976 	struct	c7000_buffer		*bufptr;
977 	struct	c7000_controller	*ccp = (struct c7000_controller *) dev->priv;
978 	struct	c7000_unit		*cup;
979 
980 	for (i = 0; i < NUNITS; i++) {
981 		cup = &ccp->cunits[i];
982 		cup->free = NULL;
983 
984 		for (j = 0; j < C7000_MAXBUF; j++) {
985 			bufptr = kmalloc(sizeof(struct c7000_buffer), GFP_KERNEL);
986 			data = kmalloc(C7000_BUFSIZE, GFP_KERNEL);
987 
988 			if (bufptr == NULL || data == NULL) {
989 				if (bufptr)
990 					kfree(bufptr);
991 				if (data)
992 					kfree(data);
993 				return(-1);
994 			}
995 
996 			/*
997 				Place filled in buffer header on free anchor.
998 			*/
999 
1000 			bufptr->next = cup->free;
1001 			bufptr->data = data;
1002 			bufptr->len = 0;
1003 			cup->free = bufptr;
1004 
1005 			if (data == NULL)
1006 				return(-1);
1007 
1008 			memset(data, '\0', C7000_BUFSIZE);
1009 		}
1010 
1011 	}
1012 
1013 	CPrintk(1, "c7000: c7000_alloc_buffers: allocated buffers for base unit 0x%lx\n", dev->base_addr);
1014 	return(0);
1015 }
1016 
1017 /*
1018 	Free buffers on a chain.
1019 */
1020 
1021 static void
c7000_free_chain(struct c7000_buffer * buf)1022 c7000_free_chain(struct c7000_buffer *buf)
1023 {
1024 	char			*data;
1025 	struct	c7000_buffer	*bufptr = buf;
1026 	struct	c7000_buffer	*tmp;
1027 
1028 	while (bufptr != NULL) {
1029 		data = bufptr->data;
1030 
1031 		if (data != NULL)
1032 			kfree(data);
1033 
1034 		tmp = bufptr;
1035 		bufptr = bufptr->next;
1036 		kfree(tmp);
1037 	}
1038 
1039 	return;
1040 }
1041 
1042 /*
1043 	Free buffers on all possible chains for all units.
1044 */
1045 
1046 static void
c7000_free_buffers(struct net_device * dev)1047 c7000_free_buffers(struct net_device *dev)
1048 {
1049 	int				i;
1050 	struct	c7000_controller	*ccp = (struct c7000_controller *) dev->priv;
1051 	struct	c7000_unit	*cup;
1052 
1053 	for (i = 0; i < NUNITS; i++) {
1054 		cup = &ccp->cunits[i];
1055 		c7000_free_chain(cup->free);
1056 		cup->free = NULL;
1057 		c7000_free_chain(cup->proc_head);
1058 		cup->proc_head = cup->proc_tail = NULL;
1059 		c7000_free_chain(cup->bh_head);
1060 		cup->bh_head = cup->bh_tail = NULL;
1061 	}
1062 
1063 	CPrintk(1, "c7000: c7000_free_buffers: freed buffers for base unit 0x%lx\n", dev->base_addr);
1064 	return;
1065 }
1066 
1067 /*
1068 	Obtain a free buffer.  Return a pointer to the c7000_buffer
1069 	structure OR NULL.
1070 */
1071 
1072 struct c7000_buffer *
c7000_get_buffer(struct c7000_unit * cup)1073 c7000_get_buffer(struct c7000_unit *cup)
1074 {
1075 	struct	c7000_buffer	*buf;
1076 
1077 	buf = cup->free;
1078 
1079 	if (buf == NULL)
1080 		return(NULL);
1081 
1082 	cup->free = buf->next;
1083 	buf->next = NULL;
1084 	return(buf);
1085 }
1086 
1087 /*
1088 	Release a buffer to the free list.
1089 */
1090 
1091 void
c7000_release_buffer(struct c7000_unit * cup,struct c7000_buffer * buf)1092 c7000_release_buffer(struct c7000_unit *cup, struct c7000_buffer *buf)
1093 {
1094 	struct	c7000_buffer	*tmp;
1095 
1096 	tmp = cup->free;
1097 	cup->free = buf;
1098 	buf->next = tmp;
1099 	return;
1100 }
1101 
1102 /*
1103 	Queue a buffer on the end of the processing (proc) chain.
1104 */
1105 
1106 void
c7000_queue_buffer(struct c7000_unit * cup,struct c7000_buffer * buf)1107 c7000_queue_buffer(struct c7000_unit *cup, struct c7000_buffer *buf)
1108 {
1109 	buf->next = NULL;
1110 
1111 	if (cup->proc_head == NULL) {
1112 		cup->proc_head = cup->proc_tail = buf;
1113 		return;
1114 	}
1115 
1116 	cup->proc_tail->next = buf;
1117 	cup->proc_tail = buf;
1118 	return;
1119 }
1120 
1121 /*
1122 	Dequeue a buffer from the start of the processing (proc) chain.
1123 */
1124 
1125 struct c7000_buffer *
c7000_dequeue_buffer(struct c7000_unit * cup)1126 c7000_dequeue_buffer(struct c7000_unit *cup)
1127 {
1128 	struct	c7000_buffer	*buf = cup->proc_head;
1129 
1130 	if (buf == NULL)
1131 		return(NULL);
1132 
1133 	cup->proc_head = buf->next;
1134 
1135 	if (cup->proc_head == NULL)
1136 		cup->proc_tail = NULL;
1137 
1138 	buf->next = NULL;
1139 	return(buf);
1140 }
1141 
1142 /*
1143 	Queue a buffer on the end of the bh routine chain.
1144 */
1145 
1146 void
c7000_queue_bh_buffer(struct c7000_unit * cup,struct c7000_buffer * buf)1147 c7000_queue_bh_buffer(struct c7000_unit *cup, struct c7000_buffer *buf)
1148 {
1149 	buf->next = NULL;
1150 
1151 	if (cup->bh_head == NULL) {
1152 		cup->bh_head = cup->bh_tail = buf;
1153 		return;
1154 	}
1155 
1156 	cup->bh_tail->next = buf;
1157 	cup->bh_tail = buf;
1158 	return;
1159 }
1160 
1161 /*
1162 	Dequeue a buffer from the start of the bh routine chain.
1163 */
1164 
1165 struct c7000_buffer *
c7000_dequeue_bh_buffer(struct c7000_unit * cup)1166 c7000_dequeue_bh_buffer(struct c7000_unit *cup)
1167 {
1168 	struct	c7000_buffer	*buf = cup->bh_head;
1169 
1170 	if (buf == NULL)
1171 		return(NULL);
1172 
1173 	cup->bh_head = buf->next;
1174 
1175 	if (cup->bh_head == NULL)
1176 		cup->bh_tail = NULL;
1177 
1178 	buf->next = NULL;
1179 	return(buf);
1180 }
1181 
1182 /*
1183 	Build up a list of buffers to read.  Each buffer is described
1184 	by one c7000_buffer structure.  The c7000_buffer structure
1185 	contains a channel segment that will read that one buffer.
1186 	The channel program segments are chained together via TIC
1187 	CCWS.
1188 */
1189 
1190 static int
c7000_bld_read_chain(struct c7000_unit * cup)1191 c7000_bld_read_chain(struct c7000_unit *cup)
1192 {
1193 	struct	c7000_buffer	*buf, *pbuf = NULL;
1194 	struct	c7000_rd_header	*head;
1195 	int			num = 0;
1196 
1197 	while (cup->free != NULL) {
1198 
1199 		/*
1200 			Obtain a buffer for a read channel segment.
1201 		*/
1202 
1203 		if ((buf = c7000_get_buffer(cup)) == NULL) {
1204 			CPrintk(0, "c7000: c7000_bld_read_chain: can not obtain a read buffer for unit 0x%x\n", cup->devno);
1205 			return(-ENOMEM);
1206 		}
1207 
1208 		num++;
1209 		buf->len = 0;
1210 
1211 		/*
1212 			Clear out the read header flag.
1213 		*/
1214 
1215 		head = (struct c7000_rd_header *)(buf->data + C7000_DATAL);
1216 		head->flag = 0x00;
1217 		c7000_queue_buffer(cup, buf);
1218 
1219 		/*
1220 			Build the read channel program segment.
1221 		*/
1222 
1223 		c7000_bld_read_chpgm(cup, buf);
1224 
1225 		/*
1226 			Chain the prior (if any) channel program segment to
1227 			this one.
1228 		*/
1229 
1230 		if (pbuf != NULL)
1231 			pbuf->ccws[3].cda = pbuf->ccws[5].cda = (__u32)virt_to_phys(&buf->ccws[0]);
1232 
1233 		pbuf = buf;
1234 	}
1235 
1236 	CPrintk(1, "c7000: c7000_bld_read_chain: chained %d buffers for unit 0x%x\n", num, cup->devno);
1237 	return(0);
1238 }
1239 
1240 /*
1241 	Build up a list of buffers to write.  Each buffer is described
1242 	by one c7000_buffer structure.  The c7000_buffer structure
1243 	contains a channel segment that will write that one buffer.
1244 	The channel program segments are chained together via TIC
1245 	CCWS.
1246 */
1247 
1248 static void
c7000_bld_wrt_chain(struct c7000_unit * cup)1249 c7000_bld_wrt_chain(struct c7000_unit *cup)
1250 {
1251 	struct	c7000_buffer	*buf = cup->proc_head, *pbuf = NULL;
1252 	int			num = 0;
1253 
1254 	while (buf != NULL) {
1255 		c7000_bld_wrt_chpgm(cup, buf);
1256 
1257 		/*
1258 			Chain the channel program segments together.
1259 		*/
1260 
1261 		if (pbuf != NULL)
1262 			pbuf->ccws[2].cda = (__u32)virt_to_phys(&buf->ccws[0]);
1263 
1264 		pbuf = buf;
1265 		buf = buf->next;
1266 		num++;
1267 	}
1268 
1269 	CPrintk(1, "c7000: c7000_bld_wrt_chain: chained %d buffers for unit 0x%x\n", num, cup->devno);
1270 	return;
1271 }
1272 
1273 /*
1274 	Interrupt handler bottom half (bh) routine.
1275 	Process all of the buffers on the c7000_unit bh chain.
1276 	The bh chain is populated by the interrupt routine when
1277 	a READ channel program completes on a buffer.
1278 */
1279 
1280 static void
c7000_irq_bh(struct c7000_unit * cup)1281 c7000_irq_bh(struct c7000_unit *cup)
1282 {
1283 	struct	c7000_buffer		*buf, *pbuf;
1284 	struct	c7000_rd_header		*head;
1285 	struct	sk_buff			*skb;
1286 	struct	c7000_controller	*ccp;
1287 	struct	net_device		*dev;
1288 	int				rc;
1289 	__u16				data_length;
1290 	unsigned long			parm;
1291 	__u8				flags = 0x00;
1292 	unsigned long			saveflags;
1293 
1294 	ccp = cup->cntlp;
1295 	dev = ccp->dev;
1296 
1297 	s390irq_spin_lock_irqsave(cup->irq, saveflags);
1298 
1299 	/*
1300 		Process all buffers sent to bh by the interrupt routine.
1301 	*/
1302 
1303 	while (cup->bh_head != NULL) {
1304 		buf = c7000_dequeue_bh_buffer(cup);
1305 
1306 		/*
1307 			Deference the data as a c7000 header.
1308 		*/
1309 
1310 		head = (struct c7000_rd_header *)(buf->data + C7000_DATAL);
1311 
1312 		/*
1313 			If it is a control message, release the buffer and
1314 			continue the loop.
1315 		*/
1316 
1317 		if (C7000_LINKID(head->cmd) == 0) {
1318 			CPrintk(0, "c7000: c7000_irq_bh: unexpected control command %d on unit 0x%x\n", head->cmd, cup->devno);
1319 			c7000_release_buffer(cup, buf);
1320 			continue;
1321 		}
1322 
1323 		/*
1324 			Allocate a socket buffer.
1325 		*/
1326 
1327 		data_length = head->len;
1328 		skb = dev_alloc_skb(data_length);
1329 
1330 		/*
1331 			Copy the data to the skb.
1332 			Send it to the upper layers.
1333 		*/
1334 
1335 		if (skb != NULL) {
1336 			memcpy(skb_put(skb, data_length), buf->data, data_length);
1337 			skb->dev = dev;
1338 			skb->protocol = htons(ETH_P_IP);
1339 			skb->pkt_type = PACKET_HOST;
1340 			skb->mac.raw = skb->data;
1341 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1342 			netif_rx(skb);
1343 			ccp->stats.rx_packets++;
1344 			ccp->stats.rx_bytes += skb->len;
1345 		} else {
1346 			CPrintk(0, "c7000: c7000_irq_bh: can not allocate a skb for unit 0x%x\n", cup->devno);
1347 			ccp->stats.rx_dropped++;
1348 		}
1349 
1350 		/*
1351 			Rechain the buffer on the processing list.
1352 		*/
1353 
1354 		head->flag = 0x00;
1355 		buf->len = 0;
1356 		pbuf = cup->proc_tail;
1357 		c7000_queue_buffer(cup, buf);
1358 
1359 		/*
1360 			Rechain the buffer on the running channel program.
1361 		*/
1362 
1363 		buf->ccws[3].cda = buf->ccws[5].cda = (__u32)virt_to_phys(&buf->ccws[6]);
1364 		if (pbuf != NULL)
1365 			pbuf->ccws[3].cda = pbuf->ccws[5].cda = (__u32)virt_to_phys(&buf->ccws[0]);
1366 
1367 	}
1368 
1369 	/*
1370 		Restart the READ channel program if IO_active is 0.
1371 	*/
1372 
1373 	if (test_and_set_bit(0, (void *)&cup->IO_active) == 0) {
1374 
1375 		if ((rc = c7000_bld_read_chain(cup)) != 0) {
1376 			CPrintk(0, "c7000: c7000_irq_bh: can not build read chain for unit 0x%x, return code %d\n", cup->devno, rc);
1377 			c7000_error(cup->cntlp);
1378 			clear_bit(C7000_BH_ACTIVE, (void *)&cup->flag_a);
1379 			s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
1380 			return;
1381 		}
1382 
1383 		parm = (unsigned long)cup;
1384 		cup->state = C7000_READ;
1385 
1386 		if ((rc = do_IO(cup->irq, &cup->proc_head->ccws[0], parm, 0xff, flags)) != 0) {
1387 			CPrintk(0, "c7000: c7000_irq_bh: can not start READ IO to unit 0x%x, return code %d\n", cup->devno, rc);
1388 			c7000_error(cup->cntlp);
1389 			clear_bit(C7000_BH_ACTIVE, (void *)&cup->flag_a);
1390 			s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
1391 			return;
1392 		}
1393 
1394 		CPrintk(1, "c7000: c7000_irq_bh: started READ IO to unit 0x%x\n", cup->devno);
1395 	}
1396 
1397 	/*
1398 		Clear the bh active indication.
1399 	*/
1400 
1401 	clear_bit(C7000_BH_ACTIVE, (void *)&cup->flag_a);
1402 	s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
1403 	return;
1404 }
1405 
1406 /*
1407 	Send a system validate control command to a unit.
1408 */
1409 
1410 static int
c7000_send_sysval(struct c7000_unit * cup)1411 c7000_send_sysval(struct c7000_unit *cup)
1412 {
1413 	int				rc;
1414 	struct	c7000_controller	*ccp = cup->cntlp;
1415 	struct	c7000_control_blk	*ctlblkp = &(cup->control_blk);
1416 
1417 	CPrintk(1, "c7000: c7000_send_sysval: send sysval for device 0x%x\n", cup->devno);
1418 
1419 	/*
1420 		Build the system validate control message.
1421 	*/
1422 
1423 	memset(ctlblkp, '\0', sizeof(struct c7000_control_blk));
1424 	ctlblkp->cmd = C7000_SYS_VALIDATE;
1425 	ctlblkp->correlator = 0;
1426 	ctlblkp->link_id = ccp->linkid;
1427 	ctlblkp->ver = ccp->version;
1428 	memcpy(ctlblkp->hostname, ccp->lhost, NAMLEN);
1429 	memcpy(ctlblkp->unitname, ccp->uhost, NAMLEN);
1430 	ctlblkp->rdsize = C7000_DATAL;
1431         ctlblkp->wrtsize = C7000_DATAL;
1432 
1433 	/*
1434 		Build the channel program.
1435 	*/
1436 
1437 	c7000_bld_wrtctl_chpgm(cup);
1438 
1439 	/*
1440 		Do the IO and wait for write to complete.
1441 	*/
1442 
1443 	if ((rc = c7000_doio(cup)) != 0) {
1444 		CPrintk(0, "c7000: c7000_send_sysval failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1445 		return(-1);
1446 	}
1447 
1448 	return(0);
1449 }
1450 
1451 /*
1452 	Send a system validate response control command to a unit.
1453 */
1454 
1455 static int
c7000_send_sysval_resp(struct c7000_unit * cup,unsigned char correlator,int ret_code)1456 c7000_send_sysval_resp(struct c7000_unit *cup, unsigned char correlator, int ret_code)
1457 {
1458 	int				rc;
1459 	struct	c7000_controller	*ccp = cup->cntlp;
1460 	struct	c7000_control_blk	*ctlblkp = &(cup->control_blk);
1461 
1462 	CPrintk(1, "c7000: c7000_send_sysval_resp: send sysval response for device 0x%x\n", cup->devno);
1463 
1464 	/*
1465 		Build the system validate response control message.
1466 	*/
1467 
1468 	memset(ctlblkp, '\0', sizeof(struct c7000_control_blk));
1469 	ctlblkp->cmd = C7000_SYS_VALIDATE_RESP;
1470 	ctlblkp->correlator = correlator;
1471 	ctlblkp->ret_code = ret_code;
1472 	ctlblkp->link_id = ccp->linkid;
1473 	ctlblkp->ver = ccp->version;
1474 	memcpy(ctlblkp->hostname, ccp->lhost, NAMLEN);
1475 	memcpy(ctlblkp->unitname, ccp->uhost, NAMLEN);
1476 	ctlblkp->rdsize = C7000_DATAL;
1477         ctlblkp->wrtsize = C7000_DATAL;
1478 
1479 	/*
1480 		Build the channel program.
1481 	*/
1482 
1483 	c7000_bld_wrtctl_chpgm(cup);
1484 
1485 	/*
1486 		Do the IO and wait for write to complete.
1487 	*/
1488 
1489 	if ((rc = c7000_doio(cup)) != 0) {
1490 		CPrintk(0, "c7000: c7000_send_sysval_resp failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1491 		return(-1);
1492 	}
1493 
1494 	return(0);
1495 }
1496 
1497 /*
1498 	Check the information read in a SYS_VALIDATE control message.
1499 */
1500 
1501 static int
c7000_checkinfo(struct c7000_unit * cup)1502 c7000_checkinfo(struct c7000_unit *cup)
1503 {
1504 	struct	c7000_controller	*ccp = cup->cntlp;
1505  	struct	c7000_control_blk	*ctlblkp = &cup->control_blk;
1506 	int				ret_code = 0;
1507 
1508 	if (memcmp(ccp->lhost, ctlblkp->hostname, NAMLEN) ||
1509 		memcmp(ccp->uhost, ctlblkp->unitname, NAMLEN))
1510 		ret_code = Err_Names_not_Matched;
1511 
1512 	if (ctlblkp->ver != ccp->version)
1513 		ret_code = Err_Wrong_Version;
1514 
1515         if ((ctlblkp->rdsize < C7000_DATAL) || (ctlblkp->wrtsize < C7000_DATAL))
1516 		ret_code = Err_Wrong_Frame_Size;
1517 
1518 	if (ret_code != 0)
1519 		CPrintk(0, "c7000: c7000_checkinfo: ret_code %d for device 0x%x\n", ret_code, cup->devno);
1520 
1521 	return(ret_code);
1522 }
1523 
1524 /*
1525 	Keep reading until a sysval response comes in or an error.
1526 */
1527 
1528 static int
c7000_get_sysval_resp(struct c7000_unit * cup)1529 c7000_get_sysval_resp(struct c7000_unit *cup)
1530 {
1531 	struct	c7000_controller	*ccp = cup->cntlp;
1532 	int				resp = 1;
1533 	int				req = 1;
1534 	int				rc;
1535 	int				ret_code = 0;
1536 
1537 	CPrintk(1, "c7000: c7000_get_sysval_resp: get sysval response for unit 0x%x\n", cup->devno);
1538 
1539 	/*
1540 		Wait for the response to C7000_SYS_VALIDATE and for an
1541 		inbound C7000_SYS_VALIDATE.
1542 	*/
1543 
1544 	while (resp || req) {
1545 
1546 		/*
1547 			Build the read channel program.
1548 		*/
1549 
1550 		c7000_bld_readctl_chpgm(cup);
1551 
1552 		if ((rc = c7000_doio(cup)) != 0) {
1553 			CPrintk(0, "c7000: c7000_get_sysval_resp: failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1554 			return(-1);
1555 		}
1556 
1557 		/*
1558 			Process the control message.
1559 		*/
1560 
1561 		switch (cup->control_blk.cmd) {
1562 
1563 			/*
1564 				Check that response is positive and return
1565 				with success. Otherwise, return with an
1566 				error.
1567 			*/
1568 
1569 			case C7000_SYS_VALIDATE_RESP:
1570 
1571 				if (cup->control_blk.ret_code == 0)
1572 					resp = 0;
1573 				else {
1574 					CPrintk(0, "c7000: c7000_get_sysval_resp: receive sysval response for device 0x%x, return code %d\n",
1575 						cup->devno,
1576 						cup->control_blk.ret_code);
1577 					return(-1);
1578 				}
1579 
1580 				break;
1581 
1582 			/*
1583 				Check that the request is reasonable and
1584 				send a SYS_VALIDATE_RESP.  Otherwise,
1585 				return with an error.
1586 			*/
1587 
1588 			case C7000_SYS_VALIDATE:
1589 				CPrintk(1, "c7000: c7000_get_sysval_resp: receive sysval for device 0x%x\n", cup->devno);
1590 				req = 0;
1591 				ret_code = c7000_checkinfo(cup);
1592 
1593 				if (c7000_send_sysval_resp(&ccp->cunits[C7000_WR], cup->control_blk.correlator, ret_code) != 0)
1594 					return(-1);
1595 
1596 				if (ret_code != 0)
1597 					return(-1);
1598 
1599 				break;
1600 
1601 			/*
1602 				Anything else is unexpected and will result
1603 				in a return with an error.
1604 			*/
1605 
1606 			default:
1607 				CPrintk(0, "c7000: c7000_get_sysval_resp: receive unexpected command for device 0x%x, command %d\n", cup->devno, cup->control_blk.cmd);
1608 				return(-1);
1609 				break;
1610 		}
1611 
1612 	}
1613 
1614 	return(0);
1615 }
1616 
1617 /*
1618 	Send a connection confirm control message.
1619 */
1620 
1621 static int
c7000_conn_confrm(struct c7000_unit * cup,unsigned char correlator,int linkid)1622 c7000_conn_confrm(struct c7000_unit *cup, unsigned char correlator, int linkid)
1623 {
1624 	int				rc;
1625 	struct	c7000_controller	*ccp = cup->cntlp;
1626 	struct	c7000_control_blk	*ctlblkp = &(cup->control_blk);
1627 
1628 	CPrintk(1, "c7000: c7000_conn_confrm: send the connection confirmation message for unit 0x%x\n", cup->devno);
1629 
1630 	/*
1631 		Build the connection confirm control message.
1632 	*/
1633 
1634 	memset(ctlblkp, '\0', sizeof(struct c7000_control_blk));
1635 	ctlblkp->cmd = C7000_CONN_CONFRM;
1636 	ctlblkp->ver = ccp->version;
1637 	ctlblkp->link_id = linkid;
1638 	ctlblkp->correlator = correlator;
1639 	ctlblkp->rdsize = 0;
1640 	ctlblkp->wrtsize = 0;
1641 	memcpy(ctlblkp->hostname, ccp->lappl, NAMLEN);
1642 	memcpy(ctlblkp->unitname, ccp->uappl, NAMLEN);
1643 
1644 	/*
1645 		Build the channel program.
1646 	*/
1647 
1648 	c7000_bld_wrtctl_chpgm(cup);
1649 
1650 	/*
1651 		Do the IO and wait for write to complete.
1652 	*/
1653 
1654 	if ((rc = c7000_doio(cup)) != 0) {
1655 		CPrintk(0, "c7000: c7000_conn_confrm: failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1656 		return(-1);
1657 	}
1658 
1659 	return(0);
1660 }
1661 
1662 /*
1663 	Send a connection request control message.
1664 */
1665 
1666 static int
c7000_send_conn(struct c7000_unit * cup)1667 c7000_send_conn(struct c7000_unit *cup)
1668 {
1669 	int				rc;
1670 	struct	c7000_controller	*ccp = cup->cntlp;
1671 	struct	c7000_control_blk	*ctlblkp = &(cup->control_blk);
1672 
1673 	CPrintk(1, "c7000: c7000_send_conn: send the connection request message for unit 0x%x\n", cup->devno);
1674 
1675 	/*
1676 		Build the connection request control message.
1677 	*/
1678 
1679 	memset(ctlblkp, '\0', sizeof(struct c7000_control_blk));
1680 	ctlblkp->cmd = C7000_CONN_REQ;
1681 	ctlblkp->ver = ccp->version;
1682 	ctlblkp->link_id = 0;
1683 	ctlblkp->correlator = 0;
1684 	ctlblkp->rdsize = C7000_DATAL;
1685 	ctlblkp->wrtsize = C7000_DATAL;
1686 	memcpy(ctlblkp->hostname, ccp->lappl, NAMLEN);
1687 	memcpy(ctlblkp->unitname, ccp->uappl, NAMLEN);
1688 
1689 	/*
1690 		Build the channel program.
1691 	*/
1692 
1693 	c7000_bld_wrtctl_chpgm(cup);
1694 
1695 	/*
1696 		Do the IO and wait for write to complete.
1697 	*/
1698 
1699 	if ((rc = c7000_doio(cup)) != 0) {
1700 		CPrintk(0, "c7000: c7000_send_conn: failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1701 		return(-1);
1702 	}
1703 
1704 	return(0);
1705 }
1706 
1707 /*
1708 	Send a disconnect control message to the link with the value of
1709 	linkid.
1710 */
1711 
1712 static int
c7000_send_disc(struct c7000_unit * cup,int linkid)1713 c7000_send_disc(struct c7000_unit *cup, int linkid)
1714 {
1715 	int				rc;
1716 	struct	c7000_controller	*ccp = cup->cntlp;
1717 	struct	c7000_control_blk	*ctlblkp = &(cup->control_blk);
1718 
1719 	CPrintk(1, "c7000: c7000_send_disc: send disconnect message for unit 0x%x\n", cup->devno);
1720 
1721 	/*
1722 		Build the disconnect control message.
1723 	*/
1724 
1725 	memset(ctlblkp, '\0', sizeof(struct c7000_control_blk));
1726 	ctlblkp->cmd = C7000_DISCONN;
1727 	ctlblkp->ver = ccp->version;
1728 	ctlblkp->link_id = linkid;
1729 	ctlblkp->correlator = 0;
1730 	ctlblkp->rdsize = C7000_DATAL;
1731 	ctlblkp->wrtsize = C7000_DATAL;
1732 	memcpy(ctlblkp->hostname, ccp->lappl, NAMLEN);
1733 	memcpy(ctlblkp->unitname, ccp->uappl, NAMLEN);
1734 
1735 	/*
1736 		Build the channel program.
1737 	*/
1738 
1739 	c7000_bld_wrtctl_chpgm(cup);
1740 
1741 	/*
1742 		Do the IO and wait for write to complete.
1743 	*/
1744 
1745 	if ((rc = c7000_doio(cup)) != 0) {
1746 		CPrintk(0, "c7000: c7000_send_disc: failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1747 		return(-1);
1748 	}
1749 
1750 	return(0);
1751 }
1752 
1753 /*
1754 	Resolve the race condition based on the link identifier value.
1755 	The adapter microcode assigns the identifiers.  A higher value implies
1756 	that the race was lost. A side effect of this function is that
1757 	ccp->linkid is set to the link identifier to be used for this
1758 	connection (provided that 0 is returned).
1759 */
1760 
1761 static int
c7000_resolve_race(struct c7000_unit * cup,int local_linkid,int remote_linkid)1762 c7000_resolve_race(struct c7000_unit *cup, int local_linkid, int remote_linkid)
1763 {
1764 	struct c7000_controller 	*ccp = cup->cntlp;
1765 
1766 	CPrintk(1, "c7000: c7000_resolve_race: for unit 0x%x, local linkid %d, remote linkid %d\n", cup->devno, local_linkid, remote_linkid);
1767 
1768 	/*
1769 		This local link identifier should not be zero..
1770 	*/
1771 
1772 	if (local_linkid == 0) {
1773 		CPrintk(0, "c7000: c7000_resolve_race: error for unit 0x%x, local linkid is null\n", cup->devno);
1774 		return(-1);
1775 	}
1776 
1777 	/*
1778 		This indicates that there is no race.  Just use our
1779 		local link identifier.
1780 	*/
1781 
1782 	if (remote_linkid == 0) {
1783 		ccp->linkid = local_linkid;
1784 		return(0);
1785 	}
1786 
1787 	/*
1788 		Send a connection confirm message if we lost the race to
1789 		the winning link identifier.
1790 
1791 		Either way, save the winning link identifier.
1792 	*/
1793 
1794 	if (local_linkid > remote_linkid) {
1795 
1796 		if (c7000_conn_confrm(&ccp->cunits[C7000_WR], cup->control_blk.correlator, remote_linkid) != 0) {
1797 			CPrintk(0, "c7000: c7000_resolve_race: failed for unit 0x%x\n", cup->devno);
1798 			return(-1);
1799 		}
1800 
1801 		ccp->linkid = remote_linkid;
1802 	} else {
1803 		ccp->linkid = local_linkid;
1804 	}
1805 
1806 	return(0);
1807 }
1808 
1809 /*
1810 	Get connected by processing the connection request/response/confirm
1811 	control messages.  A connection request has already been sent by
1812 	calling function c7000_send_conn.
1813 */
1814 
1815 static int
c7000_get_conn(struct c7000_unit * cup)1816 c7000_get_conn(struct c7000_unit *cup)
1817 {
1818 	struct c7000_controller 	*ccp = cup->cntlp;
1819 	int				rc;
1820 	int				cont = 1;
1821 	int				remote_linkid = 0;
1822 	int				local_linkid = 0;
1823 
1824 	CPrintk(1, "c7000: c7000_get_conn: read the connected message for unit 0x%x\n", cup->devno);
1825 	ccp->linkid = 0;
1826 
1827 	while (cont == 1) {
1828 
1829 		/*
1830 			Build the read channel program.
1831 		*/
1832 
1833 		c7000_bld_readctl_chpgm(cup);
1834 
1835 		/*
1836 			Start the channel program to read a control message.
1837 		*/
1838 
1839 		if ((rc = c7000_doio(cup)) != 0) {
1840 			CPrintk(0, "c7000: c7000_get_conn: failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1841 			return(-1);
1842 		}
1843 
1844 		/*
1845 			Process the control message that was received based
1846 			on the command code.
1847 		*/
1848 
1849 		CPrintk(1, "c7000: c7000_get_conn: received command %d for unit 0x%x\n", cup->control_blk.cmd, cup->devno);
1850 
1851 		switch(cup->control_blk.cmd) {
1852 
1853 			/*
1854 				Save the remote link_id in the message for
1855 				a check in c7000_resolve_race.
1856 			*/
1857 
1858 			case C7000_CONN_REQ:
1859 				remote_linkid = cup->control_blk.link_id;
1860 				break;
1861 
1862 			/*
1863 				A connection response received.  Resolve
1864 				the network race condition (if any) by
1865 				comparing the link identifier values.
1866 			*/
1867 
1868 			case C7000_CONN_RESP:
1869 
1870 				if (cup->control_blk.ret_code != 0) {
1871 					CPrintk(0, "c7000: c7000_get_conn: failed for unit 0x%x , connection response return code %d\n",
1872 						cup->devno, cup->control_blk.ret_code);
1873 					return(-1);
1874 				}
1875 
1876 				local_linkid = cup->control_blk.link_id;
1877 
1878 				if (c7000_resolve_race(cup, local_linkid, remote_linkid) != 0)
1879 					return(-1);
1880 
1881 				break;
1882 
1883 			/*
1884 				Got a confirmation to our connection request.
1885 				Disconnect the remote link identifier (if any).
1886 				Break out of the loop.
1887 			*/
1888 
1889 			case C7000_CONN_CONFRM:
1890 
1891 				if (remote_linkid != 0) {
1892 
1893 					if (c7000_send_disc(&ccp->cunits[C7000_WR], remote_linkid) != 0) {
1894 						CPrintk(0, "c7000: c7000_get_conn: send disconnect failed for unit 0x%x\n", cup->devno);
1895 						return(-1);
1896 					}
1897 
1898 				}
1899 
1900 				cont = 0;
1901 				break;
1902 
1903 			/*
1904 				Got a disconnect to our connection request.
1905 				Break out of the loop.
1906 			*/
1907 
1908 			case C7000_DISCONN:
1909 				cont = 0;
1910 				break;
1911 
1912 			/*
1913 				Anything else must be an error.
1914 				Return with an error immediately.
1915 			*/
1916 
1917 			default:
1918 				CPrintk(0, "c7000: c7000_get_conn: failed for unit 0x%x unexpected command %d\n",
1919 					cup->devno, cup->control_blk.cmd);
1920 				return(-1);
1921 		}
1922 
1923 	}
1924 
1925 	/*
1926 		Be sure that we now have a link identifier.
1927 	*/
1928 
1929 	if (ccp->linkid == 0)
1930 		return(-1);
1931 
1932 	return(0);
1933 }
1934 
1935 /*
1936 	Get statistics method.
1937 */
1938 
1939 struct net_device_stats *
c7000_stats(struct net_device * dev)1940 c7000_stats(struct net_device *dev)
1941 {
1942 	struct	c7000_controller	*ccp = (struct c7000_controller *)dev->priv;
1943 
1944 	return(&ccp->stats);
1945 }
1946 
1947 /*
1948 	Open method.
1949 */
1950 
1951 static int
c7000_open(struct net_device * dev)1952 c7000_open(struct net_device *dev)
1953 {
1954 	int				i;
1955 	struct	c7000_controller	*ccp = (struct c7000_controller *)dev->priv;
1956 	struct	c7000_unit		*cup;
1957 	int				rc;
1958 	unsigned long			parm;
1959 	__u8				flags = 0x00;
1960 
1961 	c7000_set_busy(dev);
1962 
1963 	/*
1964 		Allocate space for the unit buffers.
1965 	*/
1966 
1967 	if (c7000_alloc_buffers(dev) == -1) {
1968 		CPrintk(0, "c7000: c7000_open: can not allocate buffer space for base unit 0x%lx\n", dev->base_addr);
1969 		c7000_free_buffers(dev);  /* free partially allocated buffers */
1970 		c7000_clear_busy(dev);
1971 		return(-ENOMEM);
1972 	}
1973 
1974 	/*
1975 		Perform the initialization for all units.
1976 	*/
1977 
1978 	for (i = 0; i < NUNITS; i++) {
1979 		cup = &ccp->cunits[i];
1980 
1981 		/*
1982 			Initialize task queue structure used for the bottom
1983 			half routine.
1984 		*/
1985 
1986 		cup->tq.sync = 0;
1987 		cup->tq.routine = (void *)(void *)c7000_irq_bh;
1988 		cup->tq.data = cup;
1989 		cup->state = C7000_HALT;
1990 		init_waitqueue_head(&cup->wait);
1991 		CPrintk(1, "c7000: c7000_open: issuing halt to unit 0x%x\n", cup->devno);
1992 
1993 		/*
1994 			Issue a halt I/O to the unit
1995 		*/
1996 
1997 		if ((rc = c7000_haltio(cup)) != 0) {
1998 			CPrintk(0, "c7000: c7000_open: halt_IO failed with rc = %d for unit 0x%x\n", rc, cup->devno);
1999 			continue;
2000 		}
2001 
2002 		cup->IO_active = 0;
2003 		cup->flag_a = 0;
2004 		cup->sigsmod = 0x00;
2005 
2006 		CPrintk(1, "c7000: c7000_open: halt complete for unit 0x%x\n", cup->devno);
2007 	}
2008 
2009 	/*
2010 		On each subchannel send a sense id.
2011 	*/
2012 
2013 	for (i = 0; i < NUNITS; i++) {
2014 		cup = &ccp->cunits[i];
2015 
2016 		/*
2017 			Build SENSE ID channel program.
2018 		*/
2019 
2020 		c7000_bld_senseid_chpgm(cup);
2021 
2022 		/*
2023 			Issue the start I/O for SENSE ID channel program.
2024 		*/
2025 
2026 		CPrintk(1, "c7000: c7000_open: issuing SENSEID to unit 0x%x\n", cup->devno);
2027 
2028 		if ((rc = c7000_doio(cup)) != 0) {
2029 			CPrintk(0, "c7000: c7000_open: SENSEID failed with rc = %d for unit 0x%x\n", rc, cup->devno);
2030 			c7000_clear_busy(dev);
2031 			return(-EIO);
2032 		}
2033 
2034 		CPrintk(1, "c7000: c7000_open: SENSEID complete for unit 0x%x\n", cup->devno);
2035 	}
2036 
2037 	/*
2038 		Send the system validation control message.
2039 	*/
2040 
2041 	cup = &ccp->cunits[C7000_WR];
2042 
2043 	if (c7000_send_sysval(cup) != 0) {
2044 		CPrintk(0, "c7000: c7000_open: can not send sysval for unit 0x%x\n", cup->devno);
2045 		c7000_clear_busy(dev);
2046 		return(-EIO);
2047 	}
2048 
2049 	CPrintk(1, "c7000: c7000_open: successfully sent sysval for unit 0x%x\n", cup->devno);
2050 	/*
2051 		Get the system validation response message.
2052 	*/
2053 
2054 	cup = &ccp->cunits[C7000_RD];
2055 
2056 	if (c7000_get_sysval_resp(cup) != 0) {
2057 		CPrintk(0, "c7000: c7000_open: can not read sysval response for unit 0x%x\n", cup->devno);
2058 		c7000_clear_busy(dev);
2059 		return(-EIO);
2060 	}
2061 
2062 	CPrintk(1, "c7000: c7000_open: successfully received sysval reply for unit 0x%x\n", cup->devno);
2063 	ccp->cunits[C7000_RD].state = ccp->cunits[C7000_WR].state = C7000_CONNECT;
2064 
2065 	cup = &ccp->cunits[C7000_WR];
2066 
2067 	/*
2068 		Send a connection request.
2069 	*/
2070 
2071 	if (c7000_send_conn(cup) != 0) {
2072 		CPrintk(0, "c7000: c7000_open: connection failed for unit 0x%x\n", cup->devno);
2073 		c7000_clear_busy(dev);
2074 		return(-EIO);
2075 	}
2076 
2077 	cup = &ccp->cunits[C7000_RD];
2078 
2079 	/*
2080 		Get the response to our connection request Note that a
2081 		network race may occur.  This is handled in c7000_get_conn.
2082 	*/
2083 
2084 	if (c7000_get_conn(cup) != 0) {
2085 		CPrintk(0, "c7000: c7000_open: unit 0x%x has connected\n", cup->devno);
2086 		c7000_clear_busy(dev);
2087 		return(-EIO);
2088 	}
2089 
2090 	CPrintk(1, "c7000: c7000_open: successfully received connection request for unit 0x%x\n", cup->devno);
2091 	ccp->cunits[C7000_RD].state = ccp->cunits[C7000_WR].state = C7000_READY;
2092 
2093 	/*
2094 		Clear the interface statistics.
2095 	*/
2096 
2097 	memset(&ccp->stats, '\0', sizeof(struct net_device_stats));
2098 
2099 	if ((rc = c7000_bld_read_chain(cup)) != 0) {
2100 		c7000_clear_busy(dev);
2101 		return(rc);
2102 	}
2103 
2104 	/*
2105 		Start the C7000_READ channel program but do not wait for it's
2106 		completion.
2107 	*/
2108 
2109 	cup->state = C7000_READ;
2110 	parm = (unsigned long)cup;
2111 	set_bit(0, (void *)&cup->IO_active);
2112 
2113 	if ((rc = do_IO(cup->irq, &cup->proc_head->ccws[0], parm, 0xff, flags)) != 0) {
2114 		CPrintk(0, "c7000: c7000_open: READ failed with return code %d for unit 0x%x\n", rc, cup->devno);
2115 		c7000_error(cup->cntlp);
2116 		clear_bit(0, (void *)&cup->IO_active);
2117 		c7000_clear_busy(dev);
2118 		return(-EIO);
2119 	}
2120 
2121 	netif_start_queue(dev);
2122 	CPrintk(0, "c7000: c7000_open: base unit 0x%lx is opened\n", dev->base_addr);
2123 	c7000_clear_busy(dev);
2124 	MOD_INC_USE_COUNT;	/* increment module usage count */
2125 	return(0);
2126 }
2127 
2128 /*
2129 	Stop method.
2130 */
2131 
2132 static int
c7000_stop(struct net_device * dev)2133 c7000_stop(struct net_device *dev)
2134 {
2135 	int				i;
2136 	struct	c7000_controller	*ccp = (struct c7000_controller *)dev->priv;
2137 	struct	c7000_unit		*cup;
2138 	int				rc;
2139 
2140 	c7000_set_busy(dev);
2141 
2142 	/*
2143 		Send a disconnect message.
2144 	*/
2145 
2146 	ccp->cunits[C7000_RD].state = ccp->cunits[C7000_WR].state = C7000_DISC;
2147 	cup = &ccp->cunits[C7000_WR];
2148 
2149 	if (c7000_send_disc(cup, ccp->linkid) != 0) {
2150 		CPrintk(0, "c7000: c7000_stop: send of disconnect message failed for unit 0x%x\n", cup->devno);
2151 	}
2152 
2153 	CPrintk(1, "c7000: c7000_stop: successfully sent disconnect message to unit 0x%x\n", cup->devno);
2154 
2155 	/*
2156 		Issue a halt I/O to all units.
2157 	*/
2158 
2159 	for (i = 0; i < NUNITS; i++) {
2160 		cup = &ccp->cunits[i];
2161 		cup->state = C7000_STOP;
2162 		CPrintk(1, "c7000: c7000_stop: issuing halt to unit 0x%x\n", cup->devno);
2163 
2164 		if ((rc = c7000_haltio(cup)) != 0) {
2165 			CPrintk(0, "c7000: c7000_stop: halt_IO failed with rc = %d for unit 0x%x\n", rc, cup->devno);
2166 			continue;
2167 		}
2168 
2169 		CPrintk(1, "c7000: c7000_stop: halt complete for unit 0x%x\n", cup->devno);
2170 	}
2171 
2172 	c7000_free_buffers(dev);
2173 	CPrintk(0, "c7000: c7000_stop: base unit 0x%lx is stopped\n", dev->base_addr);
2174 	MOD_DEC_USE_COUNT;	/* Decrement module usage count */
2175 	return(0);
2176 }
2177 
2178 /*
2179 	Configure the interface.
2180 */
2181 
2182 static int
c7000_config(struct net_device * dev,struct ifmap * map)2183 c7000_config(struct net_device *dev, struct ifmap *map)
2184 {
2185 	CPrintk(1, "c7000: c7000_config: entered for base unit 0x%lx\n", dev->base_addr);
2186 	return(0);
2187 }
2188 
2189 /*
2190 	Transmit a packet.
2191 */
2192 
2193 static int
c7000_xmit(struct sk_buff * skb,struct net_device * dev)2194 c7000_xmit(struct sk_buff *skb, struct net_device *dev)
2195 {
2196 	struct	c7000_controller	*ccp = (struct c7000_controller *)dev->priv;
2197 	struct	c7000_unit		*cup;
2198 	unsigned long			saveflags;
2199 	unsigned long			parm;
2200 	__u8				flags = 0x00;
2201 	struct	c7000_buffer		*buf, *pbuf;
2202 	int				rc;
2203 
2204 	CPrintk(1, "c7000: c7000_xmit: entered for base unit 0x%lx\n", dev->base_addr);
2205 
2206 	/*
2207 		When the skb pointer is NULL return.
2208 	*/
2209 
2210 	if (skb == NULL) {
2211 		CPrintk(0, "c7000: c7000_xmit: skb pointer is null for base unit 0x%lx\n", dev->base_addr);
2212 		ccp->stats.tx_dropped++;
2213 		return(-EIO);
2214 	}
2215 
2216 	cup = &ccp->cunits[C7000_WR];
2217 
2218 	/*
2219 		Lock the irq.
2220 	*/
2221 
2222 	s390irq_spin_lock_irqsave(cup->irq, saveflags);
2223 
2224 	/*
2225 		When the device transmission busy flag is on , no data
2226 		can be sent.  Unlock the irq and return EBUSY.
2227 	*/
2228 
2229 	if (c7000_check_busy(dev)) {
2230 		CPrintk(1, "c7000: c7000_xmit: c7000_check_busy returns true for base unit 0x%lx\n", dev->base_addr);
2231 		s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
2232 		return(-EBUSY);
2233 	}
2234 
2235 	/*
2236 		Set the device transmission busy flag on atomically.
2237 	*/
2238 
2239 	if (c7000_ts_busy(TB_TX, dev)) {
2240 		CPrintk(1, "c7000: c7000_xmit: c7000_ts_busy returns true for base unit 0x%lx\n", dev->base_addr);
2241 		s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
2242 		return(-EBUSY);
2243 	}
2244 
2245 	CPrintk(1, "c7000: c7000_xmit: set TB_TX for unit 0x%x\n", cup->devno);
2246 
2247 	/*
2248 		Obtain a free buffer.  If none are free then mark tbusy
2249 		with TB_NOBUFFER and return EBUSY.
2250 	*/
2251 
2252 	if ((buf = c7000_get_buffer(cup)) == NULL) {
2253 		CPrintk(1, "c7000: c7000_xmit: setting TB_NOBUFFER for base unit 0x%lx\n", dev->base_addr);
2254 		c7000_setbit_busy(TB_NOBUFFER, dev);
2255 		c7000_clearbit_busy(TB_TX, dev);
2256 		s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
2257 		return(-EBUSY);
2258 	}
2259 
2260 	CPrintk(1, "c7000: c7000_xmit: Got buffer for unit 0x%x\n", cup->devno);
2261 
2262 	/*
2263 		Save the length of the skb data and then copy it to the
2264 		buffer.  Queue the buffer on the processing list.
2265 	*/
2266 
2267 	buf->len = skb->len;
2268 	memcpy(buf->data, skb->data, skb->len);
2269 	memset(buf->data + C7000_DATAL + C7000_READHDRL, '\0', C7000_READFFL);
2270 	pbuf = cup->proc_tail;
2271 	c7000_queue_buffer(cup, buf);
2272 
2273 	/*
2274 		Chain the buffer to the running channel program.
2275 	*/
2276 
2277 	if (test_bit(0, (void *)&cup->IO_active) && pbuf != NULL) {
2278 		c7000_bld_wrt_chpgm(cup, buf);
2279 		pbuf->ccws[2].cda = (__u32)virt_to_phys(&buf->ccws[0]);
2280 	}
2281 
2282 	/*
2283 		Free the socket buffer.
2284 	*/
2285 
2286 	dev_kfree_skb(skb);
2287 
2288 	/*
2289 		If the unit is not currently doing IO, build a channel
2290 		program and start the IO for the buffers on the processing
2291 		chain.
2292 	*/
2293 
2294 	if (test_and_set_bit(0, (void *)&cup->IO_active) == 0) {
2295 		CPrintk(1, "c7000: c7000_xmit: start IO for unit 0x%x\n", cup->devno);
2296 		c7000_bld_wrt_chain(cup);
2297 		parm = (unsigned long)cup;
2298 		cup->state = C7000_WRITE;
2299 
2300 		if ((rc = do_IO(cup->irq, &cup->proc_head->ccws[0], parm, 0xff, flags)) != 0) {
2301 			CPrintk(0, "c7000: c7000_xmit: do_IO failed with return code %d for unit 0x%x\n", rc, cup->devno);
2302 			c7000_error(cup->cntlp);
2303 			c7000_clearbit_busy(TB_TX, dev);
2304 			s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
2305 			return(-EIO);
2306 		}
2307 
2308 		dev->trans_start = jiffies;
2309 		CPrintk(1, "c7000: c7000_xmit: do_IO succeeds for unit 0x%x\n", cup->devno);
2310 	}
2311 
2312 	/*
2313 		If the free chain is now NULL, set the TB_NOBUFFER flag.
2314 	*/
2315 
2316 	if (cup->free == NULL) {
2317 		CPrintk(1, "c7000: c7000_xmit: setting TB_NOBUFFER for base unit 0x%lx\n", dev->base_addr);
2318 		c7000_setbit_busy(TB_NOBUFFER, dev);
2319 	}
2320 
2321 	c7000_clearbit_busy(TB_TX, dev);
2322 	s390irq_spin_unlock_irqrestore(cup->irq, saveflags);
2323 	CPrintk(1, "c7000: c7000_xmit: exits for unit 0x%x\n", cup->devno);
2324 	return(0);
2325 }
2326 
2327 /*
2328 	Handle an ioctl from a user process.
2329 */
2330 
2331 static int
c7000_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)2332 c7000_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2333 {
2334 	CPrintk(1, "c7000: c7000_ioctl: entered for base unit 0x%lx with cmd %d\n", dev->base_addr, cmd);
2335 	return(0);
2336 }
2337 
2338 /*
2339 	Analyze the interrupt status and return a value
2340 	that identifies the type.
2341 */
2342 
2343 static enum c7000_rupt
c7000_check_csw(devstat_t * devstat)2344 c7000_check_csw(devstat_t *devstat)
2345 {
2346 
2347 	/*
2348 		Check for channel detected conditions (except PCI).
2349 	*/
2350 
2351 	if ((devstat->cstat & ~SCHN_STAT_PCI) != 0) {
2352 		CPrintk(0, "c7000: c7000_check_csw: channel status 0x%x for unit 0x%x\n", devstat->cstat, devstat->devno);
2353 		return(C7000_CHANERR);
2354 	}
2355 
2356 	/*
2357 		Fast path the normal cases.
2358 	*/
2359 
2360 	if (devstat->dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
2361 		return(C7000_NORMAL);
2362 
2363 	if (devstat->cstat == SCHN_STAT_PCI)
2364 		return(C7000_NORMAL);
2365 
2366 	/*
2367 		Check for exceptions.
2368 	*/
2369 
2370 	if (devstat->dstat & DEV_STAT_UNIT_CHECK) {
2371 		CPrintk(0, "c7000: c7000_check_csw: unit check for unit 0x%x, sense byte0 0x%2.2x\n", devstat->devno, devstat->ii.sense.data[0]);
2372 
2373 		if (devstat->ii.sense.data[0] == C7000_BOX_RESET)
2374 			return(C7000_UCK_RESET);
2375 		else
2376 			return(C7000_UCK);
2377 
2378 	} else if (devstat->dstat & DEV_STAT_UNIT_EXCEP) {
2379 		CPrintk(0, "c7000: c7000_check_csw: unit exception for unit 0x%x\n", devstat->devno);
2380 		return(C7000_UE);
2381 
2382 	} else if (devstat->dstat & DEV_STAT_ATTENTION) {
2383 		CPrintk(0, "c7000: c7000_check_csw: attention for unit 0x%x\n", devstat->devno);
2384 		return(C7000_ATTN);
2385 
2386 	} else if (devstat->dstat & DEV_STAT_BUSY) {
2387 		CPrintk(0, "c7000: c7000_check_csw: busy for unit 0x%x\n", devstat->devno);
2388 		return(C7000_BUSY);
2389 
2390 	} else {
2391 		CPrintk(0, "c7000: c7000_check_csw: channel status 0x%2.2x , device status 0x%2.2x, devstat flags 0x%8.8x for unit 0x%x\n",
2392 			devstat->cstat, devstat->dstat, devstat->flag, devstat->devno);
2393 		return(C7000_OTHER);
2394 	}
2395 
2396 	/* NOT REACHED */
2397 
2398 }
2399 
2400 /*
2401 	Retry the last CCW chain to the unit.
2402 */
2403 
2404 static void
c7000_retry_io(struct c7000_unit * cup)2405 c7000_retry_io(struct c7000_unit *cup)
2406 {
2407 	int	rc;
2408 	unsigned long	parm;
2409 	__u8	flags = 0x00;
2410 	ccw1_t	*ccwp;
2411 
2412 	if (++cup->retries > C7000_MAX_RETRIES) {
2413 		c7000_error(cup->cntlp);
2414 		CPrintk(0, "c7000: c7000_retry_io: retry IO for unit 0x%x exceeds maximum retry count\n", cup->devno);
2415 		return;
2416 	}
2417 
2418 	set_bit(0, (void *)&cup->IO_active);
2419 	parm = (unsigned long)cup;
2420 
2421 	if (cup->state == C7000_READ || cup->state == C7000_WRITE)
2422 		ccwp = &cup->proc_head->ccws[0];
2423 	else
2424 		ccwp = &cup->ccws[0];
2425 
2426 	if ((rc = do_IO(cup->irq, ccwp, parm, 0xff, flags)) != 0) {
2427 		CPrintk(0, "c7000: c7000_retry_io: can not retry IO for unit 0x%x, return code %d\n", cup->devno, rc);
2428 		clear_bit(0, (void *)&cup->IO_active);
2429 		c7000_error(cup->cntlp);
2430 	}
2431 
2432 	CPrintk(1, "c7000: c7000_retry_io: retry IO for unit 0x%x, retry count %d\n", cup->devno, cup->retries);
2433 	return;
2434 }
2435 
2436 /*
2437 	Process a read interrupt by scanning the list of buffers
2438 	for ones that have completed and queue them for the bottom
2439 	half to process.
2440 */
2441 
2442 static void
c7000_proc_rintr(struct c7000_unit * cup)2443 c7000_proc_rintr(struct c7000_unit *cup)
2444 {
2445 	struct	c7000_buffer	*buf;
2446 	struct	c7000_rd_header	*head;
2447 	int			num_read = 0;
2448 
2449 	while (cup->proc_head != NULL) {
2450 		head = (struct c7000_rd_header *)(cup->proc_head->data + C7000_DATAL);
2451 
2452 		/*
2453 			The flag byte in the read header will be set to
2454 			FLAG_FF when the buffer has been read.
2455 		*/
2456 
2457 		if (head->flag != FLAG_FF)
2458 			break;
2459 
2460 		/*
2461 			Dequeue the buffer from the proc chain
2462 			and enqueue it on the bh chain for
2463 			the bh routine to process.
2464 		*/
2465 
2466 		buf = c7000_dequeue_buffer(cup);
2467 		c7000_queue_bh_buffer(cup, buf);
2468 		num_read++;
2469 	}
2470 
2471 	CPrintk(1, "c7000: c7000_proc_rintr: %d buffers read for unit 0x%x\n", num_read, cup->devno);
2472 	return;
2473 }
2474 
2475 /*
2476 	Process all completed buffers on the proc chain.
2477 	A buffer is completed if it's READFF flag is FLAG_FF.
2478 */
2479 
2480 static int
c7000_proc_wintr(struct c7000_unit * cup)2481 c7000_proc_wintr(struct c7000_unit *cup)
2482 {
2483 	struct	c7000_controller	*ccp = cup->cntlp;
2484 	struct	c7000_buffer		*buf;
2485 	int				num_write = 0;
2486 
2487 	if (cup->proc_head == NULL) {
2488 		CPrintk(0, "c7000: c7000_proc_wintr: unexpected NULL processing chain pointer for unit 0x%x\n", cup->devno);
2489 		return(num_write);
2490 	}
2491 
2492 	while (cup->proc_head != NULL) {
2493 
2494 		/*
2495 			Check if the buffer has completed.
2496 		*/
2497 
2498 		if (*(cup->proc_head->data + C7000_DATAL + C7000_READHDRL) != FLAG_FF)
2499 			break;
2500 
2501 		/*
2502 			Remove buffer from top of processing chain.
2503 			Place it on free list.
2504 		*/
2505 
2506 		buf = c7000_dequeue_buffer(cup);
2507 		ccp->stats.tx_bytes += buf->len;
2508 		ccp->stats.tx_packets++;
2509 		c7000_release_buffer(cup, buf);
2510 		num_write++;
2511 	}
2512 
2513 	CPrintk(1, "c7000: c7000_proc_wintr: %d buffers written for unit 0x%x\n", num_write, cup->devno);
2514 	return(num_write);
2515 }
2516 
2517 /*
2518 	Interrupt handler.
2519 */
2520 
2521 static void
c7000_intr(int irq,void * initparm,struct pt_regs * regs)2522 c7000_intr(int irq, void *initparm, struct pt_regs *regs)
2523 {
2524 	devstat_t			*devstat = ((devstat_t *) initparm);
2525 	struct	c7000_unit		*cup = NULL;
2526 	struct	c7000_controller	*ccp = NULL;
2527 	struct	net_device		*dev = NULL;
2528 	unsigned long			parm;
2529 	__u8				flags = 0x00;
2530 	int				rc;
2531 
2532 	/*
2533 		Discard unsolicited interrupts
2534 	*/
2535 
2536 	if (devstat->intparm == 0) {
2537 		CPrintk(0, "c7000: c7000_intr: unsolicited interrupt for device 0x%x, cstat = 0x%2.2x, dstat = 0x%2.2x, flag = 0x%8.8x\n",
2538 			devstat->devno, devstat->cstat, devstat->dstat, devstat->flag);
2539 		return;
2540 	}
2541 
2542 	/*
2543 		Obtain the c7000_unit structure pointer.
2544 	*/
2545 
2546 	cup = (struct c7000_unit *)(devstat->intparm);
2547 
2548 	/*
2549 		Obtain the c7000_controller structure and device structure
2550 		pointers.
2551 	*/
2552 
2553 	if (cup == NULL) {
2554 		CPrintk(0, "c7000: c7000_intr: c7000_unit pointer is NULL in devstat\n");
2555 		return;
2556 	}
2557 
2558 	ccp = cup->cntlp;
2559 
2560 	if (ccp == NULL) {
2561 		CPrintk(0, "c7000: c7000_intr: c7000_cntlp pointer is NULL in c7000_unit structure %p for unit 0x%x\n", cup, cup->devno);
2562 		return;
2563 	}
2564 
2565 	dev = ccp->dev;
2566 
2567 	if (dev == NULL) {
2568 		CPrintk(0, "c7000: c7000_intr: device pointer is NULL in c7000_controller structure %p for unit 0x%x\n", ccp, cup->devno);
2569 		return;
2570 	}
2571 
2572 	/*
2573 		Finite state machine (fsm) handling.
2574 	*/
2575 
2576 	CPrintk(1, "c7000: c7000_intr: entered with state %d flag 0x%8.8x for unit 0x%x\n", cup->state, devstat->flag, cup->devno);
2577 
2578 	switch(cup->state) {
2579 
2580 		/*
2581 			Not expected to be here when in INIT state.
2582 		*/
2583 
2584 		case C7000_INIT:
2585 
2586 			break;
2587 
2588 		/*
2589 			Enter state C7000_SID and wakeup the sleeping
2590 			process in c7000_open.
2591 		*/
2592 
2593 		case C7000_HALT:
2594 
2595 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2596 				break;
2597 
2598 			cup->state = C7000_SID;
2599 			wake_up(&cup->wait);
2600 			break;
2601 
2602 		/*
2603 			Enter state C7000_SYSVAL and wakeup the sleeping
2604 			process in c7000_open.
2605 		*/
2606 
2607 		case C7000_SID:
2608 
2609 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2610 				break;
2611 
2612 			if (c7000_check_csw(devstat) != 0) {
2613 				c7000_retry_io(cup);
2614 
2615 				if (cup->state == C7000_ERROR)
2616 					wake_up(&cup->wait);
2617 
2618 				break;
2619 			}
2620 
2621 			cup->retries = 0;
2622 			cup->state = C7000_SYSVAL;
2623 			wake_up(&cup->wait);
2624 			break;
2625 
2626 		/*
2627 			Wakeup the sleeping process in c7000_open.
2628 		*/
2629 
2630 		case C7000_SYSVAL:
2631 
2632 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2633 				break;
2634 
2635 			if (c7000_check_csw(devstat) != 0) {
2636 				c7000_retry_io(cup);
2637 
2638 				if (cup->state == C7000_ERROR)
2639 					wake_up(&cup->wait);
2640 
2641 				break;
2642 			}
2643 
2644 			cup->retries = 0;
2645 			wake_up(&cup->wait);
2646 			break;
2647 
2648 		/*
2649 			Wakeup the sleeping process in c7000_open.
2650 		*/
2651 
2652 		case C7000_CONNECT:
2653 
2654 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2655 				break;
2656 
2657 			if (c7000_check_csw(devstat) != 0) {
2658 				c7000_retry_io(cup);
2659 
2660 				if (cup->state == C7000_ERROR)
2661 					wake_up(&cup->wait);
2662 
2663 				break;
2664 			}
2665 
2666 			cup->retries = 0;
2667 			wake_up(&cup->wait);
2668 			break;
2669 
2670 		/*
2671 			Not expected to be entered here.
2672 		*/
2673 
2674 		case C7000_READY:
2675 			break;
2676 
2677 		/*
2678 			Process the data that was just read.
2679 		*/
2680 
2681 		case C7000_READ:
2682 
2683 			if ((devstat->flag & (DEVSTAT_PCI | DEVSTAT_FINAL_STATUS)) == 0)
2684 				break;
2685 
2686 			CPrintk(1, "c7000: c7000_intr: process read interrupt for unit 0x%x , devstat flag = 0x%8.8x\n", cup->devno, devstat->flag);
2687 
2688 			/*
2689 				Check for serious errors.
2690 			*/
2691 
2692 			if (c7000_check_csw(devstat) != 0) {
2693 				ccp->stats.rx_errors++;
2694 				c7000_error(cup->cntlp);
2695 				break;
2696 			}
2697 
2698 			/*
2699 				Build the bottom half buffer list.
2700 			*/
2701 
2702 			c7000_proc_rintr(cup);
2703 
2704 			/*
2705 				When final status is received clear
2706 				the IO active bit.
2707 			*/
2708 
2709 			if (devstat->flag & DEVSTAT_FINAL_STATUS) {
2710 				clear_bit(0, (void *)&cup->IO_active);
2711 			}
2712 
2713 			/*
2714 				If there are free buffers redrive the IO.
2715 			*/
2716 
2717 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) &&
2718 			    (cup->free != NULL)) {
2719 				c7000_bld_read_chain(cup);
2720 				parm = (unsigned long)cup;
2721 				set_bit(0, (void *)&cup->IO_active);
2722 
2723 				if ((rc = do_IO(cup->irq, &cup->proc_head->ccws[0], parm, 0xff, flags)) != 0) {
2724 					clear_bit(0, (void *)&cup->IO_active);
2725 					CPrintk(0, "c7000: c7000_intr: do_IO failed with return code %d for unit 0x%x\n", rc, cup->devno);
2726 					c7000_error(cup->cntlp);
2727 					break;
2728 				}
2729 
2730 				CPrintk(1, "c7000: c7000_intr: started read io for unit 0x%x\n", cup->devno);
2731 			}
2732 
2733 			/*
2734 				Initiate bottom half routine to process
2735 				data that was read.
2736 			*/
2737 
2738 			if (test_and_set_bit(C7000_BH_ACTIVE, (void *)&cup->flag_a) == 0) {
2739 				queue_task(&cup->tq, &tq_immediate);
2740 				mark_bh(IMMEDIATE_BH);
2741 			}
2742 
2743 			break;
2744 
2745 		/*
2746 			Free the transmitted buffers and restart the channel
2747 			process (if necessary).
2748 		*/
2749 
2750 		case C7000_WRITE:
2751 
2752 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2753 				break;
2754 
2755 			if (c7000_check_csw(devstat) != 0) {
2756 				ccp->stats.tx_errors++;
2757 				c7000_error(cup->cntlp);
2758 				break;
2759 			}
2760 
2761 			/*
2762 				If at least one buffer was freed, clear
2763 				the NOBUFFER indication.
2764 			*/
2765 
2766 			if (c7000_proc_wintr(cup) != 0) {
2767 				c7000_clearbit_busy(TB_NOBUFFER, dev);
2768 			}
2769 
2770 			/*
2771 				Restart the channel program if there are more
2772 				buffers on the processing chain.
2773 			*/
2774 
2775 			if (cup->proc_head != NULL) {
2776 				c7000_bld_wrt_chain(cup);
2777 				parm = (unsigned long)cup;
2778 				set_bit(0, (void *)&cup->IO_active);
2779 
2780 				if ((rc = do_IO(cup->irq, &cup->proc_head->ccws[0], parm, 0xff, flags)) != 0) {
2781 					CPrintk(0, "c7000: c7000_intr: do_IO failed with return code %d for unit 0x%x\n", rc, cup->devno);
2782 					clear_bit(0, (void *)&cup->IO_active);
2783 					c7000_error(cup->cntlp);
2784 					break;
2785 				}
2786 
2787 				dev->trans_start = jiffies;
2788 			} else {
2789 				clear_bit(0, (void *)&cup->IO_active);
2790 				cup->state = C7000_READY;
2791 			}
2792 
2793 			break;
2794 
2795 		/*
2796 			Disconnect message completed.  Wakeup the
2797 			sleeping process in c7000_stop.
2798 		*/
2799 
2800 		case C7000_DISC:
2801 			if ((devstat->flag & DEVSTAT_FINAL_STATUS) == 0)
2802 				break;
2803 
2804 			if (c7000_check_csw(devstat) != 0) {
2805 				c7000_retry_io(cup);
2806 
2807 				if (cup->state == C7000_ERROR)
2808 					wake_up(&cup->wait);
2809 
2810 				break;
2811 			}
2812 
2813 			cup->retries = 0;
2814 			wake_up(&cup->wait);
2815 			break;
2816 
2817 		/*
2818 			Subchannel is now halted.  Wakeup the sleeping
2819 			process in c7000_stop.  Set the state to C7000_STOPPED.
2820 		*/
2821 
2822 		case C7000_STOP:
2823 
2824 			cup->state = C7000_STOPPED;
2825 			wake_up(&cup->wait);
2826 			break;
2827 
2828 		/*
2829 			When in error state, stay there until the
2830 			interface is recycled.
2831 		*/
2832 
2833 		case C7000_ERROR:
2834 
2835 			break;
2836 
2837 		/*
2838 			Should not reach here
2839 		*/
2840 
2841 		default:
2842 			CPrintk(0, "c7000: c7000_intr: entered default case for unit 0x%x, state %d\n", cup->devno, cup->state);
2843 			break;
2844 
2845 	}
2846 
2847 	CPrintk(1, "c7000: c7000_intr: exited with state %d for unit 0x%x\n", cup->state, cup->devno);
2848 	return;
2849 }
2850 
2851 /*
2852 	Fill in system validation name padding it with blanks.
2853 */
2854 
2855 static void
c7000_fill_name(char * dst,char * src)2856 c7000_fill_name(char *dst, char *src)
2857 {
2858 	char	*tmp = dst;
2859 	int	i;
2860 
2861 	for (i = 0; i < NAMLEN; i++, tmp++)
2862 		*tmp = ' ';
2863 
2864 	for (i = 0; i < NAMLEN && *src != '\0'; i++)
2865 		*dst++ = *src++;
2866 
2867 	return;
2868 }
2869 
2870 /*
2871 	Initialization routine called when the device is registered.
2872 */
2873 
2874 static int
c7000_init(struct net_device * dev)2875 c7000_init(struct net_device *dev)
2876 {
2877 	struct	c7000_controller	*ccp;
2878 	int				i;
2879 	int				unitaddr;
2880 	int				irq;
2881 
2882 	/*
2883 		Find the position of base_addr in the bases array.
2884 	*/
2885 
2886 	for (i = 0; i < MAX_C7000; i++)
2887 		if (bases[i] == dev->base_addr)
2888 			break;
2889 
2890 	if (i == MAX_C7000)
2891 		return(-ENODEV);
2892 
2893 	/*
2894 		Make sure it is a C7000 type of device.
2895 	*/
2896 
2897 	if (c7000_check_devices(dev->base_addr) != 0) {
2898 		CPrintk(0, "c7000: c7000_init: base unit 0x%lx is not the right type\n", dev->base_addr);
2899 		return(-ENODEV);
2900 	}
2901 
2902 	/*
2903 		Initialize the device structure functions.
2904 		Note that ARP is not done on the CLAW interface.
2905 		There is no ethernet header.
2906 	*/
2907 
2908 	dev->mtu = C7000_DATAL;
2909 	dev->hard_header_len = 0;
2910         dev->addr_len = 0;
2911         dev->type = ARPHRD_SLIP;
2912         dev->tx_queue_len = C7000_TXQUEUE_LEN;
2913 	dev->flags = IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
2914 	dev->open = c7000_open;
2915 	dev->stop = c7000_stop;
2916 	dev->set_config = c7000_config;
2917 	dev->hard_start_xmit = c7000_xmit;
2918 	dev->do_ioctl = c7000_ioctl;
2919 	dev->get_stats = c7000_stats;
2920 
2921 	/*
2922 		Allocate space for a private data structure.
2923 	*/
2924 
2925 	if ((ccp = dev->priv = kmalloc(sizeof(struct c7000_controller), GFP_KERNEL)) == NULL) {
2926 		CPrintk(0, "c7000: c7000_init: base unit 0x%lx can not be initialized\n", dev->base_addr);
2927 		return(-ENOMEM);
2928 	}
2929 
2930 	CPrintk(1, "c7000: c7000_init: allocated a c7000_controller structure at address %p\n", ccp);
2931 	memset(ccp, '\0', sizeof(struct c7000_controller));
2932 	ccp->dev = dev;
2933 	ccp->base_addr = dev->base_addr;
2934 
2935 	/*
2936 		Populate the system validation name with default values.
2937 	*/
2938 
2939 	c7000_fill_name(ccp->lappl, C7000_DFLT_LAPPL);
2940 	c7000_fill_name(ccp->lhost, C7000_DFLT_LHOST);
2941 	c7000_fill_name(ccp->uappl, C7000_DFLT_UAPPL);
2942 	c7000_fill_name(ccp->uhost, C7000_DFLT_UHOST);
2943 
2944 	/*
2945 		When values have been supplied, replace the prior defaults.
2946 	*/
2947 
2948 	if (i == 0) {
2949 
2950 		if (lappl0 != NULL)
2951 			c7000_fill_name(ccp->lappl, lappl0);
2952 
2953 		if (lhost0 != NULL)
2954 			c7000_fill_name(ccp->lhost, lhost0);
2955 
2956 		if (uappl0 != NULL)
2957 			c7000_fill_name(ccp->uappl, uappl0);
2958 
2959 		if (uhost0 != NULL)
2960 			c7000_fill_name(ccp->uhost, uhost0);
2961 
2962 	} else if (i == 1) {
2963 
2964 		if (lappl1 != NULL)
2965 			c7000_fill_name(ccp->lappl, lappl1);
2966 
2967 		if (lhost1 != NULL)
2968 			c7000_fill_name(ccp->lhost, lhost1);
2969 
2970 		if (uappl1 != NULL)
2971 			c7000_fill_name(ccp->uappl, uappl1);
2972 
2973 		if (uhost1 != NULL)
2974 			c7000_fill_name(ccp->uhost, uhost1);
2975 
2976 	} else if (i == 2) {
2977 
2978 		if (lappl2 != NULL)
2979 			c7000_fill_name(ccp->lappl, lappl2);
2980 
2981 		if (lhost2 != NULL)
2982 			c7000_fill_name(ccp->lhost, lhost2);
2983 
2984 		if (uappl2 != NULL)
2985 			c7000_fill_name(ccp->uappl, uappl2);
2986 
2987 		if (uhost2 != NULL)
2988 			c7000_fill_name(ccp->uhost, uhost2);
2989 
2990 	} else {
2991 
2992 		if (lappl3 != NULL)
2993 			c7000_fill_name(ccp->lappl, lappl3);
2994 
2995 		if (lhost3 != NULL)
2996 			c7000_fill_name(ccp->lhost, lhost3);
2997 
2998 		if (uappl3 != NULL)
2999 			c7000_fill_name(ccp->uappl, uappl3);
3000 
3001 		if (uhost3 != NULL)
3002 			c7000_fill_name(ccp->uhost, uhost3);
3003 
3004 	}
3005 
3006 	CPrintk(1, "c7000: c7000_init: lappl = %8.8s lhost = %8.8s uappl = %8.8s uhost = %8.8s for base unit 0x%x\n", ccp->lappl, ccp->lhost, ccp->uappl, ccp->uhost, ccp->base_addr);
3007 	ccp->version = 2;
3008 	ccp->linkid = 0;
3009 
3010 	/*
3011 		Initialize the fields in the embedded cunits
3012 		array.  This type of controller occupies a range
3013 		of three contiguous device numbers.
3014 	*/
3015 
3016 	for (i = 0; i < NUNITS; i++) {
3017 		unitaddr = dev->base_addr + i;
3018 
3019 		/*
3020 			Get the subchannel number.
3021 		*/
3022 
3023 		if ((irq = ccp->cunits[i].irq = get_irq_by_devno(unitaddr)) == -1) {
3024 			CPrintk(0, "c7000: c7000_init: can not get subchannel for unit 0x%x\n", unitaddr);
3025 			return(-ENODEV);
3026 		}
3027 
3028 		/*
3029 			Get control of the subchannel.
3030 		*/
3031 
3032 		if (request_irq(irq, c7000_intr, SA_INTERRUPT, dev->name, &ccp->cunits[i].devstat) != 0) {
3033 			CPrintk(0, "c7000: c7000_init: can not get control of subchannel 0x%x for unit 0x%x\n", irq, unitaddr);
3034 			return(-EBUSY);
3035 		}
3036 
3037 		CPrintk(1, "c7000: c7000_init: obtained control of subchannel 0x%x for unit 0x%x\n", irq, unitaddr);
3038 		ccp->cunits[i].devno = unitaddr;
3039 		ccp->cunits[i].IO_active = 0;
3040 		ccp->cunits[i].state = C7000_INIT;
3041 		ccp->cunits[i].cntlp = ccp;
3042 		CPrintk(1, "c7000: c7000_init: initialized unit 0x%x on subchannel 0x%x\n", unitaddr, irq);
3043 	}
3044 
3045 	return(0);
3046 }
3047 
3048 /*
3049 	Probe for the Cisco 7000 unit base addresses.
3050 */
3051 
3052 static void
c7000_probe(void)3053 c7000_probe(void)
3054 {
3055 	s390_dev_info_t	d;
3056 	int		i;
3057 	int		j;
3058 	int		idx;
3059 
3060 	/*
3061 		Probe for up to MAX_C7000 devices.
3062 		Get the first irq into variable idx.
3063 	*/
3064 
3065 	idx = get_irq_first();
3066 
3067 	for (j = 0; j < MAX_C7000; j++) {
3068 
3069 		if (idx < 0)
3070 			break;
3071 
3072 		/*
3073 			Continue scanning the irq's.  Variable idx
3074 			maintains the location from the prior scan.
3075 		*/
3076 
3077 		for (i = idx; i >= 0; i = get_irq_next(i)) {
3078 
3079 			/*
3080 				Ignore invalid irq's.
3081 			*/
3082 
3083 			if (get_dev_info_by_irq(i, &d) < 0)
3084 				continue;
3085 
3086 			/*
3087 				A Cisco 7000 is defined as a 3088 model
3088 				type 0x61.
3089 			*/
3090 
3091 			if (d.sid_data.cu_type == C7000_CU_TYPE &&
3092 			    d.sid_data.cu_model == C7000_CU_MODEL) {
3093 				CPrintk(0, "c7000_probe: unit probe found 0x%x\n", d.devno);
3094 				bases[j] = d.devno;
3095 
3096 				/*
3097 					Skip the write irq and setup idx
3098 					to probe for the next box.
3099 				*/
3100 
3101 				idx = get_irq_next(i + 1);
3102 				break;
3103 			}
3104 
3105 		}
3106 
3107 	}
3108 
3109 	return;
3110 }
3111 
3112 /*
3113 	Module loading.  Register each C7000 interface found via probing
3114 	or insmod command parameters.
3115 */
3116 
3117 int
init_module(void)3118 init_module(void)
3119 {
3120 	int		result;
3121 	int		i;
3122 
3123 	for (i = 0 ; i < MAX_C7000; i++)
3124 		bases[i] = -1;
3125 
3126 	/*
3127 		Perform automatic detection provided it has not been disabled
3128 		by the noauto parameter.
3129 	*/
3130 
3131 	if (noauto == 0)
3132 		c7000_probe();
3133 
3134 	/*
3135 		Populate bases array from the module basex parameters replacing
3136 		what probing found above.
3137 	*/
3138 
3139 	if (base0 != -1)
3140 		bases[0] = base0;
3141 
3142 	if (base1 != -1)
3143 		bases[1] = base1;
3144 
3145 	if (base2 != -1)
3146 		bases[2] = base2;
3147 
3148 	if (base3 != -1)
3149 		bases[3] = base3;
3150 
3151 	for (i = 0; i < MAX_C7000; i++) {
3152 
3153 		if (bases[i] == -1)
3154 			continue;
3155 
3156 		/*
3157 			Initialize the device structure.
3158 		*/
3159 
3160 		memset(&c7000_devices[i], '\0', sizeof(struct net_device));
3161 		strcpy(c7000_devices[i].name, ifnames[i]);
3162 		c7000_devices[i].base_addr = bases[i];
3163 		c7000_devices[i].init = c7000_init;
3164 
3165 		/*
3166 			Register the device.  This creates the interface
3167 			such as ci0.
3168 		*/
3169 
3170 		if ((result = register_netdev(&c7000_devices[i])) != 0)  {
3171 			CPrintk(0, "c7000: init_module: error %d registering base unit 0x%x\n",
3172 				result, bases[i]);
3173 			c7000_devices[i].base_addr = -1;
3174 		} else {
3175 			CPrintk(1, "c7000: init_module: registered base unit 0x%x on interface %s\n", bases[i], ifnames[i]);
3176 		}
3177 
3178 	}
3179 
3180 	CPrintk(0, "c7000: init_module: module loaded\n");
3181 	return(0);
3182 }
3183 
3184 /*
3185 	Module unloading.  Unregister the interface and free kernel
3186 	allocated memory.
3187 */
3188 
3189 void
cleanup_module(void)3190 cleanup_module(void)
3191 {
3192 	int				i;
3193 	int				j;
3194 	struct	c7000_controller	*ccp;
3195 
3196 	for (i = 0; i < MAX_C7000; i++) {
3197 
3198 		if (bases[i] == -1)
3199 			continue;
3200 
3201 		/*
3202 			If the device was registered, it must be unregistered
3203 			prior to unloading the module.
3204 		*/
3205 
3206 		if (c7000_devices[i].base_addr != -1) {
3207 
3208 			ccp = (struct c7000_controller *) c7000_devices[i].priv;
3209 
3210 			if (ccp != NULL) {
3211 
3212 				for (j = 0; j < NUNITS ; j++) {
3213 					CPrintk(1, "c7000: clean_module: free subchannel 0x%x for unit 0x%x\n", ccp->cunits[j].irq, ccp->cunits[j].devno);
3214 					free_irq(ccp->cunits[j].irq, &ccp->cunits[j].devstat);
3215 				}
3216 
3217 				CPrintk(1, "c7000: clean_module: free a c7000_controller structure at address %p\n", ccp);
3218 				kfree(ccp);
3219 			}
3220 
3221 			unregister_netdev(&c7000_devices[i]);
3222 		}
3223 
3224 		bases[i] = -1;
3225 	}
3226 
3227 	CPrintk(0, "c7000: clean_module: module unloaded\n");
3228 	return;
3229 }
3230