Commit | Line | Data |
---|---|---|
293d984f PT |
1 | /* |
2 | * drivers/s390/net/ctcm_main.c | |
3 | * | |
b8a2d42a | 4 | * Copyright IBM Corp. 2001, 2009 |
293d984f PT |
5 | * Author(s): |
6 | * Original CTC driver(s): | |
7 | * Fritz Elfert (felfert@millenux.com) | |
8 | * Dieter Wellerdiek (wel@de.ibm.com) | |
9 | * Martin Schwidefsky (schwidefsky@de.ibm.com) | |
10 | * Denis Joseph Barrow (barrow_dj@yahoo.com) | |
11 | * Jochen Roehrig (roehrig@de.ibm.com) | |
12 | * Cornelia Huck <cornelia.huck@de.ibm.com> | |
13 | * MPC additions: | |
14 | * Belinda Thompson (belindat@us.ibm.com) | |
15 | * Andy Richter (richtera@us.ibm.com) | |
16 | * Revived by: | |
17 | * Peter Tiedemann (ptiedem@de.ibm.com) | |
18 | */ | |
19 | ||
20 | #undef DEBUG | |
21 | #undef DEBUGDATA | |
22 | #undef DEBUGCCW | |
23 | ||
2a7c6f2c PT |
24 | #define KMSG_COMPONENT "ctcm" |
25 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
26 | ||
293d984f PT |
27 | #include <linux/module.h> |
28 | #include <linux/init.h> | |
29 | #include <linux/kernel.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/errno.h> | |
32 | #include <linux/types.h> | |
33 | #include <linux/interrupt.h> | |
34 | #include <linux/timer.h> | |
35 | #include <linux/bitops.h> | |
36 | ||
37 | #include <linux/signal.h> | |
38 | #include <linux/string.h> | |
39 | ||
40 | #include <linux/ip.h> | |
41 | #include <linux/if_arp.h> | |
42 | #include <linux/tcp.h> | |
43 | #include <linux/skbuff.h> | |
44 | #include <linux/ctype.h> | |
45 | #include <net/dst.h> | |
46 | ||
47 | #include <linux/io.h> | |
48 | #include <asm/ccwdev.h> | |
49 | #include <asm/ccwgroup.h> | |
50 | #include <linux/uaccess.h> | |
51 | ||
52 | #include <asm/idals.h> | |
53 | ||
293d984f PT |
54 | #include "ctcm_fsms.h" |
55 | #include "ctcm_main.h" | |
56 | ||
57 | /* Some common global variables */ | |
58 | ||
0ca8cc6f UB |
59 | /** |
60 | * The root device for ctcm group devices | |
61 | */ | |
62 | static struct device *ctcm_root_dev; | |
63 | ||
293d984f PT |
64 | /* |
65 | * Linked list of all detected channels. | |
66 | */ | |
67 | struct channel *channels; | |
68 | ||
69 | /** | |
70 | * Unpack a just received skb and hand it over to | |
71 | * upper layers. | |
72 | * | |
73 | * ch The channel where this skb has been received. | |
74 | * pskb The received skb. | |
75 | */ | |
76 | void ctcm_unpack_skb(struct channel *ch, struct sk_buff *pskb) | |
77 | { | |
78 | struct net_device *dev = ch->netdev; | |
261893d3 | 79 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
80 | __u16 len = *((__u16 *) pskb->data); |
81 | ||
82 | skb_put(pskb, 2 + LL_HEADER_LENGTH); | |
83 | skb_pull(pskb, 2); | |
84 | pskb->dev = dev; | |
85 | pskb->ip_summed = CHECKSUM_UNNECESSARY; | |
86 | while (len > 0) { | |
87 | struct sk_buff *skb; | |
88 | int skblen; | |
89 | struct ll_header *header = (struct ll_header *)pskb->data; | |
90 | ||
91 | skb_pull(pskb, LL_HEADER_LENGTH); | |
92 | if ((ch->protocol == CTCM_PROTO_S390) && | |
93 | (header->type != ETH_P_IP)) { | |
293d984f | 94 | if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) { |
aa3f2cb6 | 95 | ch->logflags |= LOG_FLAG_ILLEGALPKT; |
293d984f PT |
96 | /* |
97 | * Check packet type only if we stick strictly | |
98 | * to S/390's protocol of OS390. This only | |
99 | * supports IP. Otherwise allow any packet | |
100 | * type. | |
101 | */ | |
aa3f2cb6 PT |
102 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
103 | "%s(%s): Illegal packet type 0x%04x" | |
104 | " - dropping", | |
105 | CTCM_FUNTAIL, dev->name, header->type); | |
293d984f | 106 | } |
293d984f PT |
107 | priv->stats.rx_dropped++; |
108 | priv->stats.rx_frame_errors++; | |
109 | return; | |
110 | } | |
111 | pskb->protocol = ntohs(header->type); | |
fb8585fc RK |
112 | if ((header->length <= LL_HEADER_LENGTH) || |
113 | (len <= LL_HEADER_LENGTH)) { | |
293d984f | 114 | if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) { |
aa3f2cb6 PT |
115 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
116 | "%s(%s): Illegal packet size %d(%d,%d)" | |
117 | "- dropping", | |
118 | CTCM_FUNTAIL, dev->name, | |
119 | header->length, dev->mtu, len); | |
293d984f PT |
120 | ch->logflags |= LOG_FLAG_ILLEGALSIZE; |
121 | } | |
122 | ||
123 | priv->stats.rx_dropped++; | |
124 | priv->stats.rx_length_errors++; | |
125 | return; | |
126 | } | |
127 | header->length -= LL_HEADER_LENGTH; | |
128 | len -= LL_HEADER_LENGTH; | |
129 | if ((header->length > skb_tailroom(pskb)) || | |
130 | (header->length > len)) { | |
131 | if (!(ch->logflags & LOG_FLAG_OVERRUN)) { | |
aa3f2cb6 PT |
132 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
133 | "%s(%s): Packet size %d (overrun)" | |
134 | " - dropping", CTCM_FUNTAIL, | |
135 | dev->name, header->length); | |
293d984f PT |
136 | ch->logflags |= LOG_FLAG_OVERRUN; |
137 | } | |
138 | ||
139 | priv->stats.rx_dropped++; | |
140 | priv->stats.rx_length_errors++; | |
141 | return; | |
142 | } | |
143 | skb_put(pskb, header->length); | |
144 | skb_reset_mac_header(pskb); | |
145 | len -= header->length; | |
146 | skb = dev_alloc_skb(pskb->len); | |
147 | if (!skb) { | |
148 | if (!(ch->logflags & LOG_FLAG_NOMEM)) { | |
aa3f2cb6 PT |
149 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
150 | "%s(%s): MEMORY allocation error", | |
151 | CTCM_FUNTAIL, dev->name); | |
293d984f PT |
152 | ch->logflags |= LOG_FLAG_NOMEM; |
153 | } | |
154 | priv->stats.rx_dropped++; | |
155 | return; | |
156 | } | |
157 | skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len), | |
158 | pskb->len); | |
159 | skb_reset_mac_header(skb); | |
160 | skb->dev = pskb->dev; | |
161 | skb->protocol = pskb->protocol; | |
162 | pskb->ip_summed = CHECKSUM_UNNECESSARY; | |
163 | skblen = skb->len; | |
164 | /* | |
165 | * reset logflags | |
166 | */ | |
167 | ch->logflags = 0; | |
168 | priv->stats.rx_packets++; | |
169 | priv->stats.rx_bytes += skblen; | |
170 | netif_rx_ni(skb); | |
293d984f PT |
171 | if (len > 0) { |
172 | skb_pull(pskb, header->length); | |
173 | if (skb_tailroom(pskb) < LL_HEADER_LENGTH) { | |
e2fc8cb4 JF |
174 | CTCM_DBF_DEV_NAME(TRACE, dev, |
175 | "Overrun in ctcm_unpack_skb"); | |
176 | ch->logflags |= LOG_FLAG_OVERRUN; | |
293d984f PT |
177 | return; |
178 | } | |
179 | skb_put(pskb, LL_HEADER_LENGTH); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | /** | |
185 | * Release a specific channel in the channel list. | |
186 | * | |
187 | * ch Pointer to channel struct to be released. | |
188 | */ | |
189 | static void channel_free(struct channel *ch) | |
190 | { | |
aa3f2cb6 | 191 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s)", CTCM_FUNTAIL, ch->id); |
293d984f PT |
192 | ch->flags &= ~CHANNEL_FLAGS_INUSE; |
193 | fsm_newstate(ch->fsm, CTC_STATE_IDLE); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Remove a specific channel in the channel list. | |
198 | * | |
199 | * ch Pointer to channel struct to be released. | |
200 | */ | |
201 | static void channel_remove(struct channel *ch) | |
202 | { | |
203 | struct channel **c = &channels; | |
204 | char chid[CTCM_ID_SIZE+1]; | |
205 | int ok = 0; | |
206 | ||
207 | if (ch == NULL) | |
208 | return; | |
209 | else | |
210 | strncpy(chid, ch->id, CTCM_ID_SIZE); | |
211 | ||
212 | channel_free(ch); | |
213 | while (*c) { | |
214 | if (*c == ch) { | |
215 | *c = ch->next; | |
216 | fsm_deltimer(&ch->timer); | |
217 | if (IS_MPC(ch)) | |
218 | fsm_deltimer(&ch->sweep_timer); | |
219 | ||
220 | kfree_fsm(ch->fsm); | |
221 | clear_normalized_cda(&ch->ccw[4]); | |
222 | if (ch->trans_skb != NULL) { | |
223 | clear_normalized_cda(&ch->ccw[1]); | |
224 | dev_kfree_skb_any(ch->trans_skb); | |
225 | } | |
226 | if (IS_MPC(ch)) { | |
227 | tasklet_kill(&ch->ch_tasklet); | |
228 | tasklet_kill(&ch->ch_disc_tasklet); | |
229 | kfree(ch->discontact_th); | |
230 | } | |
231 | kfree(ch->ccw); | |
232 | kfree(ch->irb); | |
233 | kfree(ch); | |
234 | ok = 1; | |
235 | break; | |
236 | } | |
237 | c = &((*c)->next); | |
238 | } | |
239 | ||
240 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s) %s", CTCM_FUNTAIL, | |
241 | chid, ok ? "OK" : "failed"); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Get a specific channel from the channel list. | |
246 | * | |
247 | * type Type of channel we are interested in. | |
248 | * id Id of channel we are interested in. | |
249 | * direction Direction we want to use this channel for. | |
250 | * | |
251 | * returns Pointer to a channel or NULL if no matching channel available. | |
252 | */ | |
0ca8cc6f | 253 | static struct channel *channel_get(enum ctcm_channel_types type, |
293d984f PT |
254 | char *id, int direction) |
255 | { | |
256 | struct channel *ch = channels; | |
257 | ||
293d984f PT |
258 | while (ch && (strncmp(ch->id, id, CTCM_ID_SIZE) || (ch->type != type))) |
259 | ch = ch->next; | |
260 | if (!ch) { | |
aa3f2cb6 PT |
261 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
262 | "%s(%d, %s, %d) not found in channel list\n", | |
293d984f | 263 | CTCM_FUNTAIL, type, id, direction); |
293d984f PT |
264 | } else { |
265 | if (ch->flags & CHANNEL_FLAGS_INUSE) | |
266 | ch = NULL; | |
267 | else { | |
268 | ch->flags |= CHANNEL_FLAGS_INUSE; | |
269 | ch->flags &= ~CHANNEL_FLAGS_RWMASK; | |
270 | ch->flags |= (direction == WRITE) | |
271 | ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ; | |
272 | fsm_newstate(ch->fsm, CTC_STATE_STOPPED); | |
273 | } | |
274 | } | |
275 | return ch; | |
276 | } | |
277 | ||
278 | static long ctcm_check_irb_error(struct ccw_device *cdev, struct irb *irb) | |
279 | { | |
280 | if (!IS_ERR(irb)) | |
281 | return 0; | |
282 | ||
aa3f2cb6 PT |
283 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_WARN, |
284 | "irb error %ld on device %s\n", | |
2a0217d5 | 285 | PTR_ERR(irb), dev_name(&cdev->dev)); |
293d984f PT |
286 | |
287 | switch (PTR_ERR(irb)) { | |
288 | case -EIO: | |
2a7c6f2c PT |
289 | dev_err(&cdev->dev, |
290 | "An I/O-error occurred on the CTCM device\n"); | |
293d984f PT |
291 | break; |
292 | case -ETIMEDOUT: | |
2a7c6f2c PT |
293 | dev_err(&cdev->dev, |
294 | "An adapter hardware operation timed out\n"); | |
293d984f PT |
295 | break; |
296 | default: | |
2a7c6f2c PT |
297 | dev_err(&cdev->dev, |
298 | "An error occurred on the adapter hardware\n"); | |
293d984f PT |
299 | } |
300 | return PTR_ERR(irb); | |
301 | } | |
302 | ||
303 | ||
304 | /** | |
305 | * Check sense of a unit check. | |
306 | * | |
307 | * ch The channel, the sense code belongs to. | |
308 | * sense The sense code to inspect. | |
309 | */ | |
aa3f2cb6 | 310 | static inline void ccw_unit_check(struct channel *ch, __u8 sense) |
293d984f | 311 | { |
aa3f2cb6 PT |
312 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG, |
313 | "%s(%s): %02x", | |
314 | CTCM_FUNTAIL, ch->id, sense); | |
315 | ||
293d984f PT |
316 | if (sense & SNS0_INTERVENTION_REQ) { |
317 | if (sense & 0x01) { | |
aa3f2cb6 | 318 | if (ch->sense_rc != 0x01) { |
2a7c6f2c PT |
319 | pr_notice( |
320 | "%s: The communication peer has " | |
321 | "disconnected\n", ch->id); | |
aa3f2cb6 PT |
322 | ch->sense_rc = 0x01; |
323 | } | |
293d984f PT |
324 | fsm_event(ch->fsm, CTC_EVENT_UC_RCRESET, ch); |
325 | } else { | |
aa3f2cb6 | 326 | if (ch->sense_rc != SNS0_INTERVENTION_REQ) { |
2a7c6f2c PT |
327 | pr_notice( |
328 | "%s: The remote operating system is " | |
329 | "not available\n", ch->id); | |
aa3f2cb6 PT |
330 | ch->sense_rc = SNS0_INTERVENTION_REQ; |
331 | } | |
293d984f PT |
332 | fsm_event(ch->fsm, CTC_EVENT_UC_RSRESET, ch); |
333 | } | |
334 | } else if (sense & SNS0_EQUIPMENT_CHECK) { | |
335 | if (sense & SNS0_BUS_OUT_CHECK) { | |
aa3f2cb6 PT |
336 | if (ch->sense_rc != SNS0_BUS_OUT_CHECK) { |
337 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, | |
338 | "%s(%s): remote HW error %02x", | |
339 | CTCM_FUNTAIL, ch->id, sense); | |
340 | ch->sense_rc = SNS0_BUS_OUT_CHECK; | |
341 | } | |
293d984f PT |
342 | fsm_event(ch->fsm, CTC_EVENT_UC_HWFAIL, ch); |
343 | } else { | |
aa3f2cb6 PT |
344 | if (ch->sense_rc != SNS0_EQUIPMENT_CHECK) { |
345 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, | |
346 | "%s(%s): remote read parity error %02x", | |
347 | CTCM_FUNTAIL, ch->id, sense); | |
348 | ch->sense_rc = SNS0_EQUIPMENT_CHECK; | |
349 | } | |
293d984f PT |
350 | fsm_event(ch->fsm, CTC_EVENT_UC_RXPARITY, ch); |
351 | } | |
352 | } else if (sense & SNS0_BUS_OUT_CHECK) { | |
aa3f2cb6 PT |
353 | if (ch->sense_rc != SNS0_BUS_OUT_CHECK) { |
354 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, | |
355 | "%s(%s): BUS OUT error %02x", | |
356 | CTCM_FUNTAIL, ch->id, sense); | |
357 | ch->sense_rc = SNS0_BUS_OUT_CHECK; | |
358 | } | |
359 | if (sense & 0x04) /* data-streaming timeout */ | |
293d984f | 360 | fsm_event(ch->fsm, CTC_EVENT_UC_TXTIMEOUT, ch); |
aa3f2cb6 | 361 | else /* Data-transfer parity error */ |
293d984f | 362 | fsm_event(ch->fsm, CTC_EVENT_UC_TXPARITY, ch); |
293d984f | 363 | } else if (sense & SNS0_CMD_REJECT) { |
aa3f2cb6 PT |
364 | if (ch->sense_rc != SNS0_CMD_REJECT) { |
365 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, | |
366 | "%s(%s): Command rejected", | |
367 | CTCM_FUNTAIL, ch->id); | |
368 | ch->sense_rc = SNS0_CMD_REJECT; | |
369 | } | |
293d984f | 370 | } else if (sense == 0) { |
aa3f2cb6 PT |
371 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, |
372 | "%s(%s): Unit check ZERO", | |
373 | CTCM_FUNTAIL, ch->id); | |
293d984f PT |
374 | fsm_event(ch->fsm, CTC_EVENT_UC_ZERO, ch); |
375 | } else { | |
aa3f2cb6 PT |
376 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, |
377 | "%s(%s): Unit check code %02x unknown", | |
378 | CTCM_FUNTAIL, ch->id, sense); | |
293d984f PT |
379 | fsm_event(ch->fsm, CTC_EVENT_UC_UNKNOWN, ch); |
380 | } | |
381 | } | |
382 | ||
383 | int ctcm_ch_alloc_buffer(struct channel *ch) | |
384 | { | |
293d984f PT |
385 | clear_normalized_cda(&ch->ccw[1]); |
386 | ch->trans_skb = __dev_alloc_skb(ch->max_bufsize, GFP_ATOMIC | GFP_DMA); | |
387 | if (ch->trans_skb == NULL) { | |
aa3f2cb6 PT |
388 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
389 | "%s(%s): %s trans_skb allocation error", | |
390 | CTCM_FUNTAIL, ch->id, | |
293d984f PT |
391 | (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX"); |
392 | return -ENOMEM; | |
393 | } | |
394 | ||
395 | ch->ccw[1].count = ch->max_bufsize; | |
396 | if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) { | |
397 | dev_kfree_skb(ch->trans_skb); | |
398 | ch->trans_skb = NULL; | |
aa3f2cb6 PT |
399 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
400 | "%s(%s): %s set norm_cda failed", | |
401 | CTCM_FUNTAIL, ch->id, | |
293d984f PT |
402 | (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX"); |
403 | return -ENOMEM; | |
404 | } | |
405 | ||
406 | ch->ccw[1].count = 0; | |
407 | ch->trans_skb_data = ch->trans_skb->data; | |
408 | ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED; | |
409 | return 0; | |
410 | } | |
411 | ||
412 | /* | |
413 | * Interface API for upper network layers | |
414 | */ | |
415 | ||
416 | /** | |
417 | * Open an interface. | |
418 | * Called from generic network layer when ifconfig up is run. | |
419 | * | |
420 | * dev Pointer to interface struct. | |
421 | * | |
422 | * returns 0 on success, -ERRNO on failure. (Never fails.) | |
423 | */ | |
424 | int ctcm_open(struct net_device *dev) | |
425 | { | |
261893d3 | 426 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
427 | |
428 | CTCMY_DBF_DEV_NAME(SETUP, dev, ""); | |
429 | if (!IS_MPC(priv)) | |
430 | fsm_event(priv->fsm, DEV_EVENT_START, dev); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * Close an interface. | |
436 | * Called from generic network layer when ifconfig down is run. | |
437 | * | |
438 | * dev Pointer to interface struct. | |
439 | * | |
440 | * returns 0 on success, -ERRNO on failure. (Never fails.) | |
441 | */ | |
442 | int ctcm_close(struct net_device *dev) | |
443 | { | |
261893d3 | 444 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
445 | |
446 | CTCMY_DBF_DEV_NAME(SETUP, dev, ""); | |
447 | if (!IS_MPC(priv)) | |
448 | fsm_event(priv->fsm, DEV_EVENT_STOP, dev); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | ||
453 | /** | |
454 | * Transmit a packet. | |
455 | * This is a helper function for ctcm_tx(). | |
456 | * | |
457 | * ch Channel to be used for sending. | |
458 | * skb Pointer to struct sk_buff of packet to send. | |
459 | * The linklevel header has already been set up | |
460 | * by ctcm_tx(). | |
461 | * | |
462 | * returns 0 on success, -ERRNO on failure. (Never fails.) | |
463 | */ | |
464 | static int ctcm_transmit_skb(struct channel *ch, struct sk_buff *skb) | |
465 | { | |
466 | unsigned long saveflags; | |
467 | struct ll_header header; | |
468 | int rc = 0; | |
469 | __u16 block_len; | |
470 | int ccw_idx; | |
471 | struct sk_buff *nskb; | |
472 | unsigned long hi; | |
473 | ||
474 | /* we need to acquire the lock for testing the state | |
475 | * otherwise we can have an IRQ changing the state to | |
476 | * TXIDLE after the test but before acquiring the lock. | |
477 | */ | |
478 | spin_lock_irqsave(&ch->collect_lock, saveflags); | |
479 | if (fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) { | |
480 | int l = skb->len + LL_HEADER_LENGTH; | |
481 | ||
482 | if (ch->collect_len + l > ch->max_bufsize - 2) { | |
483 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); | |
484 | return -EBUSY; | |
485 | } else { | |
486 | atomic_inc(&skb->users); | |
487 | header.length = l; | |
488 | header.type = skb->protocol; | |
489 | header.unused = 0; | |
490 | memcpy(skb_push(skb, LL_HEADER_LENGTH), &header, | |
491 | LL_HEADER_LENGTH); | |
492 | skb_queue_tail(&ch->collect_queue, skb); | |
493 | ch->collect_len += l; | |
494 | } | |
495 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); | |
496 | goto done; | |
497 | } | |
498 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); | |
499 | /* | |
500 | * Protect skb against beeing free'd by upper | |
501 | * layers. | |
502 | */ | |
503 | atomic_inc(&skb->users); | |
504 | ch->prof.txlen += skb->len; | |
505 | header.length = skb->len + LL_HEADER_LENGTH; | |
506 | header.type = skb->protocol; | |
507 | header.unused = 0; | |
508 | memcpy(skb_push(skb, LL_HEADER_LENGTH), &header, LL_HEADER_LENGTH); | |
509 | block_len = skb->len + 2; | |
510 | *((__u16 *)skb_push(skb, 2)) = block_len; | |
511 | ||
512 | /* | |
513 | * IDAL support in CTCM is broken, so we have to | |
514 | * care about skb's above 2G ourselves. | |
515 | */ | |
516 | hi = ((unsigned long)skb_tail_pointer(skb) + LL_HEADER_LENGTH) >> 31; | |
517 | if (hi) { | |
518 | nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA); | |
519 | if (!nskb) { | |
520 | atomic_dec(&skb->users); | |
521 | skb_pull(skb, LL_HEADER_LENGTH + 2); | |
522 | ctcm_clear_busy(ch->netdev); | |
523 | return -ENOMEM; | |
524 | } else { | |
525 | memcpy(skb_put(nskb, skb->len), skb->data, skb->len); | |
526 | atomic_inc(&nskb->users); | |
527 | atomic_dec(&skb->users); | |
528 | dev_kfree_skb_irq(skb); | |
529 | skb = nskb; | |
530 | } | |
531 | } | |
532 | ||
533 | ch->ccw[4].count = block_len; | |
534 | if (set_normalized_cda(&ch->ccw[4], skb->data)) { | |
535 | /* | |
536 | * idal allocation failed, try via copying to | |
537 | * trans_skb. trans_skb usually has a pre-allocated | |
538 | * idal. | |
539 | */ | |
540 | if (ctcm_checkalloc_buffer(ch)) { | |
541 | /* | |
542 | * Remove our header. It gets added | |
543 | * again on retransmit. | |
544 | */ | |
545 | atomic_dec(&skb->users); | |
546 | skb_pull(skb, LL_HEADER_LENGTH + 2); | |
547 | ctcm_clear_busy(ch->netdev); | |
aa3f2cb6 | 548 | return -ENOMEM; |
293d984f PT |
549 | } |
550 | ||
551 | skb_reset_tail_pointer(ch->trans_skb); | |
552 | ch->trans_skb->len = 0; | |
553 | ch->ccw[1].count = skb->len; | |
554 | skb_copy_from_linear_data(skb, | |
555 | skb_put(ch->trans_skb, skb->len), skb->len); | |
556 | atomic_dec(&skb->users); | |
557 | dev_kfree_skb_irq(skb); | |
558 | ccw_idx = 0; | |
559 | } else { | |
560 | skb_queue_tail(&ch->io_queue, skb); | |
561 | ccw_idx = 3; | |
562 | } | |
563 | ch->retry = 0; | |
564 | fsm_newstate(ch->fsm, CTC_STATE_TX); | |
565 | fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch); | |
566 | spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags); | |
567 | ch->prof.send_stamp = current_kernel_time(); /* xtime */ | |
568 | rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx], | |
569 | (unsigned long)ch, 0xff, 0); | |
570 | spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags); | |
571 | if (ccw_idx == 3) | |
572 | ch->prof.doios_single++; | |
573 | if (rc != 0) { | |
574 | fsm_deltimer(&ch->timer); | |
575 | ctcm_ccw_check_rc(ch, rc, "single skb TX"); | |
576 | if (ccw_idx == 3) | |
577 | skb_dequeue_tail(&ch->io_queue); | |
578 | /* | |
579 | * Remove our header. It gets added | |
580 | * again on retransmit. | |
581 | */ | |
582 | skb_pull(skb, LL_HEADER_LENGTH + 2); | |
583 | } else if (ccw_idx == 0) { | |
584 | struct net_device *dev = ch->netdev; | |
261893d3 | 585 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
586 | priv->stats.tx_packets++; |
587 | priv->stats.tx_bytes += skb->len - LL_HEADER_LENGTH; | |
588 | } | |
589 | done: | |
590 | ctcm_clear_busy(ch->netdev); | |
591 | return rc; | |
592 | } | |
593 | ||
594 | static void ctcmpc_send_sweep_req(struct channel *rch) | |
595 | { | |
596 | struct net_device *dev = rch->netdev; | |
597 | struct ctcm_priv *priv; | |
598 | struct mpc_group *grp; | |
599 | struct th_sweep *header; | |
600 | struct sk_buff *sweep_skb; | |
601 | struct channel *ch; | |
aa3f2cb6 | 602 | /* int rc = 0; */ |
293d984f | 603 | |
261893d3 | 604 | priv = dev->ml_priv; |
293d984f PT |
605 | grp = priv->mpcg; |
606 | ch = priv->channel[WRITE]; | |
607 | ||
293d984f PT |
608 | /* sweep processing is not complete until response and request */ |
609 | /* has completed for all read channels in group */ | |
610 | if (grp->in_sweep == 0) { | |
611 | grp->in_sweep = 1; | |
612 | grp->sweep_rsp_pend_num = grp->active_channels[READ]; | |
613 | grp->sweep_req_pend_num = grp->active_channels[READ]; | |
614 | } | |
615 | ||
616 | sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA); | |
617 | ||
618 | if (sweep_skb == NULL) { | |
aa3f2cb6 PT |
619 | /* rc = -ENOMEM; */ |
620 | goto nomem; | |
293d984f PT |
621 | } |
622 | ||
623 | header = kmalloc(TH_SWEEP_LENGTH, gfp_type()); | |
624 | ||
625 | if (!header) { | |
626 | dev_kfree_skb_any(sweep_skb); | |
aa3f2cb6 PT |
627 | /* rc = -ENOMEM; */ |
628 | goto nomem; | |
293d984f PT |
629 | } |
630 | ||
631 | header->th.th_seg = 0x00 ; | |
632 | header->th.th_ch_flag = TH_SWEEP_REQ; /* 0x0f */ | |
633 | header->th.th_blk_flag = 0x00; | |
634 | header->th.th_is_xid = 0x00; | |
635 | header->th.th_seq_num = 0x00; | |
636 | header->sw.th_last_seq = ch->th_seq_num; | |
637 | ||
638 | memcpy(skb_put(sweep_skb, TH_SWEEP_LENGTH), header, TH_SWEEP_LENGTH); | |
639 | ||
640 | kfree(header); | |
641 | ||
642 | dev->trans_start = jiffies; | |
643 | skb_queue_tail(&ch->sweep_queue, sweep_skb); | |
644 | ||
645 | fsm_addtimer(&ch->sweep_timer, 100, CTC_EVENT_RSWEEP_TIMER, ch); | |
646 | ||
647 | return; | |
648 | ||
aa3f2cb6 PT |
649 | nomem: |
650 | grp->in_sweep = 0; | |
651 | ctcm_clear_busy(dev); | |
652 | fsm_event(grp->fsm, MPCG_EVENT_INOP, dev); | |
293d984f PT |
653 | |
654 | return; | |
655 | } | |
656 | ||
657 | /* | |
658 | * MPC mode version of transmit_skb | |
659 | */ | |
660 | static int ctcmpc_transmit_skb(struct channel *ch, struct sk_buff *skb) | |
661 | { | |
662 | struct pdu *p_header; | |
663 | struct net_device *dev = ch->netdev; | |
261893d3 | 664 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
665 | struct mpc_group *grp = priv->mpcg; |
666 | struct th_header *header; | |
667 | struct sk_buff *nskb; | |
668 | int rc = 0; | |
669 | int ccw_idx; | |
670 | unsigned long hi; | |
671 | unsigned long saveflags = 0; /* avoids compiler warning */ | |
672 | __u16 block_len; | |
673 | ||
aa3f2cb6 PT |
674 | CTCM_PR_DEBUG("Enter %s: %s, cp=%i ch=0x%p id=%s state=%s\n", |
675 | __func__, dev->name, smp_processor_id(), ch, | |
676 | ch->id, fsm_getstate_str(ch->fsm)); | |
293d984f PT |
677 | |
678 | if ((fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) || grp->in_sweep) { | |
679 | spin_lock_irqsave(&ch->collect_lock, saveflags); | |
680 | atomic_inc(&skb->users); | |
681 | p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type()); | |
682 | ||
683 | if (!p_header) { | |
293d984f | 684 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); |
aa3f2cb6 | 685 | goto nomem_exit; |
293d984f PT |
686 | } |
687 | ||
688 | p_header->pdu_offset = skb->len; | |
689 | p_header->pdu_proto = 0x01; | |
690 | p_header->pdu_flag = 0x00; | |
691 | if (skb->protocol == ntohs(ETH_P_SNAP)) { | |
692 | p_header->pdu_flag |= PDU_FIRST | PDU_CNTL; | |
693 | } else { | |
694 | p_header->pdu_flag |= PDU_FIRST; | |
695 | } | |
696 | p_header->pdu_seq = 0; | |
697 | memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header, | |
698 | PDU_HEADER_LENGTH); | |
699 | ||
aa3f2cb6 PT |
700 | CTCM_PR_DEBUG("%s(%s): Put on collect_q - skb len: %04x \n" |
701 | "pdu header and data for up to 32 bytes:\n", | |
702 | __func__, dev->name, skb->len); | |
703 | CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len)); | |
293d984f PT |
704 | |
705 | skb_queue_tail(&ch->collect_queue, skb); | |
706 | ch->collect_len += skb->len; | |
707 | kfree(p_header); | |
708 | ||
709 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); | |
710 | goto done; | |
711 | } | |
712 | ||
713 | /* | |
714 | * Protect skb against beeing free'd by upper | |
715 | * layers. | |
716 | */ | |
717 | atomic_inc(&skb->users); | |
718 | ||
719 | block_len = skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH; | |
720 | /* | |
721 | * IDAL support in CTCM is broken, so we have to | |
722 | * care about skb's above 2G ourselves. | |
723 | */ | |
724 | hi = ((unsigned long)skb->tail + TH_HEADER_LENGTH) >> 31; | |
725 | if (hi) { | |
726 | nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA); | |
727 | if (!nskb) { | |
aa3f2cb6 | 728 | goto nomem_exit; |
293d984f PT |
729 | } else { |
730 | memcpy(skb_put(nskb, skb->len), skb->data, skb->len); | |
731 | atomic_inc(&nskb->users); | |
732 | atomic_dec(&skb->users); | |
733 | dev_kfree_skb_irq(skb); | |
734 | skb = nskb; | |
735 | } | |
736 | } | |
737 | ||
738 | p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type()); | |
739 | ||
aa3f2cb6 PT |
740 | if (!p_header) |
741 | goto nomem_exit; | |
293d984f PT |
742 | |
743 | p_header->pdu_offset = skb->len; | |
744 | p_header->pdu_proto = 0x01; | |
745 | p_header->pdu_flag = 0x00; | |
746 | p_header->pdu_seq = 0; | |
747 | if (skb->protocol == ntohs(ETH_P_SNAP)) { | |
748 | p_header->pdu_flag |= PDU_FIRST | PDU_CNTL; | |
749 | } else { | |
750 | p_header->pdu_flag |= PDU_FIRST; | |
751 | } | |
752 | memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header, PDU_HEADER_LENGTH); | |
753 | ||
754 | kfree(p_header); | |
755 | ||
756 | if (ch->collect_len > 0) { | |
757 | spin_lock_irqsave(&ch->collect_lock, saveflags); | |
758 | skb_queue_tail(&ch->collect_queue, skb); | |
759 | ch->collect_len += skb->len; | |
760 | skb = skb_dequeue(&ch->collect_queue); | |
761 | ch->collect_len -= skb->len; | |
762 | spin_unlock_irqrestore(&ch->collect_lock, saveflags); | |
763 | } | |
764 | ||
765 | p_header = (struct pdu *)skb->data; | |
766 | p_header->pdu_flag |= PDU_LAST; | |
767 | ||
768 | ch->prof.txlen += skb->len - PDU_HEADER_LENGTH; | |
769 | ||
770 | header = kmalloc(TH_HEADER_LENGTH, gfp_type()); | |
aa3f2cb6 PT |
771 | if (!header) |
772 | goto nomem_exit; | |
293d984f PT |
773 | |
774 | header->th_seg = 0x00; | |
775 | header->th_ch_flag = TH_HAS_PDU; /* Normal data */ | |
776 | header->th_blk_flag = 0x00; | |
777 | header->th_is_xid = 0x00; /* Just data here */ | |
778 | ch->th_seq_num++; | |
779 | header->th_seq_num = ch->th_seq_num; | |
780 | ||
aa3f2cb6 PT |
781 | CTCM_PR_DBGDATA("%s(%s) ToVTAM_th_seq= %08x\n" , |
782 | __func__, dev->name, ch->th_seq_num); | |
293d984f PT |
783 | |
784 | /* put the TH on the packet */ | |
785 | memcpy(skb_push(skb, TH_HEADER_LENGTH), header, TH_HEADER_LENGTH); | |
786 | ||
787 | kfree(header); | |
788 | ||
aa3f2cb6 PT |
789 | CTCM_PR_DBGDATA("%s(%s): skb len: %04x\n - pdu header and data for " |
790 | "up to 32 bytes sent to vtam:\n", | |
791 | __func__, dev->name, skb->len); | |
792 | CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len)); | |
293d984f PT |
793 | |
794 | ch->ccw[4].count = skb->len; | |
795 | if (set_normalized_cda(&ch->ccw[4], skb->data)) { | |
796 | /* | |
aa3f2cb6 PT |
797 | * idal allocation failed, try via copying to trans_skb. |
798 | * trans_skb usually has a pre-allocated idal. | |
293d984f PT |
799 | */ |
800 | if (ctcm_checkalloc_buffer(ch)) { | |
801 | /* | |
aa3f2cb6 PT |
802 | * Remove our header. |
803 | * It gets added again on retransmit. | |
293d984f | 804 | */ |
aa3f2cb6 | 805 | goto nomem_exit; |
293d984f PT |
806 | } |
807 | ||
808 | skb_reset_tail_pointer(ch->trans_skb); | |
809 | ch->trans_skb->len = 0; | |
810 | ch->ccw[1].count = skb->len; | |
811 | memcpy(skb_put(ch->trans_skb, skb->len), skb->data, skb->len); | |
812 | atomic_dec(&skb->users); | |
813 | dev_kfree_skb_irq(skb); | |
814 | ccw_idx = 0; | |
aa3f2cb6 PT |
815 | CTCM_PR_DBGDATA("%s(%s): trans_skb len: %04x\n" |
816 | "up to 32 bytes sent to vtam:\n", | |
817 | __func__, dev->name, ch->trans_skb->len); | |
818 | CTCM_D3_DUMP((char *)ch->trans_skb->data, | |
819 | min_t(int, 32, ch->trans_skb->len)); | |
293d984f PT |
820 | } else { |
821 | skb_queue_tail(&ch->io_queue, skb); | |
822 | ccw_idx = 3; | |
823 | } | |
824 | ch->retry = 0; | |
825 | fsm_newstate(ch->fsm, CTC_STATE_TX); | |
826 | fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch); | |
827 | ||
828 | if (do_debug_ccw) | |
829 | ctcmpc_dumpit((char *)&ch->ccw[ccw_idx], | |
830 | sizeof(struct ccw1) * 3); | |
831 | ||
832 | spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags); | |
833 | ch->prof.send_stamp = current_kernel_time(); /* xtime */ | |
834 | rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx], | |
835 | (unsigned long)ch, 0xff, 0); | |
836 | spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags); | |
837 | if (ccw_idx == 3) | |
838 | ch->prof.doios_single++; | |
839 | if (rc != 0) { | |
840 | fsm_deltimer(&ch->timer); | |
841 | ctcm_ccw_check_rc(ch, rc, "single skb TX"); | |
842 | if (ccw_idx == 3) | |
843 | skb_dequeue_tail(&ch->io_queue); | |
844 | } else if (ccw_idx == 0) { | |
845 | priv->stats.tx_packets++; | |
846 | priv->stats.tx_bytes += skb->len - TH_HEADER_LENGTH; | |
847 | } | |
aa3f2cb6 | 848 | if (ch->th_seq_num > 0xf0000000) /* Chose at random. */ |
293d984f PT |
849 | ctcmpc_send_sweep_req(ch); |
850 | ||
aa3f2cb6 PT |
851 | goto done; |
852 | nomem_exit: | |
853 | CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_CRIT, | |
854 | "%s(%s): MEMORY allocation ERROR\n", | |
855 | CTCM_FUNTAIL, ch->id); | |
856 | rc = -ENOMEM; | |
857 | atomic_dec(&skb->users); | |
858 | dev_kfree_skb_any(skb); | |
859 | fsm_event(priv->mpcg->fsm, MPCG_EVENT_INOP, dev); | |
293d984f | 860 | done: |
aa3f2cb6 PT |
861 | CTCM_PR_DEBUG("Exit %s(%s)\n", __func__, dev->name); |
862 | return rc; | |
293d984f PT |
863 | } |
864 | ||
865 | /** | |
866 | * Start transmission of a packet. | |
867 | * Called from generic network device layer. | |
868 | * | |
869 | * skb Pointer to buffer containing the packet. | |
870 | * dev Pointer to interface struct. | |
871 | * | |
872 | * returns 0 if packet consumed, !0 if packet rejected. | |
873 | * Note: If we return !0, then the packet is free'd by | |
874 | * the generic network layer. | |
875 | */ | |
876 | /* first merge version - leaving both functions separated */ | |
877 | static int ctcm_tx(struct sk_buff *skb, struct net_device *dev) | |
878 | { | |
261893d3 | 879 | struct ctcm_priv *priv = dev->ml_priv; |
293d984f PT |
880 | |
881 | if (skb == NULL) { | |
aa3f2cb6 PT |
882 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
883 | "%s(%s): NULL sk_buff passed", | |
884 | CTCM_FUNTAIL, dev->name); | |
293d984f | 885 | priv->stats.tx_dropped++; |
6ed10654 | 886 | return NETDEV_TX_OK; |
293d984f PT |
887 | } |
888 | if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) { | |
aa3f2cb6 PT |
889 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
890 | "%s(%s): Got sk_buff with head room < %ld bytes", | |
891 | CTCM_FUNTAIL, dev->name, LL_HEADER_LENGTH + 2); | |
293d984f PT |
892 | dev_kfree_skb(skb); |
893 | priv->stats.tx_dropped++; | |
6ed10654 | 894 | return NETDEV_TX_OK; |
293d984f PT |
895 | } |
896 | ||
897 | /* | |
898 | * If channels are not running, try to restart them | |
899 | * and throw away packet. | |
900 | */ | |
901 | if (fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) { | |
902 | fsm_event(priv->fsm, DEV_EVENT_START, dev); | |
903 | dev_kfree_skb(skb); | |
904 | priv->stats.tx_dropped++; | |
905 | priv->stats.tx_errors++; | |
906 | priv->stats.tx_carrier_errors++; | |
6ed10654 | 907 | return NETDEV_TX_OK; |
293d984f PT |
908 | } |
909 | ||
910 | if (ctcm_test_and_set_busy(dev)) | |
3a05d140 | 911 | return NETDEV_TX_BUSY; |
293d984f PT |
912 | |
913 | dev->trans_start = jiffies; | |
914 | if (ctcm_transmit_skb(priv->channel[WRITE], skb) != 0) | |
3a05d140 | 915 | return NETDEV_TX_BUSY; |
6ed10654 | 916 | return NETDEV_TX_OK; |
293d984f PT |
917 | } |
918 | ||
919 | /* unmerged MPC variant of ctcm_tx */ | |
920 | static int ctcmpc_tx(struct sk_buff *skb, struct net_device *dev) | |
921 | { | |
922 | int len = 0; | |
261893d3 | 923 | struct ctcm_priv *priv = dev->ml_priv; |
aa3f2cb6 | 924 | struct mpc_group *grp = priv->mpcg; |
293d984f PT |
925 | struct sk_buff *newskb = NULL; |
926 | ||
293d984f PT |
927 | /* |
928 | * Some sanity checks ... | |
929 | */ | |
930 | if (skb == NULL) { | |
aa3f2cb6 PT |
931 | CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR, |
932 | "%s(%s): NULL sk_buff passed", | |
933 | CTCM_FUNTAIL, dev->name); | |
293d984f PT |
934 | priv->stats.tx_dropped++; |
935 | goto done; | |
936 | } | |
937 | if (skb_headroom(skb) < (TH_HEADER_LENGTH + PDU_HEADER_LENGTH)) { | |
aa3f2cb6 PT |
938 | CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR, |
939 | "%s(%s): Got sk_buff with head room < %ld bytes", | |
940 | CTCM_FUNTAIL, dev->name, | |
941 | TH_HEADER_LENGTH + PDU_HEADER_LENGTH); | |
293d984f | 942 | |
aa3f2cb6 | 943 | CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len)); |
293d984f PT |
944 | |
945 | len = skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH; | |
946 | newskb = __dev_alloc_skb(len, gfp_type() | GFP_DMA); | |
947 | ||
948 | if (!newskb) { | |
aa3f2cb6 PT |
949 | CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR, |
950 | "%s: %s: __dev_alloc_skb failed", | |
951 | __func__, dev->name); | |
293d984f PT |
952 | |
953 | dev_kfree_skb_any(skb); | |
954 | priv->stats.tx_dropped++; | |
955 | priv->stats.tx_errors++; | |
956 | priv->stats.tx_carrier_errors++; | |
957 | fsm_event(grp->fsm, MPCG_EVENT_INOP, dev); | |
958 | goto done; | |
959 | } | |
960 | newskb->protocol = skb->protocol; | |
961 | skb_reserve(newskb, TH_HEADER_LENGTH + PDU_HEADER_LENGTH); | |
962 | memcpy(skb_put(newskb, skb->len), skb->data, skb->len); | |
963 | dev_kfree_skb_any(skb); | |
964 | skb = newskb; | |
965 | } | |
966 | ||
967 | /* | |
968 | * If channels are not running, | |
969 | * notify anybody about a link failure and throw | |
970 | * away packet. | |
971 | */ | |
972 | if ((fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) || | |
973 | (fsm_getstate(grp->fsm) < MPCG_STATE_XID2INITW)) { | |
974 | dev_kfree_skb_any(skb); | |
aa3f2cb6 PT |
975 | CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR, |
976 | "%s(%s): inactive MPCGROUP - dropped", | |
977 | CTCM_FUNTAIL, dev->name); | |
293d984f PT |
978 | priv->stats.tx_dropped++; |
979 | priv->stats.tx_errors++; | |
980 | priv->stats.tx_carrier_errors++; | |
981 | goto done; | |
982 | } | |
983 | ||
984 | if (ctcm_test_and_set_busy(dev)) { | |
aa3f2cb6 PT |
985 | CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR, |
986 | "%s(%s): device busy - dropped", | |
987 | CTCM_FUNTAIL, dev->name); | |
293d984f PT |
988 | dev_kfree_skb_any(skb); |
989 | priv->stats.tx_dropped++; | |
990 | priv->stats.tx_errors++; | |
991 | priv->stats.tx_carrier_errors++; | |
992 | fsm_event(grp->fsm, MPCG_EVENT_INOP, dev); | |
993 | goto done; | |
994 | } | |
995 | ||
996 | dev->trans_start = jiffies; | |
997 | if (ctcmpc_transmit_skb(priv->channel[WRITE], skb) != 0) { | |
aa3f2cb6 PT |
998 | CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR, |
999 | "%s(%s): device error - dropped", | |
1000 | CTCM_FUNTAIL, dev->name); | |
293d984f PT |
1001 | dev_kfree_skb_any(skb); |
1002 | priv->stats.tx_dropped++; | |
1003 | priv->stats.tx_errors++; | |
1004 | priv->stats.tx_carrier_errors++; | |
1005 | ctcm_clear_busy(dev); | |
1006 | fsm_event(grp->fsm, MPCG_EVENT_INOP, dev); | |
1007 | goto done; | |
1008 | } | |
1009 | ctcm_clear_busy(dev); | |
1010 | done: | |
1011 | if (do_debug) | |
1012 | MPC_DBF_DEV_NAME(TRACE, dev, "exit"); | |
1013 | ||
6ed10654 | 1014 | return NETDEV_TX_OK; /* handle freeing of skb here */ |
293d984f PT |
1015 | } |
1016 | ||
1017 | ||
1018 | /** | |
1019 | * Sets MTU of an interface. | |
1020 | * | |
1021 | * dev Pointer to interface struct. | |
1022 | * new_mtu The new MTU to use for this interface. | |
1023 | * | |
1024 | * returns 0 on success, -EINVAL if MTU is out of valid range. | |
1025 | * (valid range is 576 .. 65527). If VM is on the | |
1026 | * remote side, maximum MTU is 32760, however this is | |
1027 | * not checked here. | |
1028 | */ | |
1029 | static int ctcm_change_mtu(struct net_device *dev, int new_mtu) | |
1030 | { | |
1031 | struct ctcm_priv *priv; | |
1032 | int max_bufsize; | |
1033 | ||
293d984f PT |
1034 | if (new_mtu < 576 || new_mtu > 65527) |
1035 | return -EINVAL; | |
1036 | ||
261893d3 | 1037 | priv = dev->ml_priv; |
293d984f PT |
1038 | max_bufsize = priv->channel[READ]->max_bufsize; |
1039 | ||
1040 | if (IS_MPC(priv)) { | |
1041 | if (new_mtu > max_bufsize - TH_HEADER_LENGTH) | |
1042 | return -EINVAL; | |
1043 | dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH; | |
1044 | } else { | |
1045 | if (new_mtu > max_bufsize - LL_HEADER_LENGTH - 2) | |
1046 | return -EINVAL; | |
1047 | dev->hard_header_len = LL_HEADER_LENGTH + 2; | |
1048 | } | |
1049 | dev->mtu = new_mtu; | |
1050 | return 0; | |
1051 | } | |
1052 | ||
1053 | /** | |
1054 | * Returns interface statistics of a device. | |
1055 | * | |
1056 | * dev Pointer to interface struct. | |
1057 | * | |
1058 | * returns Pointer to stats struct of this interface. | |
1059 | */ | |
1060 | static struct net_device_stats *ctcm_stats(struct net_device *dev) | |
1061 | { | |
261893d3 | 1062 | return &((struct ctcm_priv *)dev->ml_priv)->stats; |
293d984f PT |
1063 | } |
1064 | ||
293d984f PT |
1065 | static void ctcm_free_netdevice(struct net_device *dev) |
1066 | { | |
1067 | struct ctcm_priv *priv; | |
1068 | struct mpc_group *grp; | |
1069 | ||
aa3f2cb6 PT |
1070 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, |
1071 | "%s(%s)", CTCM_FUNTAIL, dev->name); | |
261893d3 | 1072 | priv = dev->ml_priv; |
293d984f PT |
1073 | if (priv) { |
1074 | grp = priv->mpcg; | |
1075 | if (grp) { | |
1076 | if (grp->fsm) | |
1077 | kfree_fsm(grp->fsm); | |
1078 | if (grp->xid_skb) | |
1079 | dev_kfree_skb(grp->xid_skb); | |
1080 | if (grp->rcvd_xid_skb) | |
1081 | dev_kfree_skb(grp->rcvd_xid_skb); | |
1082 | tasklet_kill(&grp->mpc_tasklet2); | |
1083 | kfree(grp); | |
1084 | priv->mpcg = NULL; | |
1085 | } | |
1086 | if (priv->fsm) { | |
1087 | kfree_fsm(priv->fsm); | |
1088 | priv->fsm = NULL; | |
1089 | } | |
1090 | kfree(priv->xid); | |
1091 | priv->xid = NULL; | |
1092 | /* | |
1093 | * Note: kfree(priv); is done in "opposite" function of | |
1094 | * allocator function probe_device which is remove_device. | |
1095 | */ | |
1096 | } | |
1097 | #ifdef MODULE | |
1098 | free_netdev(dev); | |
1099 | #endif | |
1100 | } | |
1101 | ||
1102 | struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv); | |
1103 | ||
69b3aa60 FB |
1104 | static const struct net_device_ops ctcm_netdev_ops = { |
1105 | .ndo_open = ctcm_open, | |
1106 | .ndo_stop = ctcm_close, | |
1107 | .ndo_get_stats = ctcm_stats, | |
1108 | .ndo_change_mtu = ctcm_change_mtu, | |
1109 | .ndo_start_xmit = ctcm_tx, | |
1110 | }; | |
1111 | ||
1112 | static const struct net_device_ops ctcm_mpc_netdev_ops = { | |
1113 | .ndo_open = ctcm_open, | |
1114 | .ndo_stop = ctcm_close, | |
1115 | .ndo_get_stats = ctcm_stats, | |
1116 | .ndo_change_mtu = ctcm_change_mtu, | |
1117 | .ndo_start_xmit = ctcmpc_tx, | |
1118 | }; | |
1119 | ||
293d984f PT |
1120 | void static ctcm_dev_setup(struct net_device *dev) |
1121 | { | |
293d984f PT |
1122 | dev->type = ARPHRD_SLIP; |
1123 | dev->tx_queue_len = 100; | |
1124 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * Initialize everything of the net device except the name and the | |
1129 | * channel structs. | |
1130 | */ | |
1131 | static struct net_device *ctcm_init_netdevice(struct ctcm_priv *priv) | |
1132 | { | |
1133 | struct net_device *dev; | |
1134 | struct mpc_group *grp; | |
1135 | if (!priv) | |
1136 | return NULL; | |
1137 | ||
1138 | if (IS_MPC(priv)) | |
1139 | dev = alloc_netdev(0, MPC_DEVICE_GENE, ctcm_dev_setup); | |
1140 | else | |
1141 | dev = alloc_netdev(0, CTC_DEVICE_GENE, ctcm_dev_setup); | |
1142 | ||
1143 | if (!dev) { | |
aa3f2cb6 PT |
1144 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_CRIT, |
1145 | "%s: MEMORY allocation ERROR", | |
1146 | CTCM_FUNTAIL); | |
293d984f PT |
1147 | return NULL; |
1148 | } | |
261893d3 | 1149 | dev->ml_priv = priv; |
293d984f PT |
1150 | priv->fsm = init_fsm("ctcmdev", dev_state_names, dev_event_names, |
1151 | CTCM_NR_DEV_STATES, CTCM_NR_DEV_EVENTS, | |
1152 | dev_fsm, dev_fsm_len, GFP_KERNEL); | |
1153 | if (priv->fsm == NULL) { | |
1154 | CTCMY_DBF_DEV(SETUP, dev, "init_fsm error"); | |
1155 | kfree(dev); | |
1156 | return NULL; | |
1157 | } | |
1158 | fsm_newstate(priv->fsm, DEV_STATE_STOPPED); | |
1159 | fsm_settimer(priv->fsm, &priv->restart_timer); | |
1160 | ||
1161 | if (IS_MPC(priv)) { | |
1162 | /* MPC Group Initializations */ | |
1163 | grp = ctcmpc_init_mpc_group(priv); | |
1164 | if (grp == NULL) { | |
1165 | MPC_DBF_DEV(SETUP, dev, "init_mpc_group error"); | |
1166 | kfree(dev); | |
1167 | return NULL; | |
1168 | } | |
1169 | tasklet_init(&grp->mpc_tasklet2, | |
1170 | mpc_group_ready, (unsigned long)dev); | |
1171 | dev->mtu = MPC_BUFSIZE_DEFAULT - | |
1172 | TH_HEADER_LENGTH - PDU_HEADER_LENGTH; | |
1173 | ||
69b3aa60 | 1174 | dev->netdev_ops = &ctcm_mpc_netdev_ops; |
293d984f PT |
1175 | dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH; |
1176 | priv->buffer_size = MPC_BUFSIZE_DEFAULT; | |
1177 | } else { | |
1178 | dev->mtu = CTCM_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2; | |
69b3aa60 | 1179 | dev->netdev_ops = &ctcm_netdev_ops; |
293d984f PT |
1180 | dev->hard_header_len = LL_HEADER_LENGTH + 2; |
1181 | } | |
1182 | ||
1183 | CTCMY_DBF_DEV(SETUP, dev, "finished"); | |
aa3f2cb6 | 1184 | |
293d984f PT |
1185 | return dev; |
1186 | } | |
1187 | ||
1188 | /** | |
1189 | * Main IRQ handler. | |
1190 | * | |
1191 | * cdev The ccw_device the interrupt is for. | |
1192 | * intparm interruption parameter. | |
1193 | * irb interruption response block. | |
1194 | */ | |
1195 | static void ctcm_irq_handler(struct ccw_device *cdev, | |
1196 | unsigned long intparm, struct irb *irb) | |
1197 | { | |
1198 | struct channel *ch; | |
1199 | struct net_device *dev; | |
1200 | struct ctcm_priv *priv; | |
1201 | struct ccwgroup_device *cgdev; | |
aa3f2cb6 PT |
1202 | int cstat; |
1203 | int dstat; | |
1204 | ||
1205 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG, | |
2a0217d5 | 1206 | "Enter %s(%s)", CTCM_FUNTAIL, dev_name(&cdev->dev)); |
293d984f | 1207 | |
293d984f PT |
1208 | if (ctcm_check_irb_error(cdev, irb)) |
1209 | return; | |
1210 | ||
1211 | cgdev = dev_get_drvdata(&cdev->dev); | |
1212 | ||
aa3f2cb6 PT |
1213 | cstat = irb->scsw.cmd.cstat; |
1214 | dstat = irb->scsw.cmd.dstat; | |
1215 | ||
293d984f PT |
1216 | /* Check for unsolicited interrupts. */ |
1217 | if (cgdev == NULL) { | |
2a7c6f2c PT |
1218 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_ERROR, |
1219 | "%s(%s) unsolicited irq: c-%02x d-%02x\n", | |
1220 | CTCM_FUNTAIL, dev_name(&cdev->dev), cstat, dstat); | |
1221 | dev_warn(&cdev->dev, | |
1222 | "The adapter received a non-specific IRQ\n"); | |
293d984f PT |
1223 | return; |
1224 | } | |
1225 | ||
1226 | priv = dev_get_drvdata(&cgdev->dev); | |
1227 | ||
1228 | /* Try to extract channel from driver data. */ | |
1229 | if (priv->channel[READ]->cdev == cdev) | |
1230 | ch = priv->channel[READ]; | |
1231 | else if (priv->channel[WRITE]->cdev == cdev) | |
1232 | ch = priv->channel[WRITE]; | |
1233 | else { | |
2a7c6f2c PT |
1234 | dev_err(&cdev->dev, |
1235 | "%s: Internal error: Can't determine channel for " | |
1236 | "interrupt device %s\n", | |
1237 | __func__, dev_name(&cdev->dev)); | |
1238 | /* Explain: inconsistent internal structures */ | |
293d984f PT |
1239 | return; |
1240 | } | |
1241 | ||
aa3f2cb6 | 1242 | dev = ch->netdev; |
293d984f | 1243 | if (dev == NULL) { |
2a7c6f2c PT |
1244 | dev_err(&cdev->dev, |
1245 | "%s Internal error: net_device is NULL, ch = 0x%p\n", | |
1246 | __func__, ch); | |
1247 | /* Explain: inconsistent internal structures */ | |
293d984f PT |
1248 | return; |
1249 | } | |
1250 | ||
293d984f PT |
1251 | /* Copy interruption response block. */ |
1252 | memcpy(ch->irb, irb, sizeof(struct irb)); | |
1253 | ||
2a7c6f2c | 1254 | /* Issue error message and return on subchannel error code */ |
23d805b6 | 1255 | if (irb->scsw.cmd.cstat) { |
293d984f | 1256 | fsm_event(ch->fsm, CTC_EVENT_SC_UNKNOWN, ch); |
2a7c6f2c PT |
1257 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, |
1258 | "%s(%s): sub-ch check %s: cs=%02x ds=%02x", | |
1259 | CTCM_FUNTAIL, dev->name, ch->id, cstat, dstat); | |
1260 | dev_warn(&cdev->dev, | |
1261 | "A check occurred on the subchannel\n"); | |
293d984f PT |
1262 | return; |
1263 | } | |
1264 | ||
1265 | /* Check the reason-code of a unit check */ | |
23d805b6 | 1266 | if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) { |
aa3f2cb6 PT |
1267 | if ((irb->ecw[0] & ch->sense_rc) == 0) |
1268 | /* print it only once */ | |
2a7c6f2c | 1269 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN, |
aa3f2cb6 PT |
1270 | "%s(%s): sense=%02x, ds=%02x", |
1271 | CTCM_FUNTAIL, ch->id, irb->ecw[0], dstat); | |
293d984f PT |
1272 | ccw_unit_check(ch, irb->ecw[0]); |
1273 | return; | |
1274 | } | |
23d805b6 PO |
1275 | if (irb->scsw.cmd.dstat & DEV_STAT_BUSY) { |
1276 | if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) | |
293d984f PT |
1277 | fsm_event(ch->fsm, CTC_EVENT_ATTNBUSY, ch); |
1278 | else | |
1279 | fsm_event(ch->fsm, CTC_EVENT_BUSY, ch); | |
1280 | return; | |
1281 | } | |
23d805b6 | 1282 | if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) { |
293d984f PT |
1283 | fsm_event(ch->fsm, CTC_EVENT_ATTN, ch); |
1284 | return; | |
1285 | } | |
23d805b6 PO |
1286 | if ((irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) || |
1287 | (irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) || | |
1288 | (irb->scsw.cmd.stctl == | |
293d984f PT |
1289 | (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) |
1290 | fsm_event(ch->fsm, CTC_EVENT_FINSTAT, ch); | |
1291 | else | |
1292 | fsm_event(ch->fsm, CTC_EVENT_IRQ, ch); | |
1293 | ||
1294 | } | |
1295 | ||
1296 | /** | |
1297 | * Add ctcm specific attributes. | |
1298 | * Add ctcm private data. | |
1299 | * | |
1300 | * cgdev pointer to ccwgroup_device just added | |
1301 | * | |
1302 | * returns 0 on success, !0 on failure. | |
1303 | */ | |
1304 | static int ctcm_probe_device(struct ccwgroup_device *cgdev) | |
1305 | { | |
1306 | struct ctcm_priv *priv; | |
1307 | int rc; | |
1308 | ||
aa3f2cb6 PT |
1309 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, |
1310 | "%s %p", | |
1311 | __func__, cgdev); | |
293d984f PT |
1312 | |
1313 | if (!get_device(&cgdev->dev)) | |
1314 | return -ENODEV; | |
1315 | ||
1316 | priv = kzalloc(sizeof(struct ctcm_priv), GFP_KERNEL); | |
1317 | if (!priv) { | |
aa3f2cb6 PT |
1318 | CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR, |
1319 | "%s: memory allocation failure", | |
1320 | CTCM_FUNTAIL); | |
293d984f PT |
1321 | put_device(&cgdev->dev); |
1322 | return -ENOMEM; | |
1323 | } | |
1324 | ||
1325 | rc = ctcm_add_files(&cgdev->dev); | |
1326 | if (rc) { | |
1327 | kfree(priv); | |
1328 | put_device(&cgdev->dev); | |
1329 | return rc; | |
1330 | } | |
1331 | priv->buffer_size = CTCM_BUFSIZE_DEFAULT; | |
1332 | cgdev->cdev[0]->handler = ctcm_irq_handler; | |
1333 | cgdev->cdev[1]->handler = ctcm_irq_handler; | |
1334 | dev_set_drvdata(&cgdev->dev, priv); | |
1335 | ||
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | /** | |
1340 | * Add a new channel to the list of channels. | |
1341 | * Keeps the channel list sorted. | |
1342 | * | |
1343 | * cdev The ccw_device to be added. | |
1344 | * type The type class of the new channel. | |
1345 | * priv Points to the private data of the ccwgroup_device. | |
1346 | * | |
1347 | * returns 0 on success, !0 on error. | |
1348 | */ | |
0ca8cc6f | 1349 | static int add_channel(struct ccw_device *cdev, enum ctcm_channel_types type, |
293d984f PT |
1350 | struct ctcm_priv *priv) |
1351 | { | |
1352 | struct channel **c = &channels; | |
1353 | struct channel *ch; | |
1354 | int ccw_num; | |
1355 | int rc = 0; | |
1356 | ||
aa3f2cb6 PT |
1357 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, |
1358 | "%s(%s), type %d, proto %d", | |
2a0217d5 | 1359 | __func__, dev_name(&cdev->dev), type, priv->protocol); |
aa3f2cb6 | 1360 | |
293d984f PT |
1361 | ch = kzalloc(sizeof(struct channel), GFP_KERNEL); |
1362 | if (ch == NULL) | |
aa3f2cb6 | 1363 | return -ENOMEM; |
293d984f PT |
1364 | |
1365 | ch->protocol = priv->protocol; | |
1366 | if (IS_MPC(priv)) { | |
ae57b20a | 1367 | ch->discontact_th = kzalloc(TH_HEADER_LENGTH, gfp_type()); |
293d984f PT |
1368 | if (ch->discontact_th == NULL) |
1369 | goto nomem_return; | |
1370 | ||
1371 | ch->discontact_th->th_blk_flag = TH_DISCONTACT; | |
1372 | tasklet_init(&ch->ch_disc_tasklet, | |
1373 | mpc_action_send_discontact, (unsigned long)ch); | |
1374 | ||
1375 | tasklet_init(&ch->ch_tasklet, ctcmpc_bh, (unsigned long)ch); | |
1376 | ch->max_bufsize = (MPC_BUFSIZE_DEFAULT - 35); | |
1377 | ccw_num = 17; | |
1378 | } else | |
1379 | ccw_num = 8; | |
1380 | ||
ae57b20a | 1381 | ch->ccw = kzalloc(ccw_num * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA); |
293d984f PT |
1382 | if (ch->ccw == NULL) |
1383 | goto nomem_return; | |
1384 | ||
1385 | ch->cdev = cdev; | |
2a0217d5 | 1386 | snprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev)); |
293d984f PT |
1387 | ch->type = type; |
1388 | ||
1389 | /** | |
1390 | * "static" ccws are used in the following way: | |
1391 | * | |
1392 | * ccw[0..2] (Channel program for generic I/O): | |
1393 | * 0: prepare | |
1394 | * 1: read or write (depending on direction) with fixed | |
1395 | * buffer (idal allocated once when buffer is allocated) | |
1396 | * 2: nop | |
1397 | * ccw[3..5] (Channel program for direct write of packets) | |
1398 | * 3: prepare | |
1399 | * 4: write (idal allocated on every write). | |
1400 | * 5: nop | |
1401 | * ccw[6..7] (Channel program for initial channel setup): | |
1402 | * 6: set extended mode | |
1403 | * 7: nop | |
1404 | * | |
1405 | * ch->ccw[0..5] are initialized in ch_action_start because | |
1406 | * the channel's direction is yet unknown here. | |
1407 | * | |
1408 | * ccws used for xid2 negotiations | |
1409 | * ch-ccw[8-14] need to be used for the XID exchange either | |
1410 | * X side XID2 Processing | |
1411 | * 8: write control | |
1412 | * 9: write th | |
1413 | * 10: write XID | |
1414 | * 11: read th from secondary | |
1415 | * 12: read XID from secondary | |
1416 | * 13: read 4 byte ID | |
1417 | * 14: nop | |
1418 | * Y side XID Processing | |
1419 | * 8: sense | |
1420 | * 9: read th | |
1421 | * 10: read XID | |
1422 | * 11: write th | |
1423 | * 12: write XID | |
1424 | * 13: write 4 byte ID | |
1425 | * 14: nop | |
1426 | * | |
1427 | * ccws used for double noop due to VM timing issues | |
1428 | * which result in unrecoverable Busy on channel | |
1429 | * 15: nop | |
1430 | * 16: nop | |
1431 | */ | |
1432 | ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED; | |
1433 | ch->ccw[6].flags = CCW_FLAG_SLI; | |
1434 | ||
1435 | ch->ccw[7].cmd_code = CCW_CMD_NOOP; | |
1436 | ch->ccw[7].flags = CCW_FLAG_SLI; | |
1437 | ||
1438 | if (IS_MPC(priv)) { | |
1439 | ch->ccw[15].cmd_code = CCW_CMD_WRITE; | |
1440 | ch->ccw[15].flags = CCW_FLAG_SLI | CCW_FLAG_CC; | |
1441 | ch->ccw[15].count = TH_HEADER_LENGTH; | |
1442 | ch->ccw[15].cda = virt_to_phys(ch->discontact_th); | |
1443 | ||
1444 | ch->ccw[16].cmd_code = CCW_CMD_NOOP; | |
1445 | ch->ccw[16].flags = CCW_FLAG_SLI; | |
1446 | ||
1447 | ch->fsm = init_fsm(ch->id, ctc_ch_state_names, | |
1448 | ctc_ch_event_names, CTC_MPC_NR_STATES, | |
1449 | CTC_MPC_NR_EVENTS, ctcmpc_ch_fsm, | |
1450 | mpc_ch_fsm_len, GFP_KERNEL); | |
1451 | } else { | |
1452 | ch->fsm = init_fsm(ch->id, ctc_ch_state_names, | |
1453 | ctc_ch_event_names, CTC_NR_STATES, | |
1454 | CTC_NR_EVENTS, ch_fsm, | |
1455 | ch_fsm_len, GFP_KERNEL); | |
1456 | } | |
1457 | if (ch->fsm == NULL) | |
1458 | goto free_return; | |
1459 | ||
1460 | fsm_newstate(ch->fsm, CTC_STATE_IDLE); | |
1461 | ||
1462 | ch->irb = kzalloc(sizeof(struct irb), GFP_KERNEL); | |
1463 | if (ch->irb == NULL) | |
1464 | goto nomem_return; | |
1465 | ||
1466 | while (*c && ctcm_less_than((*c)->id, ch->id)) | |
1467 | c = &(*c)->next; | |
1468 | ||
1469 | if (*c && (!strncmp((*c)->id, ch->id, CTCM_ID_SIZE))) { | |
1470 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, | |
1471 | "%s (%s) already in list, using old entry", | |
aa3f2cb6 | 1472 | __func__, (*c)->id); |
293d984f PT |
1473 | |
1474 | goto free_return; | |
1475 | } | |
1476 | ||
1477 | spin_lock_init(&ch->collect_lock); | |
1478 | ||
1479 | fsm_settimer(ch->fsm, &ch->timer); | |
1480 | skb_queue_head_init(&ch->io_queue); | |
1481 | skb_queue_head_init(&ch->collect_queue); | |
1482 | ||
1483 | if (IS_MPC(priv)) { | |
1484 | fsm_settimer(ch->fsm, &ch->sweep_timer); | |
1485 | skb_queue_head_init(&ch->sweep_queue); | |
1486 | } | |
1487 | ch->next = *c; | |
1488 | *c = ch; | |
1489 | return 0; | |
1490 | ||
1491 | nomem_return: | |
293d984f PT |
1492 | rc = -ENOMEM; |
1493 | ||
1494 | free_return: /* note that all channel pointers are 0 or valid */ | |
aa3f2cb6 | 1495 | kfree(ch->ccw); |
293d984f PT |
1496 | kfree(ch->discontact_th); |
1497 | kfree_fsm(ch->fsm); | |
1498 | kfree(ch->irb); | |
1499 | kfree(ch); | |
1500 | return rc; | |
1501 | } | |
1502 | ||
1503 | /* | |
1504 | * Return type of a detected device. | |
1505 | */ | |
0ca8cc6f | 1506 | static enum ctcm_channel_types get_channel_type(struct ccw_device_id *id) |
293d984f | 1507 | { |
0ca8cc6f UB |
1508 | enum ctcm_channel_types type; |
1509 | type = (enum ctcm_channel_types)id->driver_info; | |
293d984f | 1510 | |
0ca8cc6f UB |
1511 | if (type == ctcm_channel_type_ficon) |
1512 | type = ctcm_channel_type_escon; | |
293d984f PT |
1513 | |
1514 | return type; | |
1515 | } | |
1516 | ||
1517 | /** | |
1518 | * | |
1519 | * Setup an interface. | |
1520 | * | |
1521 | * cgdev Device to be setup. | |
1522 | * | |
1523 | * returns 0 on success, !0 on failure. | |
1524 | */ | |
1525 | static int ctcm_new_device(struct ccwgroup_device *cgdev) | |
1526 | { | |
1527 | char read_id[CTCM_ID_SIZE]; | |
1528 | char write_id[CTCM_ID_SIZE]; | |
1529 | int direction; | |
0ca8cc6f | 1530 | enum ctcm_channel_types type; |
293d984f PT |
1531 | struct ctcm_priv *priv; |
1532 | struct net_device *dev; | |
aa3f2cb6 PT |
1533 | struct ccw_device *cdev0; |
1534 | struct ccw_device *cdev1; | |
a1c1f5ea EL |
1535 | struct channel *readc; |
1536 | struct channel *writec; | |
293d984f | 1537 | int ret; |
a1c1f5ea | 1538 | int result; |
293d984f | 1539 | |
293d984f | 1540 | priv = dev_get_drvdata(&cgdev->dev); |
a1c1f5ea EL |
1541 | if (!priv) { |
1542 | result = -ENODEV; | |
1543 | goto out_err_result; | |
1544 | } | |
293d984f | 1545 | |
aa3f2cb6 PT |
1546 | cdev0 = cgdev->cdev[0]; |
1547 | cdev1 = cgdev->cdev[1]; | |
1548 | ||
1549 | type = get_channel_type(&cdev0->id); | |
293d984f | 1550 | |
b9d3aed7 CH |
1551 | snprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev)); |
1552 | snprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev)); | |
293d984f | 1553 | |
aa3f2cb6 | 1554 | ret = add_channel(cdev0, type, priv); |
a1c1f5ea EL |
1555 | if (ret) { |
1556 | result = ret; | |
1557 | goto out_err_result; | |
1558 | } | |
aa3f2cb6 | 1559 | ret = add_channel(cdev1, type, priv); |
a1c1f5ea EL |
1560 | if (ret) { |
1561 | result = ret; | |
1562 | goto out_remove_channel1; | |
1563 | } | |
293d984f | 1564 | |
aa3f2cb6 | 1565 | ret = ccw_device_set_online(cdev0); |
293d984f | 1566 | if (ret != 0) { |
aa3f2cb6 PT |
1567 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE, |
1568 | "%s(%s) set_online rc=%d", | |
1569 | CTCM_FUNTAIL, read_id, ret); | |
a1c1f5ea EL |
1570 | result = -EIO; |
1571 | goto out_remove_channel2; | |
293d984f PT |
1572 | } |
1573 | ||
aa3f2cb6 | 1574 | ret = ccw_device_set_online(cdev1); |
293d984f | 1575 | if (ret != 0) { |
aa3f2cb6 PT |
1576 | CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE, |
1577 | "%s(%s) set_online rc=%d", | |
1578 | CTCM_FUNTAIL, write_id, ret); | |
a1c1f5ea EL |
1579 | |
1580 | result = -EIO; | |
1581 | goto out_ccw1; | |
293d984f PT |
1582 | } |
1583 | ||
1584 | dev = ctcm_init_netdevice(priv); | |
a1c1f5ea EL |
1585 | if (dev == NULL) { |
1586 | result = -ENODEV; | |
1587 | goto out_ccw2; | |
1588 | } | |
293d984f PT |
1589 | |
1590 | for (direction = READ; direction <= WRITE; direction++) { | |
1591 | priv->channel[direction] = | |
1592 | channel_get(type, direction == READ ? read_id : write_id, | |
1593 | direction); | |
1594 | if (priv->channel[direction] == NULL) { | |
1595 | if (direction == WRITE) | |
1596 | channel_free(priv->channel[READ]); | |
aa3f2cb6 | 1597 | goto out_dev; |
293d984f PT |
1598 | } |
1599 | priv->channel[direction]->netdev = dev; | |
1600 | priv->channel[direction]->protocol = priv->protocol; | |
1601 | priv->channel[direction]->max_bufsize = priv->buffer_size; | |
1602 | } | |
1603 | /* sysfs magic */ | |
1604 | SET_NETDEV_DEV(dev, &cgdev->dev); | |
1605 | ||
a1c1f5ea EL |
1606 | if (register_netdev(dev)) { |
1607 | result = -ENODEV; | |
1608 | goto out_dev; | |
1609 | } | |
293d984f PT |
1610 | |
1611 | if (ctcm_add_attributes(&cgdev->dev)) { | |
a1c1f5ea EL |
1612 | result = -ENODEV; |
1613 | goto out_unregister; | |
293d984f PT |
1614 | } |
1615 | ||
1616 | strlcpy(priv->fsm->name, dev->name, sizeof(priv->fsm->name)); | |
1617 | ||
2a7c6f2c PT |
1618 | dev_info(&dev->dev, |
1619 | "setup OK : r/w = %s/%s, protocol : %d\n", | |
1620 | priv->channel[READ]->id, | |
1621 | priv->channel[WRITE]->id, priv->protocol); | |
1622 | ||
293d984f | 1623 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, |
aa3f2cb6 PT |
1624 | "setup(%s) OK : r/w = %s/%s, protocol : %d", dev->name, |
1625 | priv->channel[READ]->id, | |
293d984f PT |
1626 | priv->channel[WRITE]->id, priv->protocol); |
1627 | ||
1628 | return 0; | |
a1c1f5ea EL |
1629 | out_unregister: |
1630 | unregister_netdev(dev); | |
aa3f2cb6 PT |
1631 | out_dev: |
1632 | ctcm_free_netdevice(dev); | |
a1c1f5ea | 1633 | out_ccw2: |
293d984f | 1634 | ccw_device_set_offline(cgdev->cdev[1]); |
a1c1f5ea | 1635 | out_ccw1: |
293d984f | 1636 | ccw_device_set_offline(cgdev->cdev[0]); |
a1c1f5ea EL |
1637 | out_remove_channel2: |
1638 | readc = channel_get(type, read_id, READ); | |
1639 | channel_remove(readc); | |
1640 | out_remove_channel1: | |
1641 | writec = channel_get(type, write_id, WRITE); | |
1642 | channel_remove(writec); | |
1643 | out_err_result: | |
1644 | return result; | |
293d984f PT |
1645 | } |
1646 | ||
1647 | /** | |
1648 | * Shutdown an interface. | |
1649 | * | |
1650 | * cgdev Device to be shut down. | |
1651 | * | |
1652 | * returns 0 on success, !0 on failure. | |
1653 | */ | |
1654 | static int ctcm_shutdown_device(struct ccwgroup_device *cgdev) | |
1655 | { | |
1656 | struct ctcm_priv *priv; | |
1657 | struct net_device *dev; | |
1658 | ||
1659 | priv = dev_get_drvdata(&cgdev->dev); | |
1660 | if (!priv) | |
1661 | return -ENODEV; | |
1662 | ||
1663 | if (priv->channel[READ]) { | |
1664 | dev = priv->channel[READ]->netdev; | |
1665 | CTCM_DBF_DEV(SETUP, dev, ""); | |
1666 | /* Close the device */ | |
1667 | ctcm_close(dev); | |
1668 | dev->flags &= ~IFF_RUNNING; | |
1669 | ctcm_remove_attributes(&cgdev->dev); | |
1670 | channel_free(priv->channel[READ]); | |
1671 | } else | |
1672 | dev = NULL; | |
1673 | ||
1674 | if (priv->channel[WRITE]) | |
1675 | channel_free(priv->channel[WRITE]); | |
1676 | ||
1677 | if (dev) { | |
aa3f2cb6 | 1678 | unregister_netdev(dev); |
293d984f PT |
1679 | ctcm_free_netdevice(dev); |
1680 | } | |
1681 | ||
1682 | if (priv->fsm) | |
1683 | kfree_fsm(priv->fsm); | |
1684 | ||
1685 | ccw_device_set_offline(cgdev->cdev[1]); | |
1686 | ccw_device_set_offline(cgdev->cdev[0]); | |
1687 | ||
1688 | if (priv->channel[READ]) | |
1689 | channel_remove(priv->channel[READ]); | |
1690 | if (priv->channel[WRITE]) | |
1691 | channel_remove(priv->channel[WRITE]); | |
1692 | priv->channel[READ] = priv->channel[WRITE] = NULL; | |
1693 | ||
1694 | return 0; | |
1695 | ||
1696 | } | |
1697 | ||
1698 | ||
1699 | static void ctcm_remove_device(struct ccwgroup_device *cgdev) | |
1700 | { | |
aa3f2cb6 | 1701 | struct ctcm_priv *priv = dev_get_drvdata(&cgdev->dev); |
293d984f | 1702 | |
aa3f2cb6 PT |
1703 | BUG_ON(priv == NULL); |
1704 | ||
1705 | CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, | |
8ac6d452 UB |
1706 | "removing device %p, proto : %d", |
1707 | cgdev, priv->protocol); | |
293d984f | 1708 | |
293d984f PT |
1709 | if (cgdev->state == CCWGROUP_ONLINE) |
1710 | ctcm_shutdown_device(cgdev); | |
1711 | ctcm_remove_files(&cgdev->dev); | |
1712 | dev_set_drvdata(&cgdev->dev, NULL); | |
1713 | kfree(priv); | |
1714 | put_device(&cgdev->dev); | |
1715 | } | |
1716 | ||
b8a2d42a FB |
1717 | static int ctcm_pm_suspend(struct ccwgroup_device *gdev) |
1718 | { | |
1719 | struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev); | |
1720 | ||
1721 | if (gdev->state == CCWGROUP_OFFLINE) | |
1722 | return 0; | |
1723 | netif_device_detach(priv->channel[READ]->netdev); | |
1724 | ctcm_close(priv->channel[READ]->netdev); | |
1e1815be FB |
1725 | if (!wait_event_timeout(priv->fsm->wait_q, |
1726 | fsm_getstate(priv->fsm) == DEV_STATE_STOPPED, CTCM_TIME_5_SEC)) { | |
1727 | netif_device_attach(priv->channel[READ]->netdev); | |
1728 | return -EBUSY; | |
1729 | } | |
b8a2d42a FB |
1730 | ccw_device_set_offline(gdev->cdev[1]); |
1731 | ccw_device_set_offline(gdev->cdev[0]); | |
1732 | return 0; | |
1733 | } | |
1734 | ||
1735 | static int ctcm_pm_resume(struct ccwgroup_device *gdev) | |
1736 | { | |
1737 | struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev); | |
1738 | int rc; | |
1739 | ||
1740 | if (gdev->state == CCWGROUP_OFFLINE) | |
1741 | return 0; | |
1742 | rc = ccw_device_set_online(gdev->cdev[1]); | |
1743 | if (rc) | |
1744 | goto err_out; | |
1745 | rc = ccw_device_set_online(gdev->cdev[0]); | |
1746 | if (rc) | |
1747 | goto err_out; | |
1748 | ctcm_open(priv->channel[READ]->netdev); | |
1749 | err_out: | |
1750 | netif_device_attach(priv->channel[READ]->netdev); | |
1751 | return rc; | |
1752 | } | |
1753 | ||
0ca8cc6f UB |
1754 | static struct ccw_device_id ctcm_ids[] = { |
1755 | {CCW_DEVICE(0x3088, 0x08), .driver_info = ctcm_channel_type_parallel}, | |
1756 | {CCW_DEVICE(0x3088, 0x1e), .driver_info = ctcm_channel_type_ficon}, | |
1757 | {CCW_DEVICE(0x3088, 0x1f), .driver_info = ctcm_channel_type_escon}, | |
1758 | {}, | |
1759 | }; | |
1760 | MODULE_DEVICE_TABLE(ccw, ctcm_ids); | |
1761 | ||
1762 | static struct ccw_driver ctcm_ccw_driver = { | |
1763 | .owner = THIS_MODULE, | |
1764 | .name = "ctcm", | |
1765 | .ids = ctcm_ids, | |
1766 | .probe = ccwgroup_probe_ccwdev, | |
1767 | .remove = ccwgroup_remove_ccwdev, | |
1768 | }; | |
1769 | ||
293d984f PT |
1770 | static struct ccwgroup_driver ctcm_group_driver = { |
1771 | .owner = THIS_MODULE, | |
1772 | .name = CTC_DRIVER_NAME, | |
1773 | .max_slaves = 2, | |
1774 | .driver_id = 0xC3E3C3D4, /* CTCM */ | |
1775 | .probe = ctcm_probe_device, | |
1776 | .remove = ctcm_remove_device, | |
1777 | .set_online = ctcm_new_device, | |
1778 | .set_offline = ctcm_shutdown_device, | |
b8a2d42a FB |
1779 | .freeze = ctcm_pm_suspend, |
1780 | .thaw = ctcm_pm_resume, | |
1781 | .restore = ctcm_pm_resume, | |
293d984f PT |
1782 | }; |
1783 | ||
0ca8cc6f UB |
1784 | static ssize_t |
1785 | ctcm_driver_group_store(struct device_driver *ddrv, const char *buf, | |
1786 | size_t count) | |
1787 | { | |
1788 | int err; | |
1789 | ||
1790 | err = ccwgroup_create_from_string(ctcm_root_dev, | |
1791 | ctcm_group_driver.driver_id, | |
1792 | &ctcm_ccw_driver, 2, buf); | |
1793 | return err ? err : count; | |
1794 | } | |
1795 | ||
1796 | static DRIVER_ATTR(group, 0200, NULL, ctcm_driver_group_store); | |
1797 | ||
1798 | static struct attribute *ctcm_group_attrs[] = { | |
1799 | &driver_attr_group.attr, | |
1800 | NULL, | |
1801 | }; | |
1802 | ||
1803 | static struct attribute_group ctcm_group_attr_group = { | |
1804 | .attrs = ctcm_group_attrs, | |
1805 | }; | |
1806 | ||
302689ac | 1807 | static const struct attribute_group *ctcm_group_attr_groups[] = { |
0ca8cc6f UB |
1808 | &ctcm_group_attr_group, |
1809 | NULL, | |
1810 | }; | |
293d984f PT |
1811 | |
1812 | /* | |
1813 | * Module related routines | |
1814 | */ | |
1815 | ||
1816 | /* | |
1817 | * Prepare to be unloaded. Free IRQ's and release all resources. | |
1818 | * This is called just before this module is unloaded. It is | |
1819 | * not called, if the usage count is !0, so we don't need to check | |
1820 | * for that. | |
1821 | */ | |
1822 | static void __exit ctcm_exit(void) | |
1823 | { | |
0ca8cc6f UB |
1824 | driver_remove_file(&ctcm_group_driver.driver, &driver_attr_group); |
1825 | ccwgroup_driver_unregister(&ctcm_group_driver); | |
1826 | ccw_driver_unregister(&ctcm_ccw_driver); | |
1827 | root_device_unregister(ctcm_root_dev); | |
293d984f | 1828 | ctcm_unregister_dbf_views(); |
2a7c6f2c | 1829 | pr_info("CTCM driver unloaded\n"); |
293d984f PT |
1830 | } |
1831 | ||
1832 | /* | |
1833 | * Print Banner. | |
1834 | */ | |
1835 | static void print_banner(void) | |
1836 | { | |
2a7c6f2c | 1837 | pr_info("CTCM driver initialized\n"); |
293d984f PT |
1838 | } |
1839 | ||
1840 | /** | |
1841 | * Initialize module. | |
1842 | * This is called just after the module is loaded. | |
1843 | * | |
1844 | * returns 0 on success, !0 on error. | |
1845 | */ | |
1846 | static int __init ctcm_init(void) | |
1847 | { | |
1848 | int ret; | |
1849 | ||
1850 | channels = NULL; | |
1851 | ||
1852 | ret = ctcm_register_dbf_views(); | |
0ca8cc6f UB |
1853 | if (ret) |
1854 | goto out_err; | |
1855 | ctcm_root_dev = root_device_register("ctcm"); | |
1856 | ret = IS_ERR(ctcm_root_dev) ? PTR_ERR(ctcm_root_dev) : 0; | |
1857 | if (ret) | |
1858 | goto register_err; | |
1859 | ret = ccw_driver_register(&ctcm_ccw_driver); | |
1860 | if (ret) | |
1861 | goto ccw_err; | |
1862 | ctcm_group_driver.driver.groups = ctcm_group_attr_groups; | |
1863 | ret = ccwgroup_driver_register(&ctcm_group_driver); | |
1864 | if (ret) | |
1865 | goto ccwgroup_err; | |
293d984f | 1866 | print_banner(); |
0ca8cc6f UB |
1867 | return 0; |
1868 | ||
1869 | ccwgroup_err: | |
1870 | ccw_driver_unregister(&ctcm_ccw_driver); | |
1871 | ccw_err: | |
1872 | root_device_unregister(ctcm_root_dev); | |
1873 | register_err: | |
1874 | ctcm_unregister_dbf_views(); | |
1875 | out_err: | |
1876 | pr_err("%s / Initializing the ctcm device driver failed, ret = %d\n", | |
1877 | __func__, ret); | |
293d984f PT |
1878 | return ret; |
1879 | } | |
1880 | ||
1881 | module_init(ctcm_init); | |
1882 | module_exit(ctcm_exit); | |
1883 | ||
1884 | MODULE_AUTHOR("Peter Tiedemann <ptiedem@de.ibm.com>"); | |
1885 | MODULE_DESCRIPTION("Network driver for S/390 CTC + CTCMPC (SNA)"); | |
1886 | MODULE_LICENSE("GPL"); | |
1887 |