device_fsm.c

来自「linux 内核源代码」· C语言 代码 · 共 1,244 行 · 第 1/3 页

C
1,244
字号
/* * drivers/s390/cio/device_fsm.c * finite state machine for device handling * *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, *			 IBM Corporation *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com) *		 Martin Schwidefsky (schwidefsky@de.ibm.com) */#include <linux/module.h>#include <linux/init.h>#include <linux/jiffies.h>#include <linux/string.h>#include <asm/ccwdev.h>#include <asm/cio.h>#include <asm/chpid.h>#include "cio.h"#include "cio_debug.h"#include "css.h"#include "device.h"#include "chsc.h"#include "ioasm.h"#include "chp.h"intdevice_is_online(struct subchannel *sch){	struct ccw_device *cdev;	if (!sch->dev.driver_data)		return 0;	cdev = sch->dev.driver_data;	return (cdev->private->state == DEV_STATE_ONLINE);}intdevice_is_disconnected(struct subchannel *sch){	struct ccw_device *cdev;	if (!sch->dev.driver_data)		return 0;	cdev = sch->dev.driver_data;	return (cdev->private->state == DEV_STATE_DISCONNECTED ||		cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);}voiddevice_set_disconnected(struct subchannel *sch){	struct ccw_device *cdev;	if (!sch->dev.driver_data)		return;	cdev = sch->dev.driver_data;	ccw_device_set_timeout(cdev, 0);	cdev->private->flags.fake_irb = 0;	cdev->private->state = DEV_STATE_DISCONNECTED;}void device_set_intretry(struct subchannel *sch){	struct ccw_device *cdev;	cdev = sch->dev.driver_data;	if (!cdev)		return;	cdev->private->flags.intretry = 1;}int device_trigger_verify(struct subchannel *sch){	struct ccw_device *cdev;	cdev = sch->dev.driver_data;	if (!cdev || !cdev->online)		return -EINVAL;	dev_fsm_event(cdev, DEV_EVENT_VERIFY);	return 0;}/* * Timeout function. It just triggers a DEV_EVENT_TIMEOUT. */static voidccw_device_timeout(unsigned long data){	struct ccw_device *cdev;	cdev = (struct ccw_device *) data;	spin_lock_irq(cdev->ccwlock);	dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);	spin_unlock_irq(cdev->ccwlock);}/* * Set timeout */voidccw_device_set_timeout(struct ccw_device *cdev, int expires){	if (expires == 0) {		del_timer(&cdev->private->timer);		return;	}	if (timer_pending(&cdev->private->timer)) {		if (mod_timer(&cdev->private->timer, jiffies + expires))			return;	}	cdev->private->timer.function = ccw_device_timeout;	cdev->private->timer.data = (unsigned long) cdev;	cdev->private->timer.expires = jiffies + expires;	add_timer(&cdev->private->timer);}/* Kill any pending timers after machine check. */voiddevice_kill_pending_timer(struct subchannel *sch){	struct ccw_device *cdev;	if (!sch->dev.driver_data)		return;	cdev = sch->dev.driver_data;	ccw_device_set_timeout(cdev, 0);}/* * Cancel running i/o. This is called repeatedly since halt/clear are * asynchronous operations. We do one try with cio_cancel, two tries * with cio_halt, 255 tries with cio_clear. If everythings fails panic. * Returns 0 if device now idle, -ENODEV for device not operational and * -EBUSY if an interrupt is expected (either from halt/clear or from a * status pending). */intccw_device_cancel_halt_clear(struct ccw_device *cdev){	struct subchannel *sch;	int ret;	sch = to_subchannel(cdev->dev.parent);	ret = stsch(sch->schid, &sch->schib);	if (ret || !sch->schib.pmcw.dnv)		return -ENODEV; 	if (!sch->schib.pmcw.ena)		/* Not operational -> done. */		return 0;	/* Stage 1: cancel io. */	if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&	    !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {		ret = cio_cancel(sch);		if (ret != -EINVAL)			return ret;		/* cancel io unsuccessful. From now on it is asynchronous. */		cdev->private->iretry = 3;	/* 3 halt retries. */	}	if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {		/* Stage 2: halt io. */		if (cdev->private->iretry) {			cdev->private->iretry--;			ret = cio_halt(sch);			if (ret != -EBUSY)				return (ret == 0) ? -EBUSY : ret;		}		/* halt io unsuccessful. */		cdev->private->iretry = 255;	/* 255 clear retries. */	}	/* Stage 3: clear io. */	if (cdev->private->iretry) {		cdev->private->iretry--;		ret = cio_clear (sch);		return (ret == 0) ? -EBUSY : ret;	}	panic("Can't stop i/o on subchannel.\n");}static intccw_device_handle_oper(struct ccw_device *cdev){	struct subchannel *sch;	sch = to_subchannel(cdev->dev.parent);	cdev->private->flags.recog_done = 1;	/*	 * Check if cu type and device type still match. If	 * not, it is certainly another device and we have to	 * de- and re-register.	 */	if (cdev->id.cu_type != cdev->private->senseid.cu_type ||	    cdev->id.cu_model != cdev->private->senseid.cu_model ||	    cdev->id.dev_type != cdev->private->senseid.dev_type ||	    cdev->id.dev_model != cdev->private->senseid.dev_model) {		PREPARE_WORK(&cdev->private->kick_work,			     ccw_device_do_unreg_rereg);		queue_work(ccw_device_work, &cdev->private->kick_work);		return 0;	}	cdev->private->flags.donotify = 1;	return 1;}/* * The machine won't give us any notification by machine check if a chpid has * been varied online on the SE so we have to find out by magic (i. e. driving * the channel subsystem to device selection and updating our path masks). */static void__recover_lost_chpids(struct subchannel *sch, int old_lpm){	int mask, i;	struct chp_id chpid;	chp_id_init(&chpid);	for (i = 0; i<8; i++) {		mask = 0x80 >> i;		if (!(sch->lpm & mask))			continue;		if (old_lpm & mask)			continue;		chpid.id = sch->schib.pmcw.chpid[i];		if (!chp_is_registered(chpid))			css_schedule_eval_all();	}}/* * Stop device recognition. */static voidccw_device_recog_done(struct ccw_device *cdev, int state){	struct subchannel *sch;	int notify, old_lpm, same_dev;	sch = to_subchannel(cdev->dev.parent);	ccw_device_set_timeout(cdev, 0);	cio_disable_subchannel(sch);	/*	 * Now that we tried recognition, we have performed device selection	 * through ssch() and the path information is up to date.	 */	old_lpm = sch->lpm;	stsch(sch->schid, &sch->schib);	sch->lpm = sch->schib.pmcw.pam & sch->opm;	/* Check since device may again have become not operational. */	if (!sch->schib.pmcw.dnv)		state = DEV_STATE_NOT_OPER;	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)		/* Force reprobe on all chpids. */		old_lpm = 0;	if (sch->lpm != old_lpm)		__recover_lost_chpids(sch, old_lpm);	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {		if (state == DEV_STATE_NOT_OPER) {			cdev->private->flags.recog_done = 1;			cdev->private->state = DEV_STATE_DISCONNECTED;			return;		}		/* Boxed devices don't need extra treatment. */	}	notify = 0;	same_dev = 0; /* Keep the compiler quiet... */	switch (state) {	case DEV_STATE_NOT_OPER:		CIO_DEBUG(KERN_WARNING, 2,			  "cio: SenseID : unknown device %04x on subchannel "			  "0.%x.%04x\n", cdev->private->dev_id.devno,			  sch->schid.ssid, sch->schid.sch_no);		break;	case DEV_STATE_OFFLINE:		if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {			same_dev = ccw_device_handle_oper(cdev);			notify = 1;		}		/* fill out sense information */		memset(&cdev->id, 0, sizeof(cdev->id));		cdev->id.cu_type   = cdev->private->senseid.cu_type;		cdev->id.cu_model  = cdev->private->senseid.cu_model;		cdev->id.dev_type  = cdev->private->senseid.dev_type;		cdev->id.dev_model = cdev->private->senseid.dev_model;		if (notify) {			cdev->private->state = DEV_STATE_OFFLINE;			if (same_dev) {				/* Get device online again. */				ccw_device_online(cdev);				wake_up(&cdev->private->wait_q);			}			return;		}		/* Issue device info message. */		CIO_DEBUG(KERN_INFO, 2,			  "cio: SenseID : device 0.%x.%04x reports: "			  "CU  Type/Mod = %04X/%02X, Dev Type/Mod = "			  "%04X/%02X\n",			  cdev->private->dev_id.ssid,			  cdev->private->dev_id.devno,			  cdev->id.cu_type, cdev->id.cu_model,			  cdev->id.dev_type, cdev->id.dev_model);		break;	case DEV_STATE_BOXED:		CIO_DEBUG(KERN_WARNING, 2,			  "cio: SenseID : boxed device %04x on subchannel "			  "0.%x.%04x\n", cdev->private->dev_id.devno,			  sch->schid.ssid, sch->schid.sch_no);		break;	}	cdev->private->state = state;	io_subchannel_recog_done(cdev);	if (state != DEV_STATE_NOT_OPER)		wake_up(&cdev->private->wait_q);}/* * Function called from device_id.c after sense id has completed. */voidccw_device_sense_id_done(struct ccw_device *cdev, int err){	switch (err) {	case 0:		ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);		break;	case -ETIME:		/* Sense id stopped by timeout. */		ccw_device_recog_done(cdev, DEV_STATE_BOXED);		break;	default:		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);		break;	}}static voidccw_device_oper_notify(struct work_struct *work){	struct ccw_device_private *priv;	struct ccw_device *cdev;	struct subchannel *sch;	int ret;	unsigned long flags;	priv = container_of(work, struct ccw_device_private, kick_work);	cdev = priv->cdev;	spin_lock_irqsave(cdev->ccwlock, flags);	sch = to_subchannel(cdev->dev.parent);	if (sch->driver && sch->driver->notify) {		spin_unlock_irqrestore(cdev->ccwlock, flags);		ret = sch->driver->notify(&sch->dev, CIO_OPER);		spin_lock_irqsave(cdev->ccwlock, flags);	} else		ret = 0;	if (ret) {		/* Reenable channel measurements, if needed. */		spin_unlock_irqrestore(cdev->ccwlock, flags);		cmf_reenable(cdev);		spin_lock_irqsave(cdev->ccwlock, flags);		wake_up(&cdev->private->wait_q);	}	spin_unlock_irqrestore(cdev->ccwlock, flags);	if (!ret)		/* Driver doesn't want device back. */		ccw_device_do_unreg_rereg(work);}/* * Finished with online/offline processing. */static voidccw_device_done(struct ccw_device *cdev, int state){	struct subchannel *sch;	sch = to_subchannel(cdev->dev.parent);	ccw_device_set_timeout(cdev, 0);	if (state != DEV_STATE_ONLINE)		cio_disable_subchannel(sch);	/* Reset device status. */	memset(&cdev->private->irb, 0, sizeof(struct irb));	cdev->private->state = state;	if (state == DEV_STATE_BOXED)		CIO_DEBUG(KERN_WARNING, 2,			  "cio: Boxed device %04x on subchannel %04x\n",			  cdev->private->dev_id.devno, sch->schid.sch_no);	if (cdev->private->flags.donotify) {		cdev->private->flags.donotify = 0;		PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);		queue_work(ccw_device_notify_work, &cdev->private->kick_work);	}	wake_up(&cdev->private->wait_q);	if (css_init_done && state != DEV_STATE_ONLINE)		put_device (&cdev->dev);}static int cmp_pgid(struct pgid *p1, struct pgid *p2){	char *c1;	char *c2;	c1 = (char *)p1;	c2 = (char *)p2;	return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?