blob: 5ab90ec4231884f712d14fd042c95b92944b4d3e [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;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +0100170 if (cdev->private->state == DEV_STATE_VERIFY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Remember to fake irb when finished. */
172 if (!cdev->private->flags.fake_irb) {
173 cdev->private->flags.fake_irb = 1;
174 cdev->private->intparm = intparm;
175 return 0;
176 } else
177 /* There's already a fake I/O around. */
178 return -EBUSY;
179 }
180 if (cdev->private->state != DEV_STATE_ONLINE ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200181 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
182 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 cdev->private->flags.doverify)
184 return -EBUSY;
185 ret = cio_set_options (sch, flags);
186 if (ret)
187 return ret;
Peter Oberparleitere0e32c82006-09-20 15:59:57 +0200188 /* Adjust requested path mask to excluded varied off paths. */
189 if (lpm) {
190 lpm &= sch->opm;
191 if (lpm == 0)
192 return -EACCES;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ret = cio_start_key (sch, cpa, lpm, key);
Cornelia Huckfe6173d2008-04-17 07:46:00 +0200195 switch (ret) {
196 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 cdev->private->intparm = intparm;
Cornelia Huckfe6173d2008-04-17 07:46:00 +0200198 break;
199 case -EACCES:
200 case -ENODEV:
201 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
202 break;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ret;
205}
206
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200207/**
208 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
209 * @cdev: target ccw device
210 * @cpa: logical start address of channel program
211 * @intparm: user specific interruption parameter; will be presented back to
212 * @cdev's interrupt handler. Allows a device driver to associate
213 * the interrupt with a particular I/O request.
214 * @lpm: defines the channel path to be used for a specific I/O request. A
215 * value of 0 will make cio use the opm.
216 * @key: storage key to be used for the I/O
217 * @flags: additional flags; defines the action to be performed for I/O
218 * processing.
219 * @expires: timeout value in jiffies
220 *
221 * Start a S/390 channel program. When the interrupt arrives, the
222 * IRQ handler is called, either immediately, delayed (dev-end missing,
223 * or sense required) or never (no IRQ handler registered).
224 * This function notifies the device driver if the channel program has not
225 * completed during the time specified by @expires. If a timeout occurs, the
226 * channel program is terminated via xsch, hsch or csch, and the device's
227 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
228 * Returns:
229 * %0, if the operation was successful;
230 * -%EBUSY, if the device is busy, or status pending;
231 * -%EACCES, if no path specified in @lpm is operational;
232 * -%ENODEV, if the device is not operational.
233 * Context:
234 * Interrupts disabled, ccw device lock held
235 */
236int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
237 unsigned long intparm, __u8 lpm, __u8 key,
238 unsigned long flags, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int ret;
241
242 if (!cdev)
243 return -ENODEV;
244 ccw_device_set_timeout(cdev, expires);
245 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
246 if (ret != 0)
247 ccw_device_set_timeout(cdev, 0);
248 return ret;
249}
250
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200251/**
252 * ccw_device_start() - start a s390 channel program
253 * @cdev: target ccw device
254 * @cpa: logical start address of channel program
255 * @intparm: user specific interruption parameter; will be presented back to
256 * @cdev's interrupt handler. Allows a device driver to associate
257 * the interrupt with a particular I/O request.
258 * @lpm: defines the channel path to be used for a specific I/O request. A
259 * value of 0 will make cio use the opm.
260 * @flags: additional flags; defines the action to be performed for I/O
261 * processing.
262 *
263 * Start a S/390 channel program. When the interrupt arrives, the
264 * IRQ handler is called, either immediately, delayed (dev-end missing,
265 * or sense required) or never (no IRQ handler registered).
266 * Returns:
267 * %0, if the operation was successful;
268 * -%EBUSY, if the device is busy, or status pending;
269 * -%EACCES, if no path specified in @lpm is operational;
270 * -%ENODEV, if the device is not operational.
271 * Context:
272 * Interrupts disabled, ccw device lock held
273 */
274int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
275 unsigned long intparm, __u8 lpm, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 return ccw_device_start_key(cdev, cpa, intparm, lpm,
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700278 PAGE_DEFAULT_KEY, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200281/**
282 * ccw_device_start_timeout() - start a s390 channel program with timeout
283 * @cdev: target ccw device
284 * @cpa: logical start address of channel program
285 * @intparm: user specific interruption parameter; will be presented back to
286 * @cdev's interrupt handler. Allows a device driver to associate
287 * the interrupt with a particular I/O request.
288 * @lpm: defines the channel path to be used for a specific I/O request. A
289 * value of 0 will make cio use the opm.
290 * @flags: additional flags; defines the action to be performed for I/O
291 * processing.
292 * @expires: timeout value in jiffies
293 *
294 * Start a S/390 channel program. When the interrupt arrives, the
295 * IRQ handler is called, either immediately, delayed (dev-end missing,
296 * or sense required) or never (no IRQ handler registered).
297 * This function notifies the device driver if the channel program has not
298 * completed during the time specified by @expires. If a timeout occurs, the
299 * channel program is terminated via xsch, hsch or csch, and the device's
300 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
301 * Returns:
302 * %0, if the operation was successful;
303 * -%EBUSY, if the device is busy, or status pending;
304 * -%EACCES, if no path specified in @lpm is operational;
305 * -%ENODEV, if the device is not operational.
306 * Context:
307 * Interrupts disabled, ccw device lock held
308 */
309int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
310 unsigned long intparm, __u8 lpm,
311 unsigned long flags, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700314 PAGE_DEFAULT_KEY, flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 expires);
316}
317
318
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200319/**
320 * ccw_device_halt() - halt I/O request processing
321 * @cdev: target ccw device
322 * @intparm: interruption parameter; value is only used if no I/O is
323 * outstanding, otherwise the intparm associated with the I/O request
324 * is returned
325 *
326 * ccw_device_halt() calls hsch on @cdev's subchannel.
327 * Returns:
328 * %0 on success,
329 * -%ENODEV on device not operational,
330 * -%EINVAL on invalid device state,
331 * -%EBUSY on device busy or interrupt pending.
332 * Context:
333 * Interrupts disabled, ccw device lock held
334 */
335int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct subchannel *sch;
338 int ret;
339
Sebastian Otte45efa92009-06-12 10:26:27 +0200340 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +0200342 sch = to_subchannel(cdev->dev.parent);
343 if (!sch->schib.pmcw.ena)
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (cdev->private->state == DEV_STATE_NOT_OPER)
346 return -ENODEV;
347 if (cdev->private->state != DEV_STATE_ONLINE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 cdev->private->state != DEV_STATE_W4SENSE)
349 return -EINVAL;
Sebastian Ott823d4942009-06-16 10:30:20 +0200350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ret = cio_halt(sch);
352 if (ret == 0)
353 cdev->private->intparm = intparm;
354 return ret;
355}
356
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200357/**
358 * ccw_device_resume() - resume channel program execution
359 * @cdev: target ccw device
360 *
361 * ccw_device_resume() calls rsch on @cdev's subchannel.
362 * Returns:
363 * %0 on success,
364 * -%ENODEV on device not operational,
365 * -%EINVAL on invalid device state,
366 * -%EBUSY on device busy or interrupt pending.
367 * Context:
368 * Interrupts disabled, ccw device lock held
369 */
370int ccw_device_resume(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct subchannel *sch;
373
Sebastian Otte45efa92009-06-12 10:26:27 +0200374 if (!cdev || !cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -ENODEV;
376 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +0200377 if (!sch->schib.pmcw.ena)
378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (cdev->private->state == DEV_STATE_NOT_OPER)
380 return -ENODEV;
381 if (cdev->private->state != DEV_STATE_ONLINE ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200382 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -EINVAL;
384 return cio_resume(sch);
385}
386
387/*
388 * Pass interrupt to device driver.
389 */
390int
391ccw_device_call_handler(struct ccw_device *cdev)
392{
393 struct subchannel *sch;
394 unsigned int stctl;
395 int ending_status;
396
397 sch = to_subchannel(cdev->dev.parent);
398
399 /*
400 * we allow for the device action handler if .
401 * - we received ending status
402 * - the action handler requested to see all interrupts
403 * - we received an intermediate status
404 * - fast notification was requested (primary status)
405 * - unsolicited interrupts
406 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200407 stctl = scsw_stctl(&cdev->private->irb.scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
409 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
410 (stctl == SCSW_STCTL_STATUS_PEND);
411 if (!ending_status &&
412 !cdev->private->options.repall &&
413 !(stctl & SCSW_STCTL_INTER_STATUS) &&
414 !(cdev->private->options.fast &&
415 (stctl & SCSW_STCTL_PRIM_STATUS)))
416 return 0;
417
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200418 /* Clear pending timers for device driver initiated I/O. */
419 if (ending_status)
420 ccw_device_set_timeout(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
422 * Now we are ready to call the device driver interrupt handler.
423 */
424 if (cdev->handler)
425 cdev->handler(cdev, cdev->private->intparm,
426 &cdev->private->irb);
427
428 /*
429 * Clear the old and now useless interrupt response block.
430 */
431 memset(&cdev->private->irb, 0, sizeof(struct irb));
432
433 return 1;
434}
435
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200436/**
437 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
438 * @cdev: ccw device to inspect
439 * @ct: command type to look for
440 *
441 * During SenseID, command information words (CIWs) describing special
442 * commands available to the device may have been stored in the extended
443 * sense data. This function searches for CIWs of a specified command
444 * type in the extended sense data.
445 * Returns:
446 * %NULL if no extended sense data has been stored or if no CIW of the
447 * specified command type could be found,
448 * else a pointer to the CIW of the specified command type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200450struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int ciw_cnt;
453
454 if (cdev->private->flags.esid == 0)
455 return NULL;
456 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
457 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
458 return cdev->private->senseid.ciw + ciw_cnt;
459 return NULL;
460}
461
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200462/**
463 * ccw_device_get_path_mask() - get currently available paths
464 * @cdev: ccw device to be queried
465 * Returns:
466 * %0 if no subchannel for the device is available,
467 * else the mask of currently available paths for the ccw device's subchannel.
468 */
469__u8 ccw_device_get_path_mask(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct subchannel *sch;
472
Sebastian Otte45efa92009-06-12 10:26:27 +0200473 if (!cdev->dev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
Sebastian Otte45efa92009-06-12 10:26:27 +0200475
476 sch = to_subchannel(cdev->dev.parent);
477 return sch->lpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Try to break the lock on a boxed device.
482 */
483int
484ccw_device_stlck(struct ccw_device *cdev)
485{
486 void *buf, *buf2;
487 unsigned long flags;
488 struct subchannel *sch;
489 int ret;
490
491 if (!cdev)
492 return -ENODEV;
493
494 if (cdev->drv && !cdev->private->options.force)
495 return -EINVAL;
496
497 sch = to_subchannel(cdev->dev.parent);
498
499 CIO_TRACE_EVENT(2, "stl lock");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200500 CIO_TRACE_EVENT(2, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
503 if (!buf)
504 return -ENOMEM;
505 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
506 if (!buf2) {
507 kfree(buf);
508 return -ENOMEM;
509 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100510 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckedf22092008-04-30 13:38:39 +0200511 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (ret)
513 goto out_unlock;
514 /*
515 * Setup ccw. We chain an unconditional reserve and a release so we
516 * only break the lock.
517 */
518 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
519 cdev->private->iccws[0].cda = (__u32) __pa(buf);
520 cdev->private->iccws[0].count = 32;
521 cdev->private->iccws[0].flags = CCW_FLAG_CC;
522 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
523 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
524 cdev->private->iccws[1].count = 32;
525 cdev->private->iccws[1].flags = 0;
526 ret = cio_start(sch, cdev->private->iccws, 0);
527 if (ret) {
528 cio_disable_subchannel(sch); //FIXME: return code?
529 goto out_unlock;
530 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200531 cdev->private->irb.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100532 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200533 wait_event(cdev->private->wait_q,
534 cdev->private->irb.scsw.cmd.actl == 0);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100535 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cio_disable_subchannel(sch); //FIXME: return code?
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200537 if ((cdev->private->irb.scsw.cmd.dstat !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200539 (cdev->private->irb.scsw.cmd.cstat != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ret = -EIO;
541 /* Clear irb. */
542 memset(&cdev->private->irb, 0, sizeof(struct irb));
543out_unlock:
Jesper Juhl17fd6822005-11-07 01:01:30 -0800544 kfree(buf);
545 kfree(buf2);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100546 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return ret;
548}
549
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200550void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct subchannel *sch;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200553 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 sch = to_subchannel(cdev->dev.parent);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200556 chp_id_init(&chpid);
557 chpid.id = sch->schib.pmcw.chpid[chp_no];
558 return chp_get_chp_desc(chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200561/**
562 * ccw_device_get_id - obtain a ccw device id
563 * @cdev: device to obtain the id for
564 * @dev_id: where to fill in the values
565 */
566void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
567{
568 *dev_id = cdev->private->dev_id;
569}
570EXPORT_SYMBOL(ccw_device_get_id);
571
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200572/**
573 * ccw_device_tm_start_key - perform start function
574 * @cdev: ccw device on which to perform the start function
575 * @tcw: transport-command word to be started
576 * @intparm: user defined parameter to be passed to the interrupt handler
577 * @lpm: mask of paths to use
578 * @key: storage key to use for storage access
579 *
580 * Start the tcw on the given ccw device. Return zero on success, non-zero
581 * otherwise.
582 */
583int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
584 unsigned long intparm, u8 lpm, u8 key)
585{
586 struct subchannel *sch;
587 int rc;
588
589 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +0200590 if (!sch->schib.pmcw.ena)
591 return -EINVAL;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200592 if (cdev->private->state != DEV_STATE_ONLINE)
593 return -EIO;
594 /* Adjust requested path mask to excluded varied off paths. */
595 if (lpm) {
596 lpm &= sch->opm;
597 if (lpm == 0)
598 return -EACCES;
599 }
600 rc = cio_tm_start_key(sch, tcw, lpm, key);
601 if (rc == 0)
602 cdev->private->intparm = intparm;
603 return rc;
604}
605EXPORT_SYMBOL(ccw_device_tm_start_key);
606
607/**
608 * ccw_device_tm_start_timeout_key - perform start function
609 * @cdev: ccw device on which to perform the start function
610 * @tcw: transport-command word to be started
611 * @intparm: user defined parameter to be passed to the interrupt handler
612 * @lpm: mask of paths to use
613 * @key: storage key to use for storage access
614 * @expires: time span in jiffies after which to abort request
615 *
616 * Start the tcw on the given ccw device. Return zero on success, non-zero
617 * otherwise.
618 */
619int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
620 unsigned long intparm, u8 lpm, u8 key,
621 int expires)
622{
623 int ret;
624
625 ccw_device_set_timeout(cdev, expires);
626 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
627 if (ret != 0)
628 ccw_device_set_timeout(cdev, 0);
629 return ret;
630}
631EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
632
633/**
634 * ccw_device_tm_start - perform start function
635 * @cdev: ccw device on which to perform the start function
636 * @tcw: transport-command word to be started
637 * @intparm: user defined parameter to be passed to the interrupt handler
638 * @lpm: mask of paths to use
639 *
640 * Start the tcw on the given ccw device. Return zero on success, non-zero
641 * otherwise.
642 */
643int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
644 unsigned long intparm, u8 lpm)
645{
646 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
647 PAGE_DEFAULT_KEY);
648}
649EXPORT_SYMBOL(ccw_device_tm_start);
650
651/**
652 * ccw_device_tm_start_timeout - perform start function
653 * @cdev: ccw device on which to perform the start function
654 * @tcw: transport-command word to be started
655 * @intparm: user defined parameter to be passed to the interrupt handler
656 * @lpm: mask of paths to use
657 * @expires: time span in jiffies after which to abort request
658 *
659 * Start the tcw on the given ccw device. Return zero on success, non-zero
660 * otherwise.
661 */
662int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
663 unsigned long intparm, u8 lpm, int expires)
664{
665 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
666 PAGE_DEFAULT_KEY, expires);
667}
668EXPORT_SYMBOL(ccw_device_tm_start_timeout);
669
670/**
671 * ccw_device_tm_intrg - perform interrogate function
672 * @cdev: ccw device on which to perform the interrogate function
673 *
674 * Perform an interrogate function on the given ccw device. Return zero on
675 * success, non-zero otherwise.
676 */
677int ccw_device_tm_intrg(struct ccw_device *cdev)
678{
679 struct subchannel *sch = to_subchannel(cdev->dev.parent);
680
Sebastian Ott823d4942009-06-16 10:30:20 +0200681 if (!sch->schib.pmcw.ena)
682 return -EINVAL;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200683 if (cdev->private->state != DEV_STATE_ONLINE)
684 return -EIO;
685 if (!scsw_is_tm(&sch->schib.scsw) ||
Peter Oberparleiter7a968f02009-03-26 15:24:18 +0100686 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200687 return -EINVAL;
688 return cio_tm_intrg(sch);
689}
690EXPORT_SYMBOL(ccw_device_tm_intrg);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692// FIXME: these have to go:
693
694int
695_ccw_device_get_subchannel_number(struct ccw_device *cdev)
696{
Cornelia Huck78964262006-10-11 15:31:38 +0200697 return cdev->private->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701MODULE_LICENSE("GPL");
Cornelia Huck4dd3cc52007-02-12 15:47:18 +0100702EXPORT_SYMBOL(ccw_device_set_options_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703EXPORT_SYMBOL(ccw_device_set_options);
Cornelia Huck4dd3cc52007-02-12 15:47:18 +0100704EXPORT_SYMBOL(ccw_device_clear_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705EXPORT_SYMBOL(ccw_device_clear);
706EXPORT_SYMBOL(ccw_device_halt);
707EXPORT_SYMBOL(ccw_device_resume);
708EXPORT_SYMBOL(ccw_device_start_timeout);
709EXPORT_SYMBOL(ccw_device_start);
710EXPORT_SYMBOL(ccw_device_start_timeout_key);
711EXPORT_SYMBOL(ccw_device_start_key);
712EXPORT_SYMBOL(ccw_device_get_ciw);
713EXPORT_SYMBOL(ccw_device_get_path_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);