1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3 * Copyright IBM Corp. 2002, 2009
4 *
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Cornelia Huck (cornelia.huck@de.ibm.com)
7 */
8 #include <linux/export.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/completion.h>
16
17 #include <asm/ccwdev.h>
18 #include <asm/idals.h>
19 #include <asm/chpid.h>
20 #include <asm/fcx.h>
21
22 #include "cio.h"
23 #include "cio_debug.h"
24 #include "css.h"
25 #include "chsc.h"
26 #include "device.h"
27 #include "chp.h"
28
29 /**
30 * ccw_device_set_options_mask() - set some options and unset the rest
31 * @cdev: device for which the options are to be set
32 * @flags: options to be set
33 *
34 * All flags specified in @flags are set, all flags not specified in @flags
35 * are cleared.
36 * Returns:
37 * %0 on success, -%EINVAL on an invalid flag combination.
38 */
ccw_device_set_options_mask(struct ccw_device * cdev,unsigned long flags)39 int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
40 {
41 /*
42 * The flag usage is mutal exclusive ...
43 */
44 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
45 (flags & CCWDEV_REPORT_ALL))
46 return -EINVAL;
47 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
48 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
49 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
50 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
51 cdev->private->options.mpath = (flags & CCWDEV_DO_MULTIPATH) != 0;
52 return 0;
53 }
54
55 /**
56 * ccw_device_set_options() - set some options
57 * @cdev: device for which the options are to be set
58 * @flags: options to be set
59 *
60 * All flags specified in @flags are set, the remainder is left untouched.
61 * Returns:
62 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
63 */
ccw_device_set_options(struct ccw_device * cdev,unsigned long flags)64 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
65 {
66 /*
67 * The flag usage is mutal exclusive ...
68 */
69 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
70 (flags & CCWDEV_REPORT_ALL)) ||
71 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
72 cdev->private->options.repall) ||
73 ((flags & CCWDEV_REPORT_ALL) &&
74 cdev->private->options.fast))
75 return -EINVAL;
76 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
77 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
78 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
79 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
80 cdev->private->options.mpath |= (flags & CCWDEV_DO_MULTIPATH) != 0;
81 return 0;
82 }
83
84 /**
85 * ccw_device_clear_options() - clear some options
86 * @cdev: device for which the options are to be cleared
87 * @flags: options to be cleared
88 *
89 * All flags specified in @flags are cleared, the remainder is left untouched.
90 */
ccw_device_clear_options(struct ccw_device * cdev,unsigned long flags)91 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
92 {
93 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
94 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
95 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
96 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
97 cdev->private->options.mpath &= (flags & CCWDEV_DO_MULTIPATH) == 0;
98 }
99
100 /**
101 * ccw_device_is_pathgroup() - determine if paths to this device are grouped
102 * @cdev: ccw device
103 *
104 * Return non-zero if there is a path group, zero otherwise.
105 */
ccw_device_is_pathgroup(struct ccw_device * cdev)106 int ccw_device_is_pathgroup(struct ccw_device *cdev)
107 {
108 return cdev->private->flags.pgroup;
109 }
110 EXPORT_SYMBOL(ccw_device_is_pathgroup);
111
112 /**
113 * ccw_device_is_multipath() - determine if device is operating in multipath mode
114 * @cdev: ccw device
115 *
116 * Return non-zero if device is operating in multipath mode, zero otherwise.
117 */
ccw_device_is_multipath(struct ccw_device * cdev)118 int ccw_device_is_multipath(struct ccw_device *cdev)
119 {
120 return cdev->private->flags.mpath;
121 }
122 EXPORT_SYMBOL(ccw_device_is_multipath);
123
124 /**
125 * ccw_device_clear() - terminate I/O request processing
126 * @cdev: target ccw device
127 * @intparm: interruption parameter to be returned upon conclusion of csch
128 *
129 * ccw_device_clear() calls csch on @cdev's subchannel.
130 * Returns:
131 * %0 on success,
132 * -%ENODEV on device not operational,
133 * -%EINVAL on invalid device state.
134 * Context:
135 * Interrupts disabled, ccw device lock held
136 */
ccw_device_clear(struct ccw_device * cdev,unsigned long intparm)137 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
138 {
139 struct subchannel *sch;
140 int ret;
141
142 if (!cdev || !cdev->dev.parent)
143 return -ENODEV;
144 sch = to_subchannel(cdev->dev.parent);
145 if (!sch->schib.pmcw.dnv)
146 return -ENODEV;
147 if (!sch->schib.pmcw.ena)
148 return -EINVAL;
149 if (cdev->private->state == DEV_STATE_NOT_OPER)
150 return -ENODEV;
151 if (cdev->private->state != DEV_STATE_ONLINE &&
152 cdev->private->state != DEV_STATE_W4SENSE)
153 return -EINVAL;
154
155 ret = cio_clear(sch);
156 if (ret == 0)
157 cdev->private->intparm = intparm;
158 return ret;
159 }
160
161 /**
162 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
163 * @cdev: target ccw device
164 * @cpa: logical start address of channel program
165 * @intparm: user specific interruption parameter; will be presented back to
166 * @cdev's interrupt handler. Allows a device driver to associate
167 * the interrupt with a particular I/O request.
168 * @lpm: defines the channel path to be used for a specific I/O request. A
169 * value of 0 will make cio use the opm.
170 * @key: storage key to be used for the I/O
171 * @flags: additional flags; defines the action to be performed for I/O
172 * processing.
173 * @expires: timeout value in jiffies
174 *
175 * Start a S/390 channel program. When the interrupt arrives, the
176 * IRQ handler is called, either immediately, delayed (dev-end missing,
177 * or sense required) or never (no IRQ handler registered).
178 * This function notifies the device driver if the channel program has not
179 * completed during the time specified by @expires. If a timeout occurs, the
180 * channel program is terminated via xsch, hsch or csch, and the device's
181 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
182 * The interruption handler will echo back the @intparm specified here, unless
183 * another interruption parameter is specified by a subsequent invocation of
184 * ccw_device_halt() or ccw_device_clear().
185 * Returns:
186 * %0, if the operation was successful;
187 * -%EBUSY, if the device is busy, or status pending;
188 * -%EACCES, if no path specified in @lpm is operational;
189 * -%ENODEV, if the device is not operational.
190 * Context:
191 * Interrupts disabled, ccw device lock held
192 */
ccw_device_start_timeout_key(struct ccw_device * cdev,struct ccw1 * cpa,unsigned long intparm,__u8 lpm,__u8 key,unsigned long flags,int expires)193 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
194 unsigned long intparm, __u8 lpm, __u8 key,
195 unsigned long flags, int expires)
196 {
197 struct subchannel *sch;
198 int ret;
199
200 if (!cdev || !cdev->dev.parent)
201 return -ENODEV;
202 sch = to_subchannel(cdev->dev.parent);
203 if (!sch->schib.pmcw.dnv)
204 return -ENODEV;
205 if (!sch->schib.pmcw.ena)
206 return -EINVAL;
207 if (cdev->private->state == DEV_STATE_NOT_OPER)
208 return -ENODEV;
209 if (cdev->private->state == DEV_STATE_VERIFY ||
210 cdev->private->flags.doverify) {
211 /* Remember to fake irb when finished. */
212 if (!cdev->private->flags.fake_irb) {
213 cdev->private->flags.fake_irb = FAKE_CMD_IRB;
214 cdev->private->intparm = intparm;
215 CIO_MSG_EVENT(2, "fakeirb: queue device 0.%x.%04x intparm %lx type=%d\n",
216 cdev->private->dev_id.ssid,
217 cdev->private->dev_id.devno, intparm,
218 cdev->private->flags.fake_irb);
219 return 0;
220 } else
221 /* There's already a fake I/O around. */
222 return -EBUSY;
223 }
224 if (cdev->private->state != DEV_STATE_ONLINE ||
225 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
226 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)))
227 return -EBUSY;
228 ret = cio_set_options (sch, flags);
229 if (ret)
230 return ret;
231 /* Adjust requested path mask to exclude unusable paths. */
232 if (lpm) {
233 lpm &= sch->lpm;
234 if (lpm == 0)
235 return -EACCES;
236 }
237 ret = cio_start_key (sch, cpa, lpm, key);
238 switch (ret) {
239 case 0:
240 cdev->private->intparm = intparm;
241 if (expires)
242 ccw_device_set_timeout(cdev, expires);
243 break;
244 case -EACCES:
245 case -ENODEV:
246 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
247 break;
248 }
249 return ret;
250 }
251
252 /**
253 * ccw_device_start_key() - start a s390 channel program with key
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 * @key: storage key to be used for the I/O
262 * @flags: additional flags; defines the action to be performed for I/O
263 * processing.
264 *
265 * Start a S/390 channel program. When the interrupt arrives, the
266 * IRQ handler is called, either immediately, delayed (dev-end missing,
267 * or sense required) or never (no IRQ handler registered).
268 * The interruption handler will echo back the @intparm specified here, unless
269 * another interruption parameter is specified by a subsequent invocation of
270 * ccw_device_halt() or ccw_device_clear().
271 * Returns:
272 * %0, if the operation was successful;
273 * -%EBUSY, if the device is busy, or status pending;
274 * -%EACCES, if no path specified in @lpm is operational;
275 * -%ENODEV, if the device is not operational.
276 * Context:
277 * Interrupts disabled, ccw device lock held
278 */
ccw_device_start_key(struct ccw_device * cdev,struct ccw1 * cpa,unsigned long intparm,__u8 lpm,__u8 key,unsigned long flags)279 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
280 unsigned long intparm, __u8 lpm, __u8 key,
281 unsigned long flags)
282 {
283 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm, key,
284 flags, 0);
285 }
286
287 /**
288 * ccw_device_start() - start a s390 channel program
289 * @cdev: target ccw device
290 * @cpa: logical start address of channel program
291 * @intparm: user specific interruption parameter; will be presented back to
292 * @cdev's interrupt handler. Allows a device driver to associate
293 * the interrupt with a particular I/O request.
294 * @lpm: defines the channel path to be used for a specific I/O request. A
295 * value of 0 will make cio use the opm.
296 * @flags: additional flags; defines the action to be performed for I/O
297 * processing.
298 *
299 * Start a S/390 channel program. When the interrupt arrives, the
300 * IRQ handler is called, either immediately, delayed (dev-end missing,
301 * or sense required) or never (no IRQ handler registered).
302 * The interruption handler will echo back the @intparm specified here, unless
303 * another interruption parameter is specified by a subsequent invocation of
304 * ccw_device_halt() or ccw_device_clear().
305 * Returns:
306 * %0, if the operation was successful;
307 * -%EBUSY, if the device is busy, or status pending;
308 * -%EACCES, if no path specified in @lpm is operational;
309 * -%ENODEV, if the device is not operational.
310 * Context:
311 * Interrupts disabled, ccw device lock held
312 */
ccw_device_start(struct ccw_device * cdev,struct ccw1 * cpa,unsigned long intparm,__u8 lpm,unsigned long flags)313 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
314 unsigned long intparm, __u8 lpm, unsigned long flags)
315 {
316 return ccw_device_start_key(cdev, cpa, intparm, lpm,
317 PAGE_DEFAULT_KEY, flags);
318 }
319
320 /**
321 * ccw_device_start_timeout() - start a s390 channel program with timeout
322 * @cdev: target ccw device
323 * @cpa: logical start address of channel program
324 * @intparm: user specific interruption parameter; will be presented back to
325 * @cdev's interrupt handler. Allows a device driver to associate
326 * the interrupt with a particular I/O request.
327 * @lpm: defines the channel path to be used for a specific I/O request. A
328 * value of 0 will make cio use the opm.
329 * @flags: additional flags; defines the action to be performed for I/O
330 * processing.
331 * @expires: timeout value in jiffies
332 *
333 * Start a S/390 channel program. When the interrupt arrives, the
334 * IRQ handler is called, either immediately, delayed (dev-end missing,
335 * or sense required) or never (no IRQ handler registered).
336 * This function notifies the device driver if the channel program has not
337 * completed during the time specified by @expires. If a timeout occurs, the
338 * channel program is terminated via xsch, hsch or csch, and the device's
339 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
340 * The interruption handler will echo back the @intparm specified here, unless
341 * another interruption parameter is specified by a subsequent invocation of
342 * ccw_device_halt() or ccw_device_clear().
343 * Returns:
344 * %0, if the operation was successful;
345 * -%EBUSY, if the device is busy, or status pending;
346 * -%EACCES, if no path specified in @lpm is operational;
347 * -%ENODEV, if the device is not operational.
348 * Context:
349 * Interrupts disabled, ccw device lock held
350 */
ccw_device_start_timeout(struct ccw_device * cdev,struct ccw1 * cpa,unsigned long intparm,__u8 lpm,unsigned long flags,int expires)351 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
352 unsigned long intparm, __u8 lpm,
353 unsigned long flags, int expires)
354 {
355 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
356 PAGE_DEFAULT_KEY, flags,
357 expires);
358 }
359
360
361 /**
362 * ccw_device_halt() - halt I/O request processing
363 * @cdev: target ccw device
364 * @intparm: interruption parameter to be returned upon conclusion of hsch
365 *
366 * ccw_device_halt() calls hsch on @cdev's subchannel.
367 * The interruption handler will echo back the @intparm specified here, unless
368 * another interruption parameter is specified by a subsequent invocation of
369 * ccw_device_clear().
370 * Returns:
371 * %0 on success,
372 * -%ENODEV on device not operational,
373 * -%EINVAL on invalid device state,
374 * -%EBUSY on device busy or interrupt pending.
375 * Context:
376 * Interrupts disabled, ccw device lock held
377 */
ccw_device_halt(struct ccw_device * cdev,unsigned long intparm)378 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
379 {
380 struct subchannel *sch;
381 int ret;
382
383 if (!cdev || !cdev->dev.parent)
384 return -ENODEV;
385 sch = to_subchannel(cdev->dev.parent);
386 if (!sch->schib.pmcw.dnv)
387 return -ENODEV;
388 if (!sch->schib.pmcw.ena)
389 return -EINVAL;
390 if (cdev->private->state == DEV_STATE_NOT_OPER)
391 return -ENODEV;
392 if (cdev->private->state != DEV_STATE_ONLINE &&
393 cdev->private->state != DEV_STATE_W4SENSE)
394 return -EINVAL;
395
396 ret = cio_halt(sch);
397 if (ret == 0)
398 cdev->private->intparm = intparm;
399 return ret;
400 }
401
402 /**
403 * ccw_device_resume() - resume channel program execution
404 * @cdev: target ccw device
405 *
406 * ccw_device_resume() calls rsch on @cdev's subchannel.
407 * Returns:
408 * %0 on success,
409 * -%ENODEV on device not operational,
410 * -%EINVAL on invalid device state,
411 * -%EBUSY on device busy or interrupt pending.
412 * Context:
413 * Interrupts disabled, ccw device lock held
414 */
ccw_device_resume(struct ccw_device * cdev)415 int ccw_device_resume(struct ccw_device *cdev)
416 {
417 struct subchannel *sch;
418
419 if (!cdev || !cdev->dev.parent)
420 return -ENODEV;
421 sch = to_subchannel(cdev->dev.parent);
422 if (!sch->schib.pmcw.dnv)
423 return -ENODEV;
424 if (!sch->schib.pmcw.ena)
425 return -EINVAL;
426 if (cdev->private->state == DEV_STATE_NOT_OPER)
427 return -ENODEV;
428 if (cdev->private->state != DEV_STATE_ONLINE ||
429 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
430 return -EINVAL;
431 return cio_resume(sch);
432 }
433
434 /**
435 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
436 * @cdev: ccw device to inspect
437 * @ct: command type to look for
438 *
439 * During SenseID, command information words (CIWs) describing special
440 * commands available to the device may have been stored in the extended
441 * sense data. This function searches for CIWs of a specified command
442 * type in the extended sense data.
443 * Returns:
444 * %NULL if no extended sense data has been stored or if no CIW of the
445 * specified command type could be found,
446 * else a pointer to the CIW of the specified command type.
447 */
ccw_device_get_ciw(struct ccw_device * cdev,__u32 ct)448 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
449 {
450 int ciw_cnt;
451
452 if (cdev->private->flags.esid == 0)
453 return NULL;
454 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
455 if (cdev->private->dma_area->senseid.ciw[ciw_cnt].ct == ct)
456 return &cdev->private->dma_area->senseid.ciw[ciw_cnt];
457 return NULL;
458 }
459
460 /**
461 * ccw_device_get_path_mask() - get currently available paths
462 * @cdev: ccw device to be queried
463 * Returns:
464 * %0 if no subchannel for the device is available,
465 * else the mask of currently available paths for the ccw device's subchannel.
466 */
ccw_device_get_path_mask(struct ccw_device * cdev)467 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
468 {
469 struct subchannel *sch;
470
471 if (!cdev->dev.parent)
472 return 0;
473
474 sch = to_subchannel(cdev->dev.parent);
475 return sch->lpm;
476 }
477
478 /**
479 * ccw_device_get_chp_desc() - return newly allocated channel-path descriptor
480 * @cdev: device to obtain the descriptor for
481 * @chp_idx: index of the channel path
482 *
483 * On success return a newly allocated copy of the channel-path description
484 * data associated with the given channel path. Return %NULL on error.
485 */
ccw_device_get_chp_desc(struct ccw_device * cdev,int chp_idx)486 struct channel_path_desc_fmt0 *ccw_device_get_chp_desc(struct ccw_device *cdev,
487 int chp_idx)
488 {
489 struct subchannel *sch;
490 struct chp_id chpid;
491
492 sch = to_subchannel(cdev->dev.parent);
493 if (!sch->schib.pmcw.dnv)
494 return NULL;
495 chp_id_init(&chpid);
496 chpid.id = sch->schib.pmcw.chpid[chp_idx];
497 return chp_get_chp_desc(chpid);
498 }
499
500 /**
501 * ccw_device_get_util_str() - return newly allocated utility strings
502 * @cdev: device to obtain the utility strings for
503 * @chp_idx: index of the channel path
504 *
505 * On success return a newly allocated copy of the utility strings
506 * associated with the given channel path. Return %NULL on error.
507 */
ccw_device_get_util_str(struct ccw_device * cdev,int chp_idx)508 u8 *ccw_device_get_util_str(struct ccw_device *cdev, int chp_idx)
509 {
510 struct subchannel *sch = to_subchannel(cdev->dev.parent);
511 struct channel_path *chp;
512 struct chp_id chpid;
513 u8 *util_str;
514
515 if (!sch->schib.pmcw.dnv)
516 return NULL;
517 chp_id_init(&chpid);
518 chpid.id = sch->schib.pmcw.chpid[chp_idx];
519 chp = chpid_to_chp(chpid);
520 if (!chp)
521 return NULL;
522
523 util_str = kmalloc(sizeof(chp->desc_fmt3.util_str), GFP_KERNEL);
524 if (!util_str)
525 return NULL;
526
527 mutex_lock(&chp->lock);
528 memcpy(util_str, chp->desc_fmt3.util_str, sizeof(chp->desc_fmt3.util_str));
529 mutex_unlock(&chp->lock);
530
531 return util_str;
532 }
533
534 /**
535 * ccw_device_get_id() - obtain a ccw device id
536 * @cdev: device to obtain the id for
537 * @dev_id: where to fill in the values
538 */
ccw_device_get_id(struct ccw_device * cdev,struct ccw_dev_id * dev_id)539 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
540 {
541 *dev_id = cdev->private->dev_id;
542 }
543 EXPORT_SYMBOL(ccw_device_get_id);
544
545 /**
546 * ccw_device_tm_start_timeout_key() - perform start function
547 * @cdev: ccw device on which to perform the start function
548 * @tcw: transport-command word to be started
549 * @intparm: user defined parameter to be passed to the interrupt handler
550 * @lpm: mask of paths to use
551 * @key: storage key to use for storage access
552 * @expires: time span in jiffies after which to abort request
553 *
554 * Start the tcw on the given ccw device. Return zero on success, non-zero
555 * otherwise.
556 */
ccw_device_tm_start_timeout_key(struct ccw_device * cdev,struct tcw * tcw,unsigned long intparm,u8 lpm,u8 key,int expires)557 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
558 unsigned long intparm, u8 lpm, u8 key,
559 int expires)
560 {
561 struct subchannel *sch;
562 int rc;
563
564 sch = to_subchannel(cdev->dev.parent);
565 if (!sch->schib.pmcw.dnv)
566 return -ENODEV;
567 if (!sch->schib.pmcw.ena)
568 return -EINVAL;
569 if (cdev->private->state == DEV_STATE_VERIFY) {
570 /* Remember to fake irb when finished. */
571 if (!cdev->private->flags.fake_irb) {
572 cdev->private->flags.fake_irb = FAKE_TM_IRB;
573 cdev->private->intparm = intparm;
574 CIO_MSG_EVENT(2, "fakeirb: queue device 0.%x.%04x intparm %lx type=%d\n",
575 cdev->private->dev_id.ssid,
576 cdev->private->dev_id.devno, intparm,
577 cdev->private->flags.fake_irb);
578 return 0;
579 } else
580 /* There's already a fake I/O around. */
581 return -EBUSY;
582 }
583 if (cdev->private->state != DEV_STATE_ONLINE)
584 return -EIO;
585 /* Adjust requested path mask to exclude unusable paths. */
586 if (lpm) {
587 lpm &= sch->lpm;
588 if (lpm == 0)
589 return -EACCES;
590 }
591 rc = cio_tm_start_key(sch, tcw, lpm, key);
592 if (rc == 0) {
593 cdev->private->intparm = intparm;
594 if (expires)
595 ccw_device_set_timeout(cdev, expires);
596 }
597 return rc;
598 }
599 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
600
601 /**
602 * ccw_device_tm_start_key() - perform start function
603 * @cdev: ccw device on which to perform the start function
604 * @tcw: transport-command word to be started
605 * @intparm: user defined parameter to be passed to the interrupt handler
606 * @lpm: mask of paths to use
607 * @key: storage key to use for storage access
608 *
609 * Start the tcw on the given ccw device. Return zero on success, non-zero
610 * otherwise.
611 */
ccw_device_tm_start_key(struct ccw_device * cdev,struct tcw * tcw,unsigned long intparm,u8 lpm,u8 key)612 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
613 unsigned long intparm, u8 lpm, u8 key)
614 {
615 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm, key, 0);
616 }
617 EXPORT_SYMBOL(ccw_device_tm_start_key);
618
619 /**
620 * ccw_device_tm_start() - perform start function
621 * @cdev: ccw device on which to perform the start function
622 * @tcw: transport-command word to be started
623 * @intparm: user defined parameter to be passed to the interrupt handler
624 * @lpm: mask of paths to use
625 *
626 * Start the tcw on the given ccw device. Return zero on success, non-zero
627 * otherwise.
628 */
ccw_device_tm_start(struct ccw_device * cdev,struct tcw * tcw,unsigned long intparm,u8 lpm)629 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
630 unsigned long intparm, u8 lpm)
631 {
632 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
633 PAGE_DEFAULT_KEY);
634 }
635 EXPORT_SYMBOL(ccw_device_tm_start);
636
637 /**
638 * ccw_device_tm_start_timeout() - perform start function
639 * @cdev: ccw device on which to perform the start function
640 * @tcw: transport-command word to be started
641 * @intparm: user defined parameter to be passed to the interrupt handler
642 * @lpm: mask of paths to use
643 * @expires: time span in jiffies after which to abort request
644 *
645 * Start the tcw on the given ccw device. Return zero on success, non-zero
646 * otherwise.
647 */
ccw_device_tm_start_timeout(struct ccw_device * cdev,struct tcw * tcw,unsigned long intparm,u8 lpm,int expires)648 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
649 unsigned long intparm, u8 lpm, int expires)
650 {
651 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
652 PAGE_DEFAULT_KEY, expires);
653 }
654 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
655
656 /**
657 * ccw_device_get_mdc() - accumulate max data count
658 * @cdev: ccw device for which the max data count is accumulated
659 * @mask: mask of paths to use
660 *
661 * Return the number of 64K-bytes blocks all paths at least support
662 * for a transport command. Return value 0 indicates failure.
663 */
ccw_device_get_mdc(struct ccw_device * cdev,u8 mask)664 int ccw_device_get_mdc(struct ccw_device *cdev, u8 mask)
665 {
666 struct subchannel *sch = to_subchannel(cdev->dev.parent);
667 struct channel_path *chp;
668 struct chp_id chpid;
669 int mdc = 0, i;
670
671 if (!sch->schib.pmcw.dnv)
672 return 0;
673
674 /* Adjust requested path mask to excluded varied off paths. */
675 if (mask)
676 mask &= sch->lpm;
677 else
678 mask = sch->lpm;
679
680 chp_id_init(&chpid);
681 for (i = 0; i < 8; i++) {
682 if (!(mask & (0x80 >> i)))
683 continue;
684 chpid.id = sch->schib.pmcw.chpid[i];
685 chp = chpid_to_chp(chpid);
686 if (!chp)
687 continue;
688
689 mutex_lock(&chp->lock);
690 if (!chp->desc_fmt1.f) {
691 mutex_unlock(&chp->lock);
692 return 0;
693 }
694 if (!chp->desc_fmt1.r)
695 mdc = 1;
696 mdc = mdc ? min_t(int, mdc, chp->desc_fmt1.mdc) :
697 chp->desc_fmt1.mdc;
698 mutex_unlock(&chp->lock);
699 }
700
701 return mdc;
702 }
703 EXPORT_SYMBOL(ccw_device_get_mdc);
704
705 /**
706 * ccw_device_tm_intrg() - perform interrogate function
707 * @cdev: ccw device on which to perform the interrogate function
708 *
709 * Perform an interrogate function on the given ccw device. Return zero on
710 * success, non-zero otherwise.
711 */
ccw_device_tm_intrg(struct ccw_device * cdev)712 int ccw_device_tm_intrg(struct ccw_device *cdev)
713 {
714 struct subchannel *sch = to_subchannel(cdev->dev.parent);
715
716 if (!sch->schib.pmcw.dnv)
717 return -ENODEV;
718 if (!sch->schib.pmcw.ena)
719 return -EINVAL;
720 if (cdev->private->state != DEV_STATE_ONLINE)
721 return -EIO;
722 if (!scsw_is_tm(&sch->schib.scsw) ||
723 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
724 return -EINVAL;
725 return cio_tm_intrg(sch);
726 }
727 EXPORT_SYMBOL(ccw_device_tm_intrg);
728
729 /**
730 * ccw_device_get_schid() - obtain a subchannel id
731 * @cdev: device to obtain the id for
732 * @schid: where to fill in the values
733 */
ccw_device_get_schid(struct ccw_device * cdev,struct subchannel_id * schid)734 void ccw_device_get_schid(struct ccw_device *cdev, struct subchannel_id *schid)
735 {
736 struct subchannel *sch = to_subchannel(cdev->dev.parent);
737
738 *schid = sch->schid;
739 }
740 EXPORT_SYMBOL_GPL(ccw_device_get_schid);
741
742 /**
743 * ccw_device_pnso() - Perform Network-Subchannel Operation
744 * @cdev: device on which PNSO is performed
745 * @pnso_area: request and response block for the operation
746 * @oc: Operation Code
747 * @resume_token: resume token for multiblock response
748 * @cnc: Boolean change-notification control
749 *
750 * pnso_area must be allocated by the caller with get_zeroed_page(GFP_KERNEL)
751 *
752 * Returns 0 on success.
753 */
ccw_device_pnso(struct ccw_device * cdev,struct chsc_pnso_area * pnso_area,u8 oc,struct chsc_pnso_resume_token resume_token,int cnc)754 int ccw_device_pnso(struct ccw_device *cdev,
755 struct chsc_pnso_area *pnso_area, u8 oc,
756 struct chsc_pnso_resume_token resume_token, int cnc)
757 {
758 struct subchannel_id schid;
759
760 ccw_device_get_schid(cdev, &schid);
761 return chsc_pnso(schid, pnso_area, oc, resume_token, cnc);
762 }
763 EXPORT_SYMBOL_GPL(ccw_device_pnso);
764
765 /**
766 * ccw_device_get_cssid() - obtain Channel Subsystem ID
767 * @cdev: device to obtain the CSSID for
768 * @cssid: The resulting Channel Subsystem ID
769 */
ccw_device_get_cssid(struct ccw_device * cdev,u8 * cssid)770 int ccw_device_get_cssid(struct ccw_device *cdev, u8 *cssid)
771 {
772 struct device *sch_dev = cdev->dev.parent;
773 struct channel_subsystem *css = to_css(sch_dev->parent);
774
775 if (css->id_valid)
776 *cssid = css->cssid;
777 return css->id_valid ? 0 : -ENODEV;
778 }
779 EXPORT_SYMBOL_GPL(ccw_device_get_cssid);
780
781 /**
782 * ccw_device_get_iid() - obtain MIF-image ID
783 * @cdev: device to obtain the MIF-image ID for
784 * @iid: The resulting MIF-image ID
785 */
ccw_device_get_iid(struct ccw_device * cdev,u8 * iid)786 int ccw_device_get_iid(struct ccw_device *cdev, u8 *iid)
787 {
788 struct device *sch_dev = cdev->dev.parent;
789 struct channel_subsystem *css = to_css(sch_dev->parent);
790
791 if (css->id_valid)
792 *iid = css->iid;
793 return css->id_valid ? 0 : -ENODEV;
794 }
795 EXPORT_SYMBOL_GPL(ccw_device_get_iid);
796
797 /**
798 * ccw_device_get_chpid() - obtain Channel Path ID
799 * @cdev: device to obtain the Channel Path ID for
800 * @chp_idx: Index of the channel path
801 * @chpid: The resulting Channel Path ID
802 */
ccw_device_get_chpid(struct ccw_device * cdev,int chp_idx,u8 * chpid)803 int ccw_device_get_chpid(struct ccw_device *cdev, int chp_idx, u8 *chpid)
804 {
805 struct subchannel *sch = to_subchannel(cdev->dev.parent);
806 int mask;
807
808 if ((chp_idx < 0) || (chp_idx > 7))
809 return -EINVAL;
810 if (!sch->schib.pmcw.dnv)
811 return -ENODEV;
812 mask = 0x80 >> chp_idx;
813 if (!(sch->schib.pmcw.pim & mask))
814 return -ENODEV;
815
816 *chpid = sch->schib.pmcw.chpid[chp_idx];
817 return 0;
818 }
819 EXPORT_SYMBOL_GPL(ccw_device_get_chpid);
820
821 /**
822 * ccw_device_get_chid() - obtain Channel ID associated with specified CHPID
823 * @cdev: device to obtain the Channel ID for
824 * @chp_idx: Index of the channel path
825 * @chid: The resulting Channel ID
826 */
ccw_device_get_chid(struct ccw_device * cdev,int chp_idx,u16 * chid)827 int ccw_device_get_chid(struct ccw_device *cdev, int chp_idx, u16 *chid)
828 {
829 struct chp_id cssid_chpid;
830 struct channel_path *chp;
831 int rc;
832
833 chp_id_init(&cssid_chpid);
834 rc = ccw_device_get_chpid(cdev, chp_idx, &cssid_chpid.id);
835 if (rc)
836 return rc;
837 chp = chpid_to_chp(cssid_chpid);
838 if (!chp)
839 return -ENODEV;
840
841 mutex_lock(&chp->lock);
842 if (chp->desc_fmt1.flags & 0x10)
843 *chid = chp->desc_fmt1.chid;
844 else
845 rc = -ENODEV;
846 mutex_unlock(&chp->lock);
847
848 return rc;
849 }
850 EXPORT_SYMBOL_GPL(ccw_device_get_chid);
851
852 /*
853 * Allocate zeroed dma coherent 31 bit addressable memory using
854 * the subchannels dma pool. Maximal size of allocation supported
855 * is PAGE_SIZE.
856 */
ccw_device_dma_zalloc(struct ccw_device * cdev,size_t size,dma32_t * dma_handle)857 void *ccw_device_dma_zalloc(struct ccw_device *cdev, size_t size,
858 dma32_t *dma_handle)
859 {
860 void *addr;
861
862 if (!get_device(&cdev->dev))
863 return NULL;
864 addr = __cio_gp_dma_zalloc(cdev->private->dma_pool, &cdev->dev, size, dma_handle);
865 if (IS_ERR_OR_NULL(addr))
866 put_device(&cdev->dev);
867 return addr;
868 }
869 EXPORT_SYMBOL(ccw_device_dma_zalloc);
870
ccw_device_dma_free(struct ccw_device * cdev,void * cpu_addr,size_t size)871 void ccw_device_dma_free(struct ccw_device *cdev, void *cpu_addr, size_t size)
872 {
873 if (!cpu_addr)
874 return;
875 cio_gp_dma_free(cdev->private->dma_pool, cpu_addr, size);
876 put_device(&cdev->dev);
877 }
878 EXPORT_SYMBOL(ccw_device_dma_free);
879
880 EXPORT_SYMBOL(ccw_device_set_options_mask);
881 EXPORT_SYMBOL(ccw_device_set_options);
882 EXPORT_SYMBOL(ccw_device_clear_options);
883 EXPORT_SYMBOL(ccw_device_clear);
884 EXPORT_SYMBOL(ccw_device_halt);
885 EXPORT_SYMBOL(ccw_device_resume);
886 EXPORT_SYMBOL(ccw_device_start_timeout);
887 EXPORT_SYMBOL(ccw_device_start);
888 EXPORT_SYMBOL(ccw_device_start_timeout_key);
889 EXPORT_SYMBOL(ccw_device_start_key);
890 EXPORT_SYMBOL(ccw_device_get_ciw);
891 EXPORT_SYMBOL(ccw_device_get_path_mask);
892 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
893 EXPORT_SYMBOL_GPL(ccw_device_get_util_str);
894