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 ret = cio_start (sch, cdev->private->iccws,
75 cdev->private->imask);
76 /* ret is 0, -EBUSY, -EACCES or -ENODEV */
79 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel "
80 "0.%x.%04x, lpm %02X, became 'not "
82 cdev->private->devno, sch->schid.ssid,
83 sch->schid.sch_no, cdev->private->imask);
86 cdev->private->imask >>= 1;
87 cdev->private->iretry = 5;
95 ccw_device_sense_pgid_start(struct ccw_device *cdev)
99 cdev->private->state = DEV_STATE_SENSE_PGID;
100 cdev->private->imask = 0x80;
101 cdev->private->iretry = 5;
102 memset (&cdev->private->pgid, 0, sizeof (cdev->private->pgid));
103 ret = __ccw_device_sense_pgid_start(cdev);
104 if (ret && ret != -EBUSY)
105 ccw_device_sense_pgid_done(cdev, ret);
109 * Called from interrupt context to check if a valid answer
110 * to Sense Path Group ID was received.
113 __ccw_device_check_sense_pgid(struct ccw_device *cdev)
115 struct subchannel *sch;
119 sch = to_subchannel(cdev->dev.parent);
120 irb = &cdev->private->irb;
121 if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
123 if (irb->esw.esw0.erw.cons &&
124 (irb->ecw[0]&(SNS0_CMD_REJECT|SNS0_INTERVENTION_REQ))) {
126 * If the device doesn't support the Sense Path Group ID
127 * command further retries wouldn't help ...
131 if (irb->esw.esw0.erw.cons) {
132 CIO_MSG_EVENT(2, "SNID - device 0.%x.%04x, unit check, "
133 "lpum %02X, cnt %02d, sns : "
134 "%02X%02X%02X%02X %02X%02X%02X%02X ...\n",
135 cdev->private->ssid, cdev->private->devno,
136 irb->esw.esw0.sublog.lpum,
137 irb->esw.esw0.erw.scnt,
138 irb->ecw[0], irb->ecw[1],
139 irb->ecw[2], irb->ecw[3],
140 irb->ecw[4], irb->ecw[5],
141 irb->ecw[6], irb->ecw[7]);
144 if (irb->scsw.cc == 3) {
145 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel 0.%x.%04x,"
146 " lpm %02X, became 'not operational'\n",
147 cdev->private->devno, sch->schid.ssid,
148 sch->schid.sch_no, sch->orb.lpm);
151 i = 8 - ffs(cdev->private->imask);
152 if (cdev->private->pgid[i].inf.ps.state2 == SNID_STATE2_RESVD_ELSE) {
153 CIO_MSG_EVENT(2, "SNID - Device %04x on Subchannel 0.%x.%04x "
154 "is reserved by someone else\n",
155 cdev->private->devno, sch->schid.ssid,
163 * Got interrupt for Sense Path Group ID.
166 ccw_device_sense_pgid_irq(struct ccw_device *cdev, enum dev_event dev_event)
168 struct subchannel *sch;
172 irb = (struct irb *) __LC_IRB;
174 if (irb->scsw.stctl ==
175 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
176 if (__ccw_device_should_retry(&irb->scsw)) {
177 ret = __ccw_device_sense_pgid_start(cdev);
178 if (ret && ret != -EBUSY)
179 ccw_device_sense_pgid_done(cdev, ret);
183 if (ccw_device_accumulate_and_sense(cdev, irb) != 0)
185 sch = to_subchannel(cdev->dev.parent);
186 ret = __ccw_device_check_sense_pgid(cdev);
187 memset(&cdev->private->irb, 0, sizeof(struct irb));
189 /* 0, -ETIME, -EOPNOTSUPP, -EAGAIN, -EACCES or -EUSERS */
190 case -EOPNOTSUPP: /* Sense Path Group ID not supported */
191 ccw_device_sense_pgid_done(cdev, -EOPNOTSUPP);
193 case -ETIME: /* Sense path group id stopped by timeout. */
194 ccw_device_sense_pgid_done(cdev, -ETIME);
196 case -EACCES: /* channel is not operational. */
197 sch->lpm &= ~cdev->private->imask;
199 case 0: /* Sense Path Group ID successful. */
200 cdev->private->imask >>= 1;
201 cdev->private->iretry = 5;
203 case -EAGAIN: /* Try again. */
204 ret = __ccw_device_sense_pgid_start(cdev);
205 if (ret != 0 && ret != -EBUSY)
206 ccw_device_sense_pgid_done(cdev, ret);
208 case -EUSERS: /* device is reserved for someone else. */
209 ccw_device_sense_pgid_done(cdev, -EUSERS);
215 * Path Group ID helper function.
218 __ccw_device_do_pgid(struct ccw_device *cdev, __u8 func)
220 struct subchannel *sch;
224 sch = to_subchannel(cdev->dev.parent);
226 /* Setup sense path group id channel program. */
227 cdev->private->pgid[0].inf.fc = func;
228 ccw = cdev->private->iccws;
229 if (!cdev->private->flags.pgid_single) {
230 cdev->private->pgid[0].inf.fc |= SPID_FUNC_MULTI_PATH;
231 ccw->cmd_code = CCW_CMD_SUSPEND_RECONN;
234 ccw->flags = CCW_FLAG_SLI | CCW_FLAG_CC;
237 cdev->private->pgid[0].inf.fc |= SPID_FUNC_SINGLE_PATH;
239 ccw->cmd_code = CCW_CMD_SET_PGID;
240 ccw->cda = (__u32) __pa (&cdev->private->pgid[0]);
241 ccw->count = sizeof (struct pgid);
242 ccw->flags = CCW_FLAG_SLI;
244 /* Reset device status. */
245 memset(&cdev->private->irb, 0, sizeof(struct irb));
247 /* Try multiple times. */
249 if (cdev->private->iretry > 0) {
250 cdev->private->iretry--;
251 ret = cio_start (sch, cdev->private->iccws,
252 cdev->private->imask);
253 /* We expect an interrupt in case of success or busy
255 if ((ret == 0) || (ret == -EBUSY))
258 /* PGID command failed on this path. */
259 CIO_MSG_EVENT(2, "SPID - Device %04x on Subchannel "
260 "0.%x.%04x, lpm %02X, became 'not operational'\n",
261 cdev->private->devno, sch->schid.ssid,
262 sch->schid.sch_no, cdev->private->imask);
267 * Helper function to send a nop ccw down a path.
269 static int __ccw_device_do_nop(struct ccw_device *cdev)
271 struct subchannel *sch;
275 sch = to_subchannel(cdev->dev.parent);
277 /* Setup nop channel program. */
278 ccw = cdev->private->iccws;
279 ccw->cmd_code = CCW_CMD_NOOP;
282 ccw->flags = CCW_FLAG_SLI;
284 /* Reset device status. */
285 memset(&cdev->private->irb, 0, sizeof(struct irb));
287 /* Try multiple times. */
289 if (cdev->private->iretry > 0) {
290 cdev->private->iretry--;
291 ret = cio_start (sch, cdev->private->iccws,
292 cdev->private->imask);
293 /* We expect an interrupt in case of success or busy
295 if ((ret == 0) || (ret == -EBUSY))
298 /* nop command failed on this path. */
299 CIO_MSG_EVENT(2, "NOP - Device %04x on Subchannel "
300 "0.%x.%04x, lpm %02X, became 'not operational'\n",
301 cdev->private->devno, sch->schid.ssid,
302 sch->schid.sch_no, cdev->private->imask);
308 * Called from interrupt context to check if a valid answer
309 * to Set Path Group ID was received.
312 __ccw_device_check_pgid(struct ccw_device *cdev)
314 struct subchannel *sch;
317 sch = to_subchannel(cdev->dev.parent);
318 irb = &cdev->private->irb;
319 if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
321 if (irb->esw.esw0.erw.cons) {
322 if (irb->ecw[0] & SNS0_CMD_REJECT)
324 /* Hmm, whatever happened, try again. */
325 CIO_MSG_EVENT(2, "SPID - device 0.%x.%04x, unit check, "
327 "sns : %02X%02X%02X%02X %02X%02X%02X%02X ...\n",
329 cdev->private->devno, irb->esw.esw0.erw.scnt,
330 irb->ecw[0], irb->ecw[1],
331 irb->ecw[2], irb->ecw[3],
332 irb->ecw[4], irb->ecw[5],
333 irb->ecw[6], irb->ecw[7]);
336 if (irb->scsw.cc == 3) {
337 CIO_MSG_EVENT(2, "SPID - Device %04x on Subchannel 0.%x.%04x,"
338 " lpm %02X, became 'not operational'\n",
339 cdev->private->devno, sch->schid.ssid,
340 sch->schid.sch_no, cdev->private->imask);
347 * Called from interrupt context to check the path status after a nop has
350 static int __ccw_device_check_nop(struct ccw_device *cdev)
352 struct subchannel *sch;
355 sch = to_subchannel(cdev->dev.parent);
356 irb = &cdev->private->irb;
357 if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
359 if (irb->scsw.cc == 3) {
360 CIO_MSG_EVENT(2, "NOP - Device %04x on Subchannel 0.%x.%04x,"
361 " lpm %02X, became 'not operational'\n",
362 cdev->private->devno, sch->schid.ssid,
363 sch->schid.sch_no, cdev->private->imask);
370 __ccw_device_verify_start(struct ccw_device *cdev)
372 struct subchannel *sch;
376 sch = to_subchannel(cdev->dev.parent);
377 /* Repeat for all paths. */
378 for (; cdev->private->imask; cdev->private->imask >>= 1,
379 cdev->private->iretry = 5) {
380 if ((cdev->private->imask & sch->schib.pmcw.pam) == 0)
381 /* Path not available, try next. */
383 if (cdev->private->options.pgroup) {
384 if (sch->opm & cdev->private->imask)
385 func = SPID_FUNC_ESTABLISH;
387 func = SPID_FUNC_RESIGN;
388 ret = __ccw_device_do_pgid(cdev, func);
390 ret = __ccw_device_do_nop(cdev);
391 /* We expect an interrupt in case of success or busy
393 if (ret == 0 || ret == -EBUSY)
395 /* Permanent path failure, try next. */
397 /* Done with all paths. */
398 ccw_device_verify_done(cdev, (sch->vpm != 0) ? 0 : -ENODEV);
402 * Got interrupt for Set Path Group ID.
405 ccw_device_verify_irq(struct ccw_device *cdev, enum dev_event dev_event)
407 struct subchannel *sch;
411 irb = (struct irb *) __LC_IRB;
413 if (irb->scsw.stctl ==
414 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
415 if (__ccw_device_should_retry(&irb->scsw))
416 __ccw_device_verify_start(cdev);
419 if (ccw_device_accumulate_and_sense(cdev, irb) != 0)
421 sch = to_subchannel(cdev->dev.parent);
422 if (cdev->private->options.pgroup)
423 ret = __ccw_device_check_pgid(cdev);
425 ret = __ccw_device_check_nop(cdev);
426 memset(&cdev->private->irb, 0, sizeof(struct irb));
429 /* 0, -ETIME, -EAGAIN, -EOPNOTSUPP or -EACCES */
431 /* Path verification ccw finished successfully, update lpm. */
432 sch->vpm |= sch->opm & cdev->private->imask;
433 /* Go on with next path. */
434 cdev->private->imask >>= 1;
435 cdev->private->iretry = 5;
436 __ccw_device_verify_start(cdev);
440 * One of those strange devices which claim to be able
441 * to do multipathing but not for Set Path Group ID.
443 if (cdev->private->flags.pgid_single)
444 cdev->private->options.pgroup = 0;
446 cdev->private->flags.pgid_single = 1;
449 cdev->private->imask = 0x80;
450 cdev->private->iretry = 5;
452 case -EAGAIN: /* Try again. */
453 __ccw_device_verify_start(cdev);
455 case -ETIME: /* Set path group id stopped by timeout. */
456 ccw_device_verify_done(cdev, -ETIME);
458 case -EACCES: /* channel is not operational. */
459 cdev->private->imask >>= 1;
460 cdev->private->iretry = 5;
461 __ccw_device_verify_start(cdev);
467 ccw_device_verify_start(struct ccw_device *cdev)
469 struct subchannel *sch = to_subchannel(cdev->dev.parent);
471 cdev->private->flags.pgid_single = 0;
472 cdev->private->imask = 0x80;
473 cdev->private->iretry = 5;
475 /* Start with empty vpm. */
478 /* Get current pam. */
479 if (stsch(sch->schid, &sch->schib)) {
480 ccw_device_verify_done(cdev, -ENODEV);
483 __ccw_device_verify_start(cdev);
487 __ccw_device_disband_start(struct ccw_device *cdev)
489 struct subchannel *sch;
492 sch = to_subchannel(cdev->dev.parent);
493 while (cdev->private->imask != 0) {
494 if (sch->lpm & cdev->private->imask) {
495 ret = __ccw_device_do_pgid(cdev, SPID_FUNC_DISBAND);
499 cdev->private->iretry = 5;
500 cdev->private->imask >>= 1;
502 ccw_device_disband_done(cdev, (sch->lpm != 0) ? 0 : -ENODEV);
506 * Got interrupt for Unset Path Group ID.
509 ccw_device_disband_irq(struct ccw_device *cdev, enum dev_event dev_event)
511 struct subchannel *sch;
515 irb = (struct irb *) __LC_IRB;
517 if (irb->scsw.stctl ==
518 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
519 if (__ccw_device_should_retry(&irb->scsw))
520 __ccw_device_disband_start(cdev);
523 if (ccw_device_accumulate_and_sense(cdev, irb) != 0)
525 sch = to_subchannel(cdev->dev.parent);
526 ret = __ccw_device_check_pgid(cdev);
527 memset(&cdev->private->irb, 0, sizeof(struct irb));
529 /* 0, -ETIME, -EAGAIN, -EOPNOTSUPP or -EACCES */
530 case 0: /* disband successful. */
531 ccw_device_disband_done(cdev, ret);
535 * One of those strange devices which claim to be able
536 * to do multipathing but not for Unset Path Group ID.
538 cdev->private->flags.pgid_single = 1;
540 case -EAGAIN: /* Try again. */
541 __ccw_device_disband_start(cdev);
543 case -ETIME: /* Set path group id stopped by timeout. */
544 ccw_device_disband_done(cdev, -ETIME);
546 case -EACCES: /* channel is not operational. */
547 cdev->private->imask >>= 1;
548 cdev->private->iretry = 5;
549 __ccw_device_disband_start(cdev);
555 ccw_device_disband_start(struct ccw_device *cdev)
557 cdev->private->flags.pgid_single = 0;
558 cdev->private->iretry = 5;
559 cdev->private->imask = 0x80;
560 __ccw_device_disband_start(cdev);