blob: 2d0efee8a290c54ff9564a156ae05cb8c952a7bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Sebastian Ott823d4942009-06-16 10:30:20 +02002 * Copyright IBM Corp. 2002, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ott823d4942009-06-16 10:30:20 +02004 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
5 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/errno.h>
10#include <linux/slab.h>
11#include <linux/list.h>
12#include <linux/device.h>
13#include <linux/delay.h>
14
15#include <asm/ccwdev.h>
16#include <asm/idals.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020017#include <asm/chpid.h>
Peter Oberparleiter83262d62008-07-14 09:58:51 +020018#include <asm/fcx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "cio.h"
21#include "cio_debug.h"
22#include "css.h"
23#include "chsc.h"
24#include "device.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020025#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +020027/**
28 * ccw_device_set_options_mask() - set some options and unset the rest
29 * @cdev: device for which the options are to be set
30 * @flags: options to be set
31 *
32 * All flags specified in @flags are set, all flags not specified in @flags
33 * are cleared.
34 * Returns:
35 * %0 on success, -%EINVAL on an invalid flag combination.
36 */
Cornelia Huck4dd3cc52007-02-12 15:47:18 +010037int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 /*
40 * The flag usage is mutal exclusive ...
41 */
42 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
43 (flags & CCWDEV_REPORT_ALL))
44 return -EINVAL;
45 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
46 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
47 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
48 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
49 return 0;
50}
51
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +020052/**
53 * ccw_device_set_options() - set some options
54 * @cdev: device for which the options are to be set
55 * @flags: options to be set
56 *
57 * All flags specified in @flags are set, the remainder is left untouched.
58 * Returns:
59 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
60 */
Cornelia Huck4dd3cc52007-02-12 15:47:18 +010061int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
62{
63 /*
64 * The flag usage is mutal exclusive ...
65 */
66 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
67 (flags & CCWDEV_REPORT_ALL)) ||
68 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
69 cdev->private->options.repall) ||
70 ((flags & CCWDEV_REPORT_ALL) &&
71 cdev->private->options.fast))
72 return -EINVAL;
73 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
74 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
75 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
76 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
77 return 0;
78}
79
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +020080/**
81 * ccw_device_clear_options() - clear some options
82 * @cdev: device for which the options are to be cleared
83 * @flags: options to be cleared
84 *
85 * All flags specified in @flags are cleared, the remainder is left untouched.
86 */
Cornelia Huck4dd3cc52007-02-12 15:47:18 +010087void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
88{
89 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
90 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
91 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
92 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
93}
94
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +020095/**
96 * ccw_device_clear() - terminate I/O request processing
97 * @cdev: target ccw device
98 * @intparm: interruption parameter; value is only used if no I/O is
99 * outstanding, otherwise the intparm associated with the I/O request
100 * is returned
101 *
102 * ccw_device_clear() calls csch on @cdev's subchannel.
103 * Returns:
104 * %0 on success,
105 * -%ENODEV on device not operational,
106 * -%EINVAL on invalid device state.
107 * Context:
108 * Interrupts disabled, ccw device lock held
109 */
110int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct subchannel *sch;
113 int ret;
114
Sebastian Otte45efa92009-06-12 10:26:27 +0200115 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +0200117 sch = to_subchannel(cdev->dev.parent);
118 if (!sch->schib.pmcw.ena)
119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (cdev->private->state == DEV_STATE_NOT_OPER)
121 return -ENODEV;
122 if (cdev->private->state != DEV_STATE_ONLINE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 cdev->private->state != DEV_STATE_W4SENSE)
124 return -EINVAL;
Sebastian Ott823d4942009-06-16 10:30:20 +0200125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ret = cio_clear(sch);
127 if (ret == 0)
128 cdev->private->intparm = intparm;
129 return ret;
130}
131
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200132/**
133 * ccw_device_start_key() - start a s390 channel program with key
134 * @cdev: target ccw device
135 * @cpa: logical start address of channel program
136 * @intparm: user specific interruption parameter; will be presented back to
137 * @cdev's interrupt handler. Allows a device driver to associate
138 * the interrupt with a particular I/O request.
139 * @lpm: defines the channel path to be used for a specific I/O request. A
140 * value of 0 will make cio use the opm.
141 * @key: storage key to be used for the I/O
142 * @flags: additional flags; defines the action to be performed for I/O
143 * processing.
144 *
145 * Start a S/390 channel program. When the interrupt arrives, the
146 * IRQ handler is called, either immediately, delayed (dev-end missing,
147 * or sense required) or never (no IRQ handler registered).
148 * Returns:
149 * %0, if the operation was successful;
150 * -%EBUSY, if the device is busy, or status pending;
151 * -%EACCES, if no path specified in @lpm is operational;
152 * -%ENODEV, if the device is not operational.
153 * Context:
154 * Interrupts disabled, ccw device lock held
155 */
156int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
157 unsigned long intparm, __u8 lpm, __u8 key,
158 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct subchannel *sch;
161 int ret;
162
Sebastian Otte45efa92009-06-12 10:26:27 +0200163 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENODEV;
165 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +0200166 if (!sch->schib.pmcw.ena)
167 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (cdev->private->state == DEV_STATE_NOT_OPER)
169 return -ENODEV;
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200170 if (cdev->private->state == DEV_STATE_VERIFY ||
171 cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* Remember to fake irb when finished. */
173 if (!cdev->private->flags.fake_irb) {
174 cdev->private->flags.fake_irb = 1;
175 cdev->private->intparm = intparm;
176 return 0;
177 } else
178 /* There's already a fake I/O around. */
179 return -EBUSY;
180 }
181 if (cdev->private->state != DEV_STATE_ONLINE ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200182 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
183 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 cdev->private->flags.doverify)
185 return -EBUSY;
186 ret = cio_set_options (sch, flags);
187 if (ret)
188 return ret;
Peter Oberparleitere0e32c82006-09-20 15:59:57 +0200189 /* Adjust requested path mask to excluded varied off paths. */
190 if (lpm) {
191 lpm &= sch->opm;
192 if (lpm == 0)
193 return -EACCES;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ret = cio_start_key (sch, cpa, lpm, key);
Cornelia Huckfe6173d2008-04-17 07:46:00 +0200196 switch (ret) {
197 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cdev->private->intparm = intparm;
Cornelia Huckfe6173d2008-04-17 07:46:00 +0200199 break;
200 case -EACCES:
201 case -ENODEV:
202 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
203 break;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret;
206}
207
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200208/**
209 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
210 * @cdev: target ccw device
211 * @cpa: logical start address of channel program
212 * @intparm: user specific interruption parameter; will be presented back to
213 * @cdev's interrupt handler. Allows a device driver to associate
214 * the interrupt with a particular I/O request.
215 * @lpm: defines the channel path to be used for a specific I/O request. A
216 * value of 0 will make cio use the opm.
217 * @key: storage key to be used for the I/O
218 * @flags: additional flags; defines the action to be performed for I/O
219 * processing.
220 * @expires: timeout value in jiffies
221 *
222 * Start a S/390 channel program. When the interrupt arrives, the
223 * IRQ handler is called, either immediately, delayed (dev-end missing,
224 * or sense required) or never (no IRQ handler registered).
225 * This function notifies the device driver if the channel program has not
226 * completed during the time specified by @expires. If a timeout occurs, the
227 * channel program is terminated via xsch, hsch or csch, and the device's
228 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
229 * Returns:
230 * %0, if the operation was successful;
231 * -%EBUSY, if the device is busy, or status pending;
232 * -%EACCES, if no path specified in @lpm is operational;
233 * -%ENODEV, if the device is not operational.
234 * Context:
235 * Interrupts disabled, ccw device lock held
236 */
237int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
238 unsigned long intparm, __u8 lpm, __u8 key,
239 unsigned long flags, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int ret;
242
243 if (!cdev)
244 return -ENODEV;
245 ccw_device_set_timeout(cdev, expires);
246 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
247 if (ret != 0)
248 ccw_device_set_timeout(cdev, 0);
249 return ret;
250}
251
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200252/**
253 * ccw_device_start() - start a s390 channel program
254 * @cdev: target ccw device
255 * @cpa: logical start address of channel program
256 * @intparm: user specific interruption parameter; will be presented back to
257 * @cdev's interrupt handler. Allows a device driver to associate
258 * the interrupt with a particular I/O request.
259 * @lpm: defines the channel path to be used for a specific I/O request. A
260 * value of 0 will make cio use the opm.
261 * @flags: additional flags; defines the action to be performed for I/O
262 * processing.
263 *
264 * Start a S/390 channel program. When the interrupt arrives, the
265 * IRQ handler is called, either immediately, delayed (dev-end missing,
266 * or sense required) or never (no IRQ handler registered).
267 * Returns:
268 * %0, if the operation was successful;
269 * -%EBUSY, if the device is busy, or status pending;
270 * -%EACCES, if no path specified in @lpm is operational;
271 * -%ENODEV, if the device is not operational.
272 * Context:
273 * Interrupts disabled, ccw device lock held
274 */
275int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
276 unsigned long intparm, __u8 lpm, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 return ccw_device_start_key(cdev, cpa, intparm, lpm,
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700279 PAGE_DEFAULT_KEY, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200282/**
283 * ccw_device_start_timeout() - start a s390 channel program with timeout
284 * @cdev: target ccw device
285 * @cpa: logical start address of channel program
286 * @intparm: user specific interruption parameter; will be presented back to
287 * @cdev's interrupt handler. Allows a device driver to associate
288 * the interrupt with a particular I/O request.
289 * @lpm: defines the channel path to be used for a specific I/O request. A
290 * value of 0 will make cio use the opm.
291 * @flags: additional flags; defines the action to be performed for I/O
292 * processing.
293 * @expires: timeout value in jiffies
294 *
295 * Start a S/390 channel program. When the interrupt arrives, the
296 * IRQ handler is called, either immediately, delayed (dev-end missing,
297 * or sense required) or never (no IRQ handler registered).
298 * This function notifies the device driver if the channel program has not
299 * completed during the time specified by @expires. If a timeout occurs, the
300 * channel program is terminated via xsch, hsch or csch, and the device's
301 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
302 * Returns:
303 * %0, if the operation was successful;
304 * -%EBUSY, if the device is busy, or status pending;
305 * -%EACCES, if no path specified in @lpm is operational;
306 * -%ENODEV, if the device is not operational.
307 * Context:
308 * Interrupts disabled, ccw device lock held
309 */
310int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
311 unsigned long intparm, __u8 lpm,
312 unsigned long flags, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700315 PAGE_DEFAULT_KEY, flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 expires);
317}
318
319
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200320/**
321 * ccw_device_halt() - halt I/O request processing
322 * @cdev: target ccw device
323 * @intparm: interruption parameter; value is only used if no I/O is
324 * outstanding, otherwise the intparm associated with the I/O request
325 * is returned
326 *
327 * ccw_device_halt() calls hsch on @cdev's subchannel.
328 * Returns:
329 * %0 on success,
330 * -%ENODEV on device not operational,
331 * -%EINVAL on invalid device state,
332 * -%EBUSY on device busy or interrupt pending.
333 * Context:
334 * Interrupts disabled, ccw device lock held
335 */
336int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct subchannel *sch;
339 int ret;
340
Sebastian Otte45efa92009-06-12 10:26:27 +0200341 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +0200343 sch = to_subchannel(cdev->dev.parent);
344 if (!sch->schib.pmcw.ena)
345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (cdev->private->state == DEV_STATE_NOT_OPER)
347 return -ENODEV;
348 if (cdev->private->state != DEV_STATE_ONLINE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 cdev->private->state != DEV_STATE_W4SENSE)
350 return -EINVAL;
Sebastian Ott823d4942009-06-16 10:30:20 +0200351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ret = cio_halt(sch);
353 if (ret == 0)
354 cdev->private->intparm = intparm;
355 return ret;
356}
357
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200358/**
359 * ccw_device_resume() - resume channel program execution
360 * @cdev: target ccw device
361 *
362 * ccw_device_resume() calls rsch on @cdev's subchannel.
363 * Returns:
364 * %0 on success,
365 * -%ENODEV on device not operational,
366 * -%EINVAL on invalid device state,
367 * -%EBUSY on device busy or interrupt pending.
368 * Context:
369 * Interrupts disabled, ccw device lock held
370 */
371int ccw_device_resume(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct subchannel *sch;
374
Sebastian Otte45efa92009-06-12 10:26:27 +0200375 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -ENODEV;
377 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +0200378 if (!sch->schib.pmcw.ena)
379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (cdev->private->state == DEV_STATE_NOT_OPER)
381 return -ENODEV;
382 if (cdev->private->state != DEV_STATE_ONLINE ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200383 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EINVAL;
385 return cio_resume(sch);
386}
387
388/*
389 * Pass interrupt to device driver.
390 */
391int
392ccw_device_call_handler(struct ccw_device *cdev)
393{
394 struct subchannel *sch;
395 unsigned int stctl;
396 int ending_status;
397
398 sch = to_subchannel(cdev->dev.parent);
399
400 /*
401 * we allow for the device action handler if .
402 * - we received ending status
403 * - the action handler requested to see all interrupts
404 * - we received an intermediate status
405 * - fast notification was requested (primary status)
406 * - unsolicited interrupts
407 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200408 stctl = scsw_stctl(&cdev->private->irb.scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
410 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
411 (stctl == SCSW_STCTL_STATUS_PEND);
412 if (!ending_status &&
413 !cdev->private->options.repall &&
414 !(stctl & SCSW_STCTL_INTER_STATUS) &&
415 !(cdev->private->options.fast &&
416 (stctl & SCSW_STCTL_PRIM_STATUS)))
417 return 0;
418
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200419 /* Clear pending timers for device driver initiated I/O. */
420 if (ending_status)
421 ccw_device_set_timeout(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * Now we are ready to call the device driver interrupt handler.
424 */
425 if (cdev->handler)
426 cdev->handler(cdev, cdev->private->intparm,
427 &cdev->private->irb);
428
429 /*
430 * Clear the old and now useless interrupt response block.
431 */
432 memset(&cdev->private->irb, 0, sizeof(struct irb));
433
434 return 1;
435}
436
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200437/**
438 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
439 * @cdev: ccw device to inspect
440 * @ct: command type to look for
441 *
442 * During SenseID, command information words (CIWs) describing special
443 * commands available to the device may have been stored in the extended
444 * sense data. This function searches for CIWs of a specified command
445 * type in the extended sense data.
446 * Returns:
447 * %NULL if no extended sense data has been stored or if no CIW of the
448 * specified command type could be found,
449 * else a pointer to the CIW of the specified command type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200451struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int ciw_cnt;
454
455 if (cdev->private->flags.esid == 0)
456 return NULL;
457 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
458 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
459 return cdev->private->senseid.ciw + ciw_cnt;
460 return NULL;
461}
462
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200463/**
464 * ccw_device_get_path_mask() - get currently available paths
465 * @cdev: ccw device to be queried
466 * Returns:
467 * %0 if no subchannel for the device is available,
468 * else the mask of currently available paths for the ccw device's subchannel.
469 */
470__u8 ccw_device_get_path_mask(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct subchannel *sch;
473
Sebastian Otte45efa92009-06-12 10:26:27 +0200474 if (!cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
Sebastian Otte45efa92009-06-12 10:26:27 +0200476
477 sch = to_subchannel(cdev->dev.parent);
478 return sch->lpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Try to break the lock on a boxed device.
483 */
484int
485ccw_device_stlck(struct ccw_device *cdev)
486{
487 void *buf, *buf2;
488 unsigned long flags;
489 struct subchannel *sch;
490 int ret;
491
492 if (!cdev)
493 return -ENODEV;
494
495 if (cdev->drv && !cdev->private->options.force)
496 return -EINVAL;
497
498 sch = to_subchannel(cdev->dev.parent);
499
500 CIO_TRACE_EVENT(2, "stl lock");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200501 CIO_TRACE_EVENT(2, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
504 if (!buf)
505 return -ENOMEM;
506 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
507 if (!buf2) {
508 kfree(buf);
509 return -ENOMEM;
510 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100511 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckedf22092008-04-30 13:38:39 +0200512 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (ret)
514 goto out_unlock;
515 /*
516 * Setup ccw. We chain an unconditional reserve and a release so we
517 * only break the lock.
518 */
519 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
520 cdev->private->iccws[0].cda = (__u32) __pa(buf);
521 cdev->private->iccws[0].count = 32;
522 cdev->private->iccws[0].flags = CCW_FLAG_CC;
523 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
524 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
525 cdev->private->iccws[1].count = 32;
526 cdev->private->iccws[1].flags = 0;
527 ret = cio_start(sch, cdev->private->iccws, 0);
528 if (ret) {
529 cio_disable_subchannel(sch); //FIXME: return code?
530 goto out_unlock;
531 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200532 cdev->private->irb.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100533 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200534 wait_event(cdev->private->wait_q,
535 cdev->private->irb.scsw.cmd.actl == 0);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100536 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 cio_disable_subchannel(sch); //FIXME: return code?
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200538 if ((cdev->private->irb.scsw.cmd.dstat !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200540 (cdev->private->irb.scsw.cmd.cstat != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ret = -EIO;
542 /* Clear irb. */
543 memset(&cdev->private->irb, 0, sizeof(struct irb));
544out_unlock:
Jesper Juhl17fd6822005-11-07 01:01:30 -0800545 kfree(buf);
546 kfree(buf2);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100547 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return ret;
549}
550
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200551void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct subchannel *sch;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200554 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 sch = to_subchannel(cdev->dev.parent);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200557 chp_id_init(&chpid);
558 chpid.id = sch->schib.pmcw.chpid[chp_no];
559 return chp_get_chp_desc(chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200562/**
563 * ccw_device_get_id - obtain a ccw device id
564 * @cdev: device to obtain the id for
565 * @dev_id: where to fill in the values
566 */
567void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
568{
569 *dev_id = cdev->private->dev_id;
570}
571EXPORT_SYMBOL(ccw_device_get_id);
572
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200573/**
574 * ccw_device_tm_start_key - perform start function
575 * @cdev: ccw device on which to perform the start function
576 * @tcw: transport-command word to be started
577 * @intparm: user defined parameter to be passed to the interrupt handler
578 * @lpm: mask of paths to use
579 * @key: storage key to use for storage access
580 *
581 * Start the tcw on the given ccw device. Return zero on success, non-zero
582 * otherwise.
583 */
584int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
585 unsigned long intparm, u8 lpm, u8 key)
586{
587 struct subchannel *sch;
588 int rc;
589
590 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +0200591 if (!sch->schib.pmcw.ena)
592 return -EINVAL;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200593 if (cdev->private->state != DEV_STATE_ONLINE)
594 return -EIO;
595 /* Adjust requested path mask to excluded varied off paths. */
596 if (lpm) {
597 lpm &= sch->opm;
598 if (lpm == 0)
599 return -EACCES;
600 }
601 rc = cio_tm_start_key(sch, tcw, lpm, key);
602 if (rc == 0)
603 cdev->private->intparm = intparm;
604 return rc;
605}
606EXPORT_SYMBOL(ccw_device_tm_start_key);
607
608/**
609 * ccw_device_tm_start_timeout_key - perform start function
610 * @cdev: ccw device on which to perform the start function
611 * @tcw: transport-command word to be started
612 * @intparm: user defined parameter to be passed to the interrupt handler
613 * @lpm: mask of paths to use
614 * @key: storage key to use for storage access
615 * @expires: time span in jiffies after which to abort request
616 *
617 * Start the tcw on the given ccw device. Return zero on success, non-zero
618 * otherwise.
619 */
620int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
621 unsigned long intparm, u8 lpm, u8 key,
622 int expires)
623{
624 int ret;
625
626 ccw_device_set_timeout(cdev, expires);
627 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
628 if (ret != 0)
629 ccw_device_set_timeout(cdev, 0);
630 return ret;
631}
632EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
633
634/**
635 * ccw_device_tm_start - perform start function
636 * @cdev: ccw device on which to perform the start function
637 * @tcw: transport-command word to be started
638 * @intparm: user defined parameter to be passed to the interrupt handler
639 * @lpm: mask of paths to use
640 *
641 * Start the tcw on the given ccw device. Return zero on success, non-zero
642 * otherwise.
643 */
644int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
645 unsigned long intparm, u8 lpm)
646{
647 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
648 PAGE_DEFAULT_KEY);
649}
650EXPORT_SYMBOL(ccw_device_tm_start);
651
652/**
653 * ccw_device_tm_start_timeout - perform start function
654 * @cdev: ccw device on which to perform the start function
655 * @tcw: transport-command word to be started
656 * @intparm: user defined parameter to be passed to the interrupt handler
657 * @lpm: mask of paths to use
658 * @expires: time span in jiffies after which to abort request
659 *
660 * Start the tcw on the given ccw device. Return zero on success, non-zero
661 * otherwise.
662 */
663int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
664 unsigned long intparm, u8 lpm, int expires)
665{
666 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
667 PAGE_DEFAULT_KEY, expires);
668}
669EXPORT_SYMBOL(ccw_device_tm_start_timeout);
670
671/**
672 * ccw_device_tm_intrg - perform interrogate function
673 * @cdev: ccw device on which to perform the interrogate function
674 *
675 * Perform an interrogate function on the given ccw device. Return zero on
676 * success, non-zero otherwise.
677 */
678int ccw_device_tm_intrg(struct ccw_device *cdev)
679{
680 struct subchannel *sch = to_subchannel(cdev->dev.parent);
681
Sebastian Ott823d4942009-06-16 10:30:20 +0200682 if (!sch->schib.pmcw.ena)
683 return -EINVAL;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200684 if (cdev->private->state != DEV_STATE_ONLINE)
685 return -EIO;
686 if (!scsw_is_tm(&sch->schib.scsw) ||
Peter Oberparleiter7a968f02009-03-26 15:24:18 +0100687 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200688 return -EINVAL;
689 return cio_tm_intrg(sch);
690}
691EXPORT_SYMBOL(ccw_device_tm_intrg);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693// FIXME: these have to go:
694
695int
696_ccw_device_get_subchannel_number(struct ccw_device *cdev)
697{
Cornelia Huck78964262006-10-11 15:31:38 +0200698 return cdev->private->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702MODULE_LICENSE("GPL");
Cornelia Huck4dd3cc52007-02-12 15:47:18 +0100703EXPORT_SYMBOL(ccw_device_set_options_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704EXPORT_SYMBOL(ccw_device_set_options);
Cornelia Huck4dd3cc52007-02-12 15:47:18 +0100705EXPORT_SYMBOL(ccw_device_clear_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706EXPORT_SYMBOL(ccw_device_clear);
707EXPORT_SYMBOL(ccw_device_halt);
708EXPORT_SYMBOL(ccw_device_resume);
709EXPORT_SYMBOL(ccw_device_start_timeout);
710EXPORT_SYMBOL(ccw_device_start);
711EXPORT_SYMBOL(ccw_device_start_timeout_key);
712EXPORT_SYMBOL(ccw_device_start_key);
713EXPORT_SYMBOL(ccw_device_get_ciw);
714EXPORT_SYMBOL(ccw_device_get_path_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);