2 * drivers/s390/cio/device_pgid.c
4 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * Path Group ID functions.
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <asm/ccwdev.h>
17 #include <asm/delay.h>
18 #include <asm/lowcore.h>
21 #include "cio_debug.h"
27 * Helper function called from interrupt context to decide whether an
28 * operation should be tried again.
30 static int __ccw_device_should_retry(struct scsw
*scsw
)
32 /* CC is only valid if start function bit is set. */
33 if ((scsw
->fctl
& SCSW_FCTL_START_FUNC
) && scsw
->cc
== 1)
35 /* No more activity. For sense and set PGID we stubbornly try again. */
42 * Start Sense Path Group ID helper function. Used in ccw_device_recog
43 * and ccw_device_sense_pgid.
46 __ccw_device_sense_pgid_start(struct ccw_device
*cdev
)
48 struct subchannel
*sch
;
53 sch
= to_subchannel(cdev
->dev
.parent
);
54 /* Return if we already checked on all paths. */
55 if (cdev
->private->imask
== 0)
56 return (sch
->lpm
== 0) ? -ENODEV
: -EACCES
;
57 i
= 8 - ffs(cdev
->private->imask
);
59 /* Setup sense path group id channel program. */
60 ccw
= cdev
->private->iccws
;
61 ccw
->cmd_code
= CCW_CMD_SENSE_PGID
;
62 ccw
->count
= sizeof (struct pgid
);
63 ccw
->flags
= CCW_FLAG_SLI
;
65 /* Reset device status. */
66 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
67 /* Try on every path. */
69 while (cdev
->private->imask
!= 0) {
70 /* Try every path multiple times. */
71 ccw
->cda
= (__u32
) __pa (&cdev
->private->pgid
[i
]);
72 if (cdev
->private->iretry
> 0) {
73 cdev
->private->iretry
--;
74 /* Reset internal retry indication. */
75 cdev
->private->flags
.intretry
= 0;
76 ret
= cio_start (sch
, cdev
->private->iccws
,
77 cdev
->private->imask
);
78 /* ret is 0, -EBUSY, -EACCES or -ENODEV */
81 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel "
82 "0.%x.%04x, lpm %02X, became 'not "
84 cdev
->private->dev_id
.devno
,
86 sch
->schid
.sch_no
, cdev
->private->imask
);
89 cdev
->private->imask
>>= 1;
90 cdev
->private->iretry
= 5;
98 ccw_device_sense_pgid_start(struct ccw_device
*cdev
)
102 /* Set a timeout of 60s */
103 ccw_device_set_timeout(cdev
, 60*HZ
);
105 cdev
->private->state
= DEV_STATE_SENSE_PGID
;
106 cdev
->private->imask
= 0x80;
107 cdev
->private->iretry
= 5;
108 memset (&cdev
->private->pgid
, 0, sizeof (cdev
->private->pgid
));
109 ret
= __ccw_device_sense_pgid_start(cdev
);
110 if (ret
&& ret
!= -EBUSY
)
111 ccw_device_sense_pgid_done(cdev
, ret
);
115 * Called from interrupt context to check if a valid answer
116 * to Sense Path Group ID was received.
119 __ccw_device_check_sense_pgid(struct ccw_device
*cdev
)
121 struct subchannel
*sch
;
125 sch
= to_subchannel(cdev
->dev
.parent
);
126 irb
= &cdev
->private->irb
;
127 if (irb
->scsw
.fctl
& (SCSW_FCTL_HALT_FUNC
| SCSW_FCTL_CLEAR_FUNC
)) {
128 /* Retry Sense PGID if requested. */
129 if (cdev
->private->flags
.intretry
) {
130 cdev
->private->flags
.intretry
= 0;
135 if (irb
->esw
.esw0
.erw
.cons
&&
136 (irb
->ecw
[0]&(SNS0_CMD_REJECT
|SNS0_INTERVENTION_REQ
))) {
138 * If the device doesn't support the Sense Path Group ID
139 * command further retries wouldn't help ...
143 if (irb
->esw
.esw0
.erw
.cons
) {
144 CIO_MSG_EVENT(2, "SNID - device 0.%x.%04x, unit check, "
145 "lpum %02X, cnt %02d, sns : "
146 "%02X%02X%02X%02X %02X%02X%02X%02X ...\n",
147 cdev
->private->dev_id
.ssid
,
148 cdev
->private->dev_id
.devno
,
149 irb
->esw
.esw0
.sublog
.lpum
,
150 irb
->esw
.esw0
.erw
.scnt
,
151 irb
->ecw
[0], irb
->ecw
[1],
152 irb
->ecw
[2], irb
->ecw
[3],
153 irb
->ecw
[4], irb
->ecw
[5],
154 irb
->ecw
[6], irb
->ecw
[7]);
157 if (irb
->scsw
.cc
== 3) {
158 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel 0.%x.%04x,"
159 " lpm %02X, became 'not operational'\n",
160 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
161 sch
->schid
.sch_no
, sch
->orb
.lpm
);
164 i
= 8 - ffs(cdev
->private->imask
);
165 if (cdev
->private->pgid
[i
].inf
.ps
.state2
== SNID_STATE2_RESVD_ELSE
) {
166 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel 0.%x.%04x "
167 "is reserved by someone else\n",
168 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
176 * Got interrupt for Sense Path Group ID.
179 ccw_device_sense_pgid_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
181 struct subchannel
*sch
;
185 irb
= (struct irb
*) __LC_IRB
;
187 if (irb
->scsw
.stctl
==
188 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
189 if (__ccw_device_should_retry(&irb
->scsw
)) {
190 ret
= __ccw_device_sense_pgid_start(cdev
);
191 if (ret
&& ret
!= -EBUSY
)
192 ccw_device_sense_pgid_done(cdev
, ret
);
196 if (ccw_device_accumulate_and_sense(cdev
, irb
) != 0)
198 sch
= to_subchannel(cdev
->dev
.parent
);
199 ret
= __ccw_device_check_sense_pgid(cdev
);
200 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
202 /* 0, -ETIME, -EOPNOTSUPP, -EAGAIN, -EACCES or -EUSERS */
203 case -EOPNOTSUPP
: /* Sense Path Group ID not supported */
204 ccw_device_sense_pgid_done(cdev
, -EOPNOTSUPP
);
206 case -ETIME
: /* Sense path group id stopped by timeout. */
207 ccw_device_sense_pgid_done(cdev
, -ETIME
);
209 case -EACCES
: /* channel is not operational. */
210 sch
->lpm
&= ~cdev
->private->imask
;
212 case 0: /* Sense Path Group ID successful. */
213 cdev
->private->imask
>>= 1;
214 cdev
->private->iretry
= 5;
216 case -EAGAIN
: /* Try again. */
217 ret
= __ccw_device_sense_pgid_start(cdev
);
218 if (ret
!= 0 && ret
!= -EBUSY
)
219 ccw_device_sense_pgid_done(cdev
, ret
);
221 case -EUSERS
: /* device is reserved for someone else. */
222 ccw_device_sense_pgid_done(cdev
, -EUSERS
);
228 * Path Group ID helper function.
231 __ccw_device_do_pgid(struct ccw_device
*cdev
, __u8 func
)
233 struct subchannel
*sch
;
237 sch
= to_subchannel(cdev
->dev
.parent
);
239 /* Setup sense path group id channel program. */
240 cdev
->private->pgid
[0].inf
.fc
= func
;
241 ccw
= cdev
->private->iccws
;
242 if (!cdev
->private->flags
.pgid_single
) {
243 cdev
->private->pgid
[0].inf
.fc
|= SPID_FUNC_MULTI_PATH
;
244 ccw
->cmd_code
= CCW_CMD_SUSPEND_RECONN
;
247 ccw
->flags
= CCW_FLAG_SLI
| CCW_FLAG_CC
;
250 cdev
->private->pgid
[0].inf
.fc
|= SPID_FUNC_SINGLE_PATH
;
252 ccw
->cmd_code
= CCW_CMD_SET_PGID
;
253 ccw
->cda
= (__u32
) __pa (&cdev
->private->pgid
[0]);
254 ccw
->count
= sizeof (struct pgid
);
255 ccw
->flags
= CCW_FLAG_SLI
;
257 /* Reset device status. */
258 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
260 /* Try multiple times. */
262 if (cdev
->private->iretry
> 0) {
263 cdev
->private->iretry
--;
264 /* Reset internal retry indication. */
265 cdev
->private->flags
.intretry
= 0;
266 ret
= cio_start (sch
, cdev
->private->iccws
,
267 cdev
->private->imask
);
268 /* We expect an interrupt in case of success or busy
270 if ((ret
== 0) || (ret
== -EBUSY
))
273 /* PGID command failed on this path. */
274 CIO_MSG_EVENT(2, "SPID - Device %04x on Subchannel "
275 "0.%x.%04x, lpm %02X, became 'not operational'\n",
276 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
277 sch
->schid
.sch_no
, cdev
->private->imask
);
282 * Helper function to send a nop ccw down a path.
284 static int __ccw_device_do_nop(struct ccw_device
*cdev
)
286 struct subchannel
*sch
;
290 sch
= to_subchannel(cdev
->dev
.parent
);
292 /* Setup nop channel program. */
293 ccw
= cdev
->private->iccws
;
294 ccw
->cmd_code
= CCW_CMD_NOOP
;
297 ccw
->flags
= CCW_FLAG_SLI
;
299 /* Reset device status. */
300 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
302 /* Try multiple times. */
304 if (cdev
->private->iretry
> 0) {
305 cdev
->private->iretry
--;
306 /* Reset internal retry indication. */
307 cdev
->private->flags
.intretry
= 0;
308 ret
= cio_start (sch
, cdev
->private->iccws
,
309 cdev
->private->imask
);
310 /* We expect an interrupt in case of success or busy
312 if ((ret
== 0) || (ret
== -EBUSY
))
315 /* nop command failed on this path. */
316 CIO_MSG_EVENT(2, "NOP - Device %04x on Subchannel "
317 "0.%x.%04x, lpm %02X, became 'not operational'\n",
318 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
319 sch
->schid
.sch_no
, cdev
->private->imask
);
325 * Called from interrupt context to check if a valid answer
326 * to Set Path Group ID was received.
329 __ccw_device_check_pgid(struct ccw_device
*cdev
)
331 struct subchannel
*sch
;
334 sch
= to_subchannel(cdev
->dev
.parent
);
335 irb
= &cdev
->private->irb
;
336 if (irb
->scsw
.fctl
& (SCSW_FCTL_HALT_FUNC
| SCSW_FCTL_CLEAR_FUNC
)) {
337 /* Retry Set PGID if requested. */
338 if (cdev
->private->flags
.intretry
) {
339 cdev
->private->flags
.intretry
= 0;
344 if (irb
->esw
.esw0
.erw
.cons
) {
345 if (irb
->ecw
[0] & SNS0_CMD_REJECT
)
347 /* Hmm, whatever happened, try again. */
348 CIO_MSG_EVENT(2, "SPID - device 0.%x.%04x, unit check, "
350 "sns : %02X%02X%02X%02X %02X%02X%02X%02X ...\n",
351 cdev
->private->dev_id
.ssid
,
352 cdev
->private->dev_id
.devno
,
353 irb
->esw
.esw0
.erw
.scnt
,
354 irb
->ecw
[0], irb
->ecw
[1],
355 irb
->ecw
[2], irb
->ecw
[3],
356 irb
->ecw
[4], irb
->ecw
[5],
357 irb
->ecw
[6], irb
->ecw
[7]);
360 if (irb
->scsw
.cc
== 3) {
361 CIO_MSG_EVENT(2, "SPID - Device %04x on Subchannel 0.%x.%04x,"
362 " lpm %02X, became 'not operational'\n",
363 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
364 sch
->schid
.sch_no
, cdev
->private->imask
);
371 * Called from interrupt context to check the path status after a nop has
374 static int __ccw_device_check_nop(struct ccw_device
*cdev
)
376 struct subchannel
*sch
;
379 sch
= to_subchannel(cdev
->dev
.parent
);
380 irb
= &cdev
->private->irb
;
381 if (irb
->scsw
.fctl
& (SCSW_FCTL_HALT_FUNC
| SCSW_FCTL_CLEAR_FUNC
)) {
382 /* Retry NOP if requested. */
383 if (cdev
->private->flags
.intretry
) {
384 cdev
->private->flags
.intretry
= 0;
389 if (irb
->scsw
.cc
== 3) {
390 CIO_MSG_EVENT(2, "NOP - Device %04x on Subchannel 0.%x.%04x,"
391 " lpm %02X, became 'not operational'\n",
392 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
393 sch
->schid
.sch_no
, cdev
->private->imask
);
400 __ccw_device_verify_start(struct ccw_device
*cdev
)
402 struct subchannel
*sch
;
406 sch
= to_subchannel(cdev
->dev
.parent
);
407 /* Repeat for all paths. */
408 for (; cdev
->private->imask
; cdev
->private->imask
>>= 1,
409 cdev
->private->iretry
= 5) {
410 if ((cdev
->private->imask
& sch
->schib
.pmcw
.pam
) == 0)
411 /* Path not available, try next. */
413 if (cdev
->private->options
.pgroup
) {
414 if (sch
->opm
& cdev
->private->imask
)
415 func
= SPID_FUNC_ESTABLISH
;
417 func
= SPID_FUNC_RESIGN
;
418 ret
= __ccw_device_do_pgid(cdev
, func
);
420 ret
= __ccw_device_do_nop(cdev
);
421 /* We expect an interrupt in case of success or busy
423 if (ret
== 0 || ret
== -EBUSY
)
425 /* Permanent path failure, try next. */
427 /* Done with all paths. */
428 ccw_device_verify_done(cdev
, (sch
->vpm
!= 0) ? 0 : -ENODEV
);
432 * Got interrupt for Set Path Group ID.
435 ccw_device_verify_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
437 struct subchannel
*sch
;
441 irb
= (struct irb
*) __LC_IRB
;
443 if (irb
->scsw
.stctl
==
444 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
445 if (__ccw_device_should_retry(&irb
->scsw
))
446 __ccw_device_verify_start(cdev
);
449 if (ccw_device_accumulate_and_sense(cdev
, irb
) != 0)
451 sch
= to_subchannel(cdev
->dev
.parent
);
452 if (cdev
->private->options
.pgroup
)
453 ret
= __ccw_device_check_pgid(cdev
);
455 ret
= __ccw_device_check_nop(cdev
);
456 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
459 /* 0, -ETIME, -EAGAIN, -EOPNOTSUPP or -EACCES */
461 /* Path verification ccw finished successfully, update lpm. */
462 sch
->vpm
|= sch
->opm
& cdev
->private->imask
;
463 /* Go on with next path. */
464 cdev
->private->imask
>>= 1;
465 cdev
->private->iretry
= 5;
466 __ccw_device_verify_start(cdev
);
470 * One of those strange devices which claim to be able
471 * to do multipathing but not for Set Path Group ID.
473 if (cdev
->private->flags
.pgid_single
)
474 cdev
->private->options
.pgroup
= 0;
476 cdev
->private->flags
.pgid_single
= 1;
479 cdev
->private->imask
= 0x80;
480 cdev
->private->iretry
= 5;
482 case -EAGAIN
: /* Try again. */
483 __ccw_device_verify_start(cdev
);
485 case -ETIME
: /* Set path group id stopped by timeout. */
486 ccw_device_verify_done(cdev
, -ETIME
);
488 case -EACCES
: /* channel is not operational. */
489 cdev
->private->imask
>>= 1;
490 cdev
->private->iretry
= 5;
491 __ccw_device_verify_start(cdev
);
497 ccw_device_verify_start(struct ccw_device
*cdev
)
499 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
501 cdev
->private->flags
.pgid_single
= 0;
502 cdev
->private->imask
= 0x80;
503 cdev
->private->iretry
= 5;
505 /* Start with empty vpm. */
508 /* Get current pam. */
509 if (stsch(sch
->schid
, &sch
->schib
)) {
510 ccw_device_verify_done(cdev
, -ENODEV
);
513 /* After 60s path verification is considered to have failed. */
514 ccw_device_set_timeout(cdev
, 60*HZ
);
515 __ccw_device_verify_start(cdev
);
519 __ccw_device_disband_start(struct ccw_device
*cdev
)
521 struct subchannel
*sch
;
524 sch
= to_subchannel(cdev
->dev
.parent
);
525 while (cdev
->private->imask
!= 0) {
526 if (sch
->lpm
& cdev
->private->imask
) {
527 ret
= __ccw_device_do_pgid(cdev
, SPID_FUNC_DISBAND
);
531 cdev
->private->iretry
= 5;
532 cdev
->private->imask
>>= 1;
534 ccw_device_disband_done(cdev
, (sch
->lpm
!= 0) ? 0 : -ENODEV
);
538 * Got interrupt for Unset Path Group ID.
541 ccw_device_disband_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
543 struct subchannel
*sch
;
547 irb
= (struct irb
*) __LC_IRB
;
549 if (irb
->scsw
.stctl
==
550 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
551 if (__ccw_device_should_retry(&irb
->scsw
))
552 __ccw_device_disband_start(cdev
);
555 if (ccw_device_accumulate_and_sense(cdev
, irb
) != 0)
557 sch
= to_subchannel(cdev
->dev
.parent
);
558 ret
= __ccw_device_check_pgid(cdev
);
559 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
561 /* 0, -ETIME, -EAGAIN, -EOPNOTSUPP or -EACCES */
562 case 0: /* disband successful. */
563 ccw_device_disband_done(cdev
, ret
);
567 * One of those strange devices which claim to be able
568 * to do multipathing but not for Unset Path Group ID.
570 cdev
->private->flags
.pgid_single
= 1;
572 case -EAGAIN
: /* Try again. */
573 __ccw_device_disband_start(cdev
);
575 case -ETIME
: /* Set path group id stopped by timeout. */
576 ccw_device_disband_done(cdev
, -ETIME
);
578 case -EACCES
: /* channel is not operational. */
579 cdev
->private->imask
>>= 1;
580 cdev
->private->iretry
= 5;
581 __ccw_device_disband_start(cdev
);
587 ccw_device_disband_start(struct ccw_device
*cdev
)
589 /* After 60s disbanding is considered to have failed. */
590 ccw_device_set_timeout(cdev
, 60*HZ
);
592 cdev
->private->flags
.pgid_single
= 0;
593 cdev
->private->iretry
= 5;
594 cdev
->private->imask
= 0x80;
595 __ccw_device_disband_start(cdev
);