blob: b00f3ed051a094bf259d90b62026959ad139143c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/chsc.c
3 * S/390 common I/O routines -- channel subsystem call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Arnd Bergmann (arndb@de.ibm.com)
10 */
11
12#include <linux/module.h>
13#include <linux/config.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/device.h>
17
18#include <asm/cio.h>
19
20#include "css.h"
21#include "cio.h"
22#include "cio_debug.h"
23#include "ioasm.h"
24#include "chsc.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static void *sei_page;
27
28static int new_channel_path(int chpid);
29
30static inline void
31set_chp_logically_online(int chp, int onoff)
32{
Cornelia Hucka28c6942006-01-06 00:19:23 -080033 css[0]->chps[chp]->state = onoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36static int
37get_chp_status(int chp)
38{
Cornelia Hucka28c6942006-01-06 00:19:23 -080039 return (css[0]->chps[chp] ? css[0]->chps[chp]->state : -ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42void
43chsc_validate_chpids(struct subchannel *sch)
44{
45 int mask, chp;
46
47 for (chp = 0; chp <= 7; chp++) {
48 mask = 0x80 >> chp;
49 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
50 /* disable using this path */
51 sch->opm &= ~mask;
52 }
53}
54
55void
56chpid_is_actually_online(int chp)
57{
58 int state;
59
60 state = get_chp_status(chp);
61 if (state < 0) {
62 need_rescan = 1;
63 queue_work(slow_path_wq, &slow_path_work);
64 } else
65 WARN_ON(!state);
66}
67
68/* FIXME: this is _always_ called for every subchannel. shouldn't we
69 * process more than one at a time? */
70static int
71chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
72{
73 int ccode, j;
74
75 struct {
76 struct chsc_header request;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080077 u16 reserved1a:10;
78 u16 ssid:2;
79 u16 reserved1b:4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u16 f_sch; /* first subchannel */
81 u16 reserved2;
82 u16 l_sch; /* last subchannel */
83 u32 reserved3;
84 struct chsc_header response;
85 u32 reserved4;
86 u8 sch_valid : 1;
87 u8 dev_valid : 1;
88 u8 st : 3; /* subchannel type */
89 u8 zeroes : 3;
90 u8 unit_addr; /* unit address */
91 u16 devno; /* device number */
92 u8 path_mask;
93 u8 fla_valid_mask;
94 u16 sch; /* subchannel */
95 u8 chpid[8]; /* chpids 0-7 */
96 u16 fla[8]; /* full link addresses 0-7 */
97 } *ssd_area;
98
99 ssd_area = page;
100
Cornelia Huck495a5b42006-03-24 03:15:14 -0800101 ssd_area->request.length = 0x0010;
102 ssd_area->request.code = 0x0004;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800104 ssd_area->ssid = sch->schid.ssid;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800105 ssd_area->f_sch = sch->schid.sch_no;
106 ssd_area->l_sch = sch->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 ccode = chsc(ssd_area);
109 if (ccode > 0) {
110 pr_debug("chsc returned with ccode = %d\n", ccode);
111 return (ccode == 3) ? -ENODEV : -EBUSY;
112 }
113
114 switch (ssd_area->response.code) {
115 case 0x0001: /* everything ok */
116 break;
117 case 0x0002:
118 CIO_CRW_EVENT(2, "Invalid command!\n");
119 return -EINVAL;
120 case 0x0003:
121 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
122 return -EINVAL;
123 case 0x0004:
124 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
125 return -EOPNOTSUPP;
126 default:
127 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
128 ssd_area->response.code);
129 return -EIO;
130 }
131
132 /*
133 * ssd_area->st stores the type of the detected
134 * subchannel, with the following definitions:
135 *
136 * 0: I/O subchannel: All fields have meaning
137 * 1: CHSC subchannel: Only sch_val, st and sch
138 * have meaning
139 * 2: Message subchannel: All fields except unit_addr
140 * have meaning
141 * 3: ADM subchannel: Only sch_val, st and sch
142 * have meaning
143 *
144 * Other types are currently undefined.
145 */
146 if (ssd_area->st > 3) { /* uhm, that looks strange... */
147 CIO_CRW_EVENT(0, "Strange subchannel type %d"
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800148 " for sch 0.%x.%04x\n", ssd_area->st,
149 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /*
151 * There may have been a new subchannel type defined in the
152 * time since this code was written; since we don't know which
153 * fields have meaning and what to do with it we just jump out
154 */
155 return 0;
156 } else {
157 const char *type[4] = {"I/O", "chsc", "message", "ADM"};
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800158 CIO_CRW_EVENT(6, "ssd: sch 0.%x.%04x is %s subchannel\n",
159 sch->schid.ssid, sch->schid.sch_no,
160 type[ssd_area->st]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 sch->ssd_info.valid = 1;
163 sch->ssd_info.type = ssd_area->st;
164 }
165
166 if (ssd_area->st == 0 || ssd_area->st == 2) {
167 for (j = 0; j < 8; j++) {
168 if (!((0x80 >> j) & ssd_area->path_mask &
169 ssd_area->fla_valid_mask))
170 continue;
171 sch->ssd_info.chpid[j] = ssd_area->chpid[j];
172 sch->ssd_info.fla[j] = ssd_area->fla[j];
173 }
174 }
175 return 0;
176}
177
178int
179css_get_ssd_info(struct subchannel *sch)
180{
181 int ret;
182 void *page;
183
184 page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
185 if (!page)
186 return -ENOMEM;
187 spin_lock_irq(&sch->lock);
188 ret = chsc_get_sch_desc_irq(sch, page);
189 if (ret) {
190 static int cio_chsc_err_msg;
191
192 if (!cio_chsc_err_msg) {
193 printk(KERN_ERR
194 "chsc_get_sch_descriptions:"
195 " Error %d while doing chsc; "
196 "processing some machine checks may "
197 "not work\n", ret);
198 cio_chsc_err_msg = 1;
199 }
200 }
201 spin_unlock_irq(&sch->lock);
202 free_page((unsigned long)page);
203 if (!ret) {
204 int j, chpid;
205 /* Allocate channel path structures, if needed. */
206 for (j = 0; j < 8; j++) {
207 chpid = sch->ssd_info.chpid[j];
208 if (chpid && (get_chp_status(chpid) < 0))
209 new_channel_path(chpid);
210 }
211 }
212 return ret;
213}
214
215static int
216s390_subchannel_remove_chpid(struct device *dev, void *data)
217{
218 int j;
219 int mask;
220 struct subchannel *sch;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800221 struct channel_path *chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct schib schib;
223
224 sch = to_subchannel(dev);
225 chpid = data;
226 for (j = 0; j < 8; j++)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800227 if (sch->schib.pmcw.chpid[j] == chpid->id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229 if (j >= 8)
230 return 0;
231
232 mask = 0x80 >> j;
Cornelia Huckc48d8652006-02-11 17:55:57 -0800233 spin_lock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800235 stsch(sch->schid, &schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!schib.pmcw.dnv)
237 goto out_unreg;
238 memcpy(&sch->schib, &schib, sizeof(struct schib));
239 /* Check for single path devices. */
240 if (sch->schib.pmcw.pim == 0x80)
241 goto out_unreg;
242 if (sch->vpm == mask)
243 goto out_unreg;
244
Peter Oberparleiter329b7852006-04-27 18:40:02 -0700245 if ((sch->schib.scsw.actl & SCSW_ACTL_DEVACT) &&
246 (sch->schib.scsw.actl & SCSW_ACTL_SCHACT) &&
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200247 (sch->schib.pmcw.lpum == mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int cc;
249
250 cc = cio_clear(sch);
251 if (cc == -ENODEV)
252 goto out_unreg;
253 /* Call handler. */
254 if (sch->driver && sch->driver->termination)
255 sch->driver->termination(&sch->dev);
256 goto out_unlock;
257 }
258
259 /* trigger path verification. */
260 if (sch->driver && sch->driver->verify)
261 sch->driver->verify(&sch->dev);
262out_unlock:
Cornelia Huckc48d8652006-02-11 17:55:57 -0800263 spin_unlock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
265out_unreg:
Cornelia Huckc48d8652006-02-11 17:55:57 -0800266 spin_unlock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sch->lpm = 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800268 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 css_clear_subchannel_slow_list();
270 need_rescan = 1;
271 }
272 return 0;
273}
274
275static inline void
276s390_set_chpid_offline( __u8 chpid)
277{
278 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800279 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 sprintf(dbf_txt, "chpr%x", chpid);
282 CIO_TRACE_EVENT(2, dbf_txt);
283
284 if (get_chp_status(chpid) <= 0)
285 return;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800286 dev = get_device(&css[0]->chps[chpid]->dev);
287 bus_for_each_dev(&css_bus_type, NULL, to_channelpath(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 s390_subchannel_remove_chpid);
289
290 if (need_rescan || css_slow_subchannels_exist())
291 queue_work(slow_path_wq, &slow_path_work);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800292 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800295struct res_acc_data {
296 struct channel_path *chp;
297 u32 fla_mask;
298 u16 fla;
299};
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800302s390_process_res_acc_sch(struct res_acc_data *res_data, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int found;
305 int chp;
306 int ccode;
307
308 found = 0;
309 for (chp = 0; chp <= 7; chp++)
310 /*
311 * check if chpid is in information updated by ssd
312 */
313 if (sch->ssd_info.valid &&
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800314 sch->ssd_info.chpid[chp] == res_data->chp->id &&
315 (sch->ssd_info.fla[chp] & res_data->fla_mask)
316 == res_data->fla) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 found = 1;
318 break;
319 }
320
321 if (found == 0)
322 return 0;
323
324 /*
325 * Do a stsch to update our subchannel structure with the
326 * new path information and eventually check for logically
327 * offline chpids.
328 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800329 ccode = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (ccode > 0)
331 return 0;
332
333 return 0x80 >> chp;
334}
335
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800336static inline int
337s390_process_res_acc_new_sch(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800339 struct schib schib;
340 int ret;
341 /*
342 * We don't know the device yet, but since a path
343 * may be available now to the device we'll have
344 * to do recognition again.
345 * Since we don't have any idea about which chpid
346 * that beast may be on we'll have to do a stsch
347 * on all devices, grr...
348 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800349 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800350 /* We're through */
351 return need_rescan ? -EAGAIN : -ENXIO;
352
353 /* Put it on the slow path. */
354 ret = css_enqueue_subchannel_slow(schid);
355 if (ret) {
356 css_clear_subchannel_slow_list();
357 need_rescan = 1;
358 return -EAGAIN;
359 }
360 return 0;
361}
362
363static int
364__s390_process_res_acc(struct subchannel_id schid, void *data)
365{
366 int chp_mask, old_lpm;
367 struct res_acc_data *res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct subchannel *sch;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800369
370 res_data = (struct res_acc_data *)data;
371 sch = get_subchannel_by_schid(schid);
372 if (!sch)
373 /* Check if a subchannel is newly available. */
374 return s390_process_res_acc_new_sch(schid);
375
376 spin_lock_irq(&sch->lock);
377
378 chp_mask = s390_process_res_acc_sch(res_data, sch);
379
380 if (chp_mask == 0) {
381 spin_unlock_irq(&sch->lock);
382 return 0;
383 }
384 old_lpm = sch->lpm;
385 sch->lpm = ((sch->schib.pmcw.pim &
386 sch->schib.pmcw.pam &
387 sch->schib.pmcw.pom)
388 | chp_mask) & sch->opm;
389 if (!old_lpm && sch->lpm)
390 device_trigger_reprobe(sch);
391 else if (sch->driver && sch->driver->verify)
392 sch->driver->verify(&sch->dev);
393
394 spin_unlock_irq(&sch->lock);
395 put_device(&sch->dev);
396 return (res_data->fla_mask == 0xffff) ? -ENODEV : 0;
397}
398
399
400static int
401s390_process_res_acc (struct res_acc_data *res_data)
402{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800403 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 char dbf_txt[15];
405
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800406 sprintf(dbf_txt, "accpr%x", res_data->chp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 CIO_TRACE_EVENT( 2, dbf_txt);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800408 if (res_data->fla != 0) {
409 sprintf(dbf_txt, "fla%x", res_data->fla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 CIO_TRACE_EVENT( 2, dbf_txt);
411 }
412
413 /*
414 * I/O resources may have become accessible.
415 * Scan through all subchannels that may be concerned and
416 * do a validation on those.
417 * The more information we have (info), the less scanning
418 * will we have to do.
419 */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800420 rc = for_each_subchannel(__s390_process_res_acc, res_data);
421 if (css_slow_subchannels_exist())
422 rc = -EAGAIN;
423 else if (rc != -EAGAIN)
424 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return rc;
426}
427
428static int
429__get_chpid_from_lir(void *data)
430{
431 struct lir {
432 u8 iq;
433 u8 ic;
434 u16 sci;
435 /* incident-node descriptor */
436 u32 indesc[28];
437 /* attached-node descriptor */
438 u32 andesc[28];
439 /* incident-specific information */
440 u32 isinfo[28];
441 } *lir;
442
443 lir = (struct lir*) data;
444 if (!(lir->iq&0x80))
445 /* NULL link incident record */
446 return -EINVAL;
447 if (!(lir->indesc[0]&0xc0000000))
448 /* node descriptor not valid */
449 return -EINVAL;
450 if (!(lir->indesc[0]&0x10000000))
451 /* don't handle device-type nodes - FIXME */
452 return -EINVAL;
453 /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
454
455 return (u16) (lir->indesc[0]&0x000000ff);
456}
457
458int
459chsc_process_crw(void)
460{
461 int chpid, ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800462 struct res_acc_data res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct {
464 struct chsc_header request;
465 u32 reserved1;
466 u32 reserved2;
467 u32 reserved3;
468 struct chsc_header response;
469 u32 reserved4;
470 u8 flags;
471 u8 vf; /* validity flags */
472 u8 rs; /* reporting source */
473 u8 cc; /* content code */
474 u16 fla; /* full link address */
475 u16 rsid; /* reporting source id */
476 u32 reserved5;
477 u32 reserved6;
478 u32 ccdf[96]; /* content-code dependent field */
479 /* ccdf has to be big enough for a link-incident record */
480 } *sei_area;
481
482 if (!sei_page)
483 return 0;
484 /*
485 * build the chsc request block for store event information
486 * and do the call
487 * This function is only called by the machine check handler thread,
488 * so we don't need locking for the sei_page.
489 */
490 sei_area = sei_page;
491
492 CIO_TRACE_EVENT( 2, "prcss");
493 ret = 0;
494 do {
495 int ccode, status;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800496 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 memset(sei_area, 0, sizeof(*sei_area));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800498 memset(&res_data, 0, sizeof(struct res_acc_data));
Cornelia Huck495a5b42006-03-24 03:15:14 -0800499 sei_area->request.length = 0x0010;
500 sei_area->request.code = 0x000e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 ccode = chsc(sei_area);
503 if (ccode > 0)
504 return 0;
505
506 switch (sei_area->response.code) {
507 /* for debug purposes, check for problems */
508 case 0x0001:
509 CIO_CRW_EVENT(4, "chsc_process_crw: event information "
510 "successfully stored\n");
511 break; /* everything ok */
512 case 0x0002:
513 CIO_CRW_EVENT(2,
514 "chsc_process_crw: invalid command!\n");
515 return 0;
516 case 0x0003:
517 CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
518 "request block!\n");
519 return 0;
520 case 0x0005:
521 CIO_CRW_EVENT(2, "chsc_process_crw: no event "
522 "information stored\n");
523 return 0;
524 default:
525 CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
526 sei_area->response.code);
527 return 0;
528 }
529
530 /* Check if we might have lost some information. */
531 if (sei_area->flags & 0x40)
532 CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
533 "has been lost due to overflow!\n");
534
535 if (sei_area->rs != 4) {
536 CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
537 "(%04X) isn't a chpid!\n",
538 sei_area->rsid);
539 continue;
540 }
541
542 /* which kind of information was stored? */
543 switch (sei_area->cc) {
544 case 1: /* link incident*/
545 CIO_CRW_EVENT(4, "chsc_process_crw: "
546 "channel subsystem reports link incident,"
547 " reporting source is chpid %x\n",
548 sei_area->rsid);
549 chpid = __get_chpid_from_lir(sei_area->ccdf);
550 if (chpid < 0)
551 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
552 __FUNCTION__);
553 else
554 s390_set_chpid_offline(chpid);
555 break;
556
557 case 2: /* i/o resource accessibiliy */
558 CIO_CRW_EVENT(4, "chsc_process_crw: "
559 "channel subsystem reports some I/O "
560 "devices may have become accessible\n");
561 pr_debug("Data received after sei: \n");
562 pr_debug("Validity flags: %x\n", sei_area->vf);
563
564 /* allocate a new channel path structure, if needed */
565 status = get_chp_status(sei_area->rsid);
566 if (status < 0)
567 new_channel_path(sei_area->rsid);
568 else if (!status)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800569 break;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800570 dev = get_device(&css[0]->chps[sei_area->rsid]->dev);
571 res_data.chp = to_channelpath(dev);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800572 pr_debug("chpid: %x", sei_area->rsid);
573 if ((sei_area->vf & 0xc0) != 0) {
574 res_data.fla = sei_area->fla;
575 if ((sei_area->vf & 0xc0) == 0xc0) {
576 pr_debug(" full link addr: %x",
577 sei_area->fla);
578 res_data.fla_mask = 0xffff;
579 } else {
580 pr_debug(" link addr: %x",
581 sei_area->fla);
582 res_data.fla_mask = 0xff00;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800585 ret = s390_process_res_acc(&res_data);
586 pr_debug("\n\n");
Cornelia Hucka28c6942006-01-06 00:19:23 -0800587 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
589
590 default: /* other stuff */
591 CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
592 sei_area->cc);
593 break;
594 }
595 } while (sei_area->flags & 0x80);
596 return ret;
597}
598
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800599static inline int
600__chp_add_new_sch(struct subchannel_id schid)
601{
602 struct schib schib;
603 int ret;
604
605 if (stsch(schid, &schib))
606 /* We're through */
607 return need_rescan ? -EAGAIN : -ENXIO;
608
609 /* Put it on the slow path. */
610 ret = css_enqueue_subchannel_slow(schid);
611 if (ret) {
612 css_clear_subchannel_slow_list();
613 need_rescan = 1;
614 return -EAGAIN;
615 }
616 return 0;
617}
618
619
620static int
621__chp_add(struct subchannel_id schid, void *data)
622{
623 int i;
624 struct channel_path *chp;
625 struct subchannel *sch;
626
627 chp = (struct channel_path *)data;
628 sch = get_subchannel_by_schid(schid);
629 if (!sch)
630 /* Check if the subchannel is now available. */
631 return __chp_add_new_sch(schid);
Cornelia Huckc48d8652006-02-11 17:55:57 -0800632 spin_lock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800633 for (i=0; i<8; i++)
634 if (sch->schib.pmcw.chpid[i] == chp->id) {
635 if (stsch(sch->schid, &sch->schib) != 0) {
636 /* Endgame. */
Stefan Bader6dcfca72006-04-27 18:40:04 -0700637 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800638 return -ENXIO;
639 }
640 break;
641 }
642 if (i==8) {
Stefan Bader6dcfca72006-04-27 18:40:04 -0700643 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800644 return 0;
645 }
646 sch->lpm = ((sch->schib.pmcw.pim &
647 sch->schib.pmcw.pam &
648 sch->schib.pmcw.pom)
649 | 0x80 >> i) & sch->opm;
650
651 if (sch->driver && sch->driver->verify)
652 sch->driver->verify(&sch->dev);
653
Cornelia Huckc48d8652006-02-11 17:55:57 -0800654 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800655 put_device(&sch->dev);
656 return 0;
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static int
660chp_add(int chpid)
661{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800662 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800664 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!get_chp_status(chpid))
667 return 0; /* no need to do the rest */
668
669 sprintf(dbf_txt, "cadd%x", chpid);
670 CIO_TRACE_EVENT(2, dbf_txt);
671
Cornelia Hucka28c6942006-01-06 00:19:23 -0800672 dev = get_device(&css[0]->chps[chpid]->dev);
673 rc = for_each_subchannel(__chp_add, to_channelpath(dev));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800674 if (css_slow_subchannels_exist())
675 rc = -EAGAIN;
676 if (rc != -EAGAIN)
677 rc = 0;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800678 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return rc;
680}
681
682/*
683 * Handling of crw machine checks with channel path source.
684 */
685int
686chp_process_crw(int chpid, int on)
687{
688 if (on == 0) {
689 /* Path has gone. We use the link incident routine.*/
690 s390_set_chpid_offline(chpid);
691 return 0; /* De-register is async anyway. */
692 }
693 /*
694 * Path has come. Allocate a new channel path structure,
695 * if needed.
696 */
697 if (get_chp_status(chpid) < 0)
698 new_channel_path(chpid);
699 /* Avoid the extra overhead in process_rec_acc. */
700 return chp_add(chpid);
701}
702
703static inline int
704__check_for_io_and_kill(struct subchannel *sch, int index)
705{
706 int cc;
707
708 if (!device_is_online(sch))
709 /* cio could be doing I/O. */
710 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800711 cc = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (cc)
713 return 0;
714 if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index)) {
715 device_set_waiting(sch);
716 return 1;
717 }
718 return 0;
719}
720
721static inline void
722__s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
723{
724 int chp, old_lpm;
725 unsigned long flags;
726
727 if (!sch->ssd_info.valid)
728 return;
729
730 spin_lock_irqsave(&sch->lock, flags);
731 old_lpm = sch->lpm;
732 for (chp = 0; chp < 8; chp++) {
733 if (sch->ssd_info.chpid[chp] != chpid)
734 continue;
735
736 if (on) {
737 sch->opm |= (0x80 >> chp);
738 sch->lpm |= (0x80 >> chp);
739 if (!old_lpm)
740 device_trigger_reprobe(sch);
741 else if (sch->driver && sch->driver->verify)
742 sch->driver->verify(&sch->dev);
743 } else {
744 sch->opm &= ~(0x80 >> chp);
745 sch->lpm &= ~(0x80 >> chp);
746 /*
747 * Give running I/O a grace period in which it
748 * can successfully terminate, even using the
749 * just varied off path. Then kill it.
750 */
751 if (!__check_for_io_and_kill(sch, chp) && !sch->lpm) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800752 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 css_clear_subchannel_slow_list();
754 need_rescan = 1;
755 }
756 } else if (sch->driver && sch->driver->verify)
757 sch->driver->verify(&sch->dev);
758 }
759 break;
760 }
761 spin_unlock_irqrestore(&sch->lock, flags);
762}
763
764static int
765s390_subchannel_vary_chpid_off(struct device *dev, void *data)
766{
767 struct subchannel *sch;
768 __u8 *chpid;
769
770 sch = to_subchannel(dev);
771 chpid = data;
772
773 __s390_subchannel_vary_chpid(sch, *chpid, 0);
774 return 0;
775}
776
777static int
778s390_subchannel_vary_chpid_on(struct device *dev, void *data)
779{
780 struct subchannel *sch;
781 __u8 *chpid;
782
783 sch = to_subchannel(dev);
784 chpid = data;
785
786 __s390_subchannel_vary_chpid(sch, *chpid, 1);
787 return 0;
788}
789
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800790static int
791__s390_vary_chpid_on(struct subchannel_id schid, void *data)
792{
793 struct schib schib;
794 struct subchannel *sch;
795
796 sch = get_subchannel_by_schid(schid);
797 if (sch) {
798 put_device(&sch->dev);
799 return 0;
800 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800801 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800802 /* We're through */
803 return -ENXIO;
804 /* Put it on the slow path. */
805 if (css_enqueue_subchannel_slow(schid)) {
806 css_clear_subchannel_slow_list();
807 need_rescan = 1;
808 return -EAGAIN;
809 }
810 return 0;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
814 * Function: s390_vary_chpid
815 * Varies the specified chpid online or offline
816 */
817static int
818s390_vary_chpid( __u8 chpid, int on)
819{
820 char dbf_text[15];
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800821 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
824 CIO_TRACE_EVENT( 2, dbf_text);
825
826 status = get_chp_status(chpid);
827 if (status < 0) {
828 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
829 return -EINVAL;
830 }
831
832 if (!on && !status) {
833 printk(KERN_ERR "chpid %x is already offline\n", chpid);
834 return -EINVAL;
835 }
836
837 set_chp_logically_online(chpid, on);
838
839 /*
840 * Redo PathVerification on the devices the chpid connects to
841 */
842
843 bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
844 s390_subchannel_vary_chpid_on :
845 s390_subchannel_vary_chpid_off);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800846 if (on)
847 /* Scan for new devices on varied on path. */
848 for_each_subchannel(__s390_vary_chpid_on, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (need_rescan || css_slow_subchannels_exist())
850 queue_work(slow_path_wq, &slow_path_work);
851 return 0;
852}
853
854/*
Cornelia Huck495a5b42006-03-24 03:15:14 -0800855 * Channel measurement related functions
856 */
857static ssize_t
858chp_measurement_chars_read(struct kobject *kobj, char *buf, loff_t off,
859 size_t count)
860{
861 struct channel_path *chp;
862 unsigned int size;
863
864 chp = to_channelpath(container_of(kobj, struct device, kobj));
865 if (!chp->cmg_chars)
866 return 0;
867
868 size = sizeof(struct cmg_chars);
869
870 if (off > size)
871 return 0;
872 if (off + count > size)
873 count = size - off;
874 memcpy(buf, chp->cmg_chars + off, count);
875 return count;
876}
877
878static struct bin_attribute chp_measurement_chars_attr = {
879 .attr = {
880 .name = "measurement_chars",
881 .mode = S_IRUSR,
882 .owner = THIS_MODULE,
883 },
884 .size = sizeof(struct cmg_chars),
885 .read = chp_measurement_chars_read,
886};
887
888static void
889chp_measurement_copy_block(struct cmg_entry *buf,
890 struct channel_subsystem *css, int chpid)
891{
892 void *area;
893 struct cmg_entry *entry, reference_buf;
894 int idx;
895
896 if (chpid < 128) {
897 area = css->cub_addr1;
898 idx = chpid;
899 } else {
900 area = css->cub_addr2;
901 idx = chpid - 128;
902 }
903 entry = area + (idx * sizeof(struct cmg_entry));
904 do {
905 memcpy(buf, entry, sizeof(*entry));
906 memcpy(&reference_buf, entry, sizeof(*entry));
907 } while (reference_buf.values[0] != buf->values[0]);
908}
909
910static ssize_t
911chp_measurement_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
912{
913 struct channel_path *chp;
914 struct channel_subsystem *css;
915 unsigned int size;
916
917 chp = to_channelpath(container_of(kobj, struct device, kobj));
918 css = to_css(chp->dev.parent);
919
Cornelia Huck231caa12006-06-29 14:56:45 +0200920 size = sizeof(struct cmg_entry);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800921
922 /* Only allow single reads. */
923 if (off || count < size)
924 return 0;
925 chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->id);
Cornelia Huck231caa12006-06-29 14:56:45 +0200926 count = size;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800927 return count;
928}
929
930static struct bin_attribute chp_measurement_attr = {
931 .attr = {
932 .name = "measurement",
933 .mode = S_IRUSR,
934 .owner = THIS_MODULE,
935 },
936 .size = sizeof(struct cmg_entry),
937 .read = chp_measurement_read,
938};
939
940static void
941chsc_remove_chp_cmg_attr(struct channel_path *chp)
942{
943 sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_chars_attr);
944 sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_attr);
945}
946
947static int
948chsc_add_chp_cmg_attr(struct channel_path *chp)
949{
950 int ret;
951
952 ret = sysfs_create_bin_file(&chp->dev.kobj,
953 &chp_measurement_chars_attr);
954 if (ret)
955 return ret;
956 ret = sysfs_create_bin_file(&chp->dev.kobj, &chp_measurement_attr);
957 if (ret)
958 sysfs_remove_bin_file(&chp->dev.kobj,
959 &chp_measurement_chars_attr);
960 return ret;
961}
962
963static void
964chsc_remove_cmg_attr(struct channel_subsystem *css)
965{
966 int i;
967
968 for (i = 0; i <= __MAX_CHPID; i++) {
969 if (!css->chps[i])
970 continue;
971 chsc_remove_chp_cmg_attr(css->chps[i]);
972 }
973}
974
975static int
976chsc_add_cmg_attr(struct channel_subsystem *css)
977{
978 int i, ret;
979
980 ret = 0;
981 for (i = 0; i <= __MAX_CHPID; i++) {
982 if (!css->chps[i])
983 continue;
984 ret = chsc_add_chp_cmg_attr(css->chps[i]);
985 if (ret)
986 goto cleanup;
987 }
988 return ret;
989cleanup:
990 for (--i; i >= 0; i--) {
991 if (!css->chps[i])
992 continue;
993 chsc_remove_chp_cmg_attr(css->chps[i]);
994 }
995 return ret;
996}
997
998
999static int
1000__chsc_do_secm(struct channel_subsystem *css, int enable, void *page)
1001{
1002 struct {
1003 struct chsc_header request;
1004 u32 operation_code : 2;
1005 u32 : 30;
1006 u32 key : 4;
1007 u32 : 28;
1008 u32 zeroes1;
1009 u32 cub_addr1;
1010 u32 zeroes2;
1011 u32 cub_addr2;
1012 u32 reserved[13];
1013 struct chsc_header response;
1014 u32 status : 8;
1015 u32 : 4;
1016 u32 fmt : 4;
1017 u32 : 16;
1018 } *secm_area;
1019 int ret, ccode;
1020
1021 secm_area = page;
1022 secm_area->request.length = 0x0050;
1023 secm_area->request.code = 0x0016;
1024
1025 secm_area->key = PAGE_DEFAULT_KEY;
1026 secm_area->cub_addr1 = (u64)(unsigned long)css->cub_addr1;
1027 secm_area->cub_addr2 = (u64)(unsigned long)css->cub_addr2;
1028
1029 secm_area->operation_code = enable ? 0 : 1;
1030
1031 ccode = chsc(secm_area);
1032 if (ccode > 0)
1033 return (ccode == 3) ? -ENODEV : -EBUSY;
1034
1035 switch (secm_area->response.code) {
1036 case 0x0001: /* Success. */
1037 ret = 0;
1038 break;
1039 case 0x0003: /* Invalid block. */
1040 case 0x0007: /* Invalid format. */
1041 case 0x0008: /* Other invalid block. */
1042 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1043 ret = -EINVAL;
1044 break;
1045 case 0x0004: /* Command not provided in model. */
1046 CIO_CRW_EVENT(2, "Model does not provide secm\n");
1047 ret = -EOPNOTSUPP;
1048 break;
1049 case 0x0102: /* cub adresses incorrect */
1050 CIO_CRW_EVENT(2, "Invalid addresses in chsc request block\n");
1051 ret = -EINVAL;
1052 break;
1053 case 0x0103: /* key error */
1054 CIO_CRW_EVENT(2, "Access key error in secm\n");
1055 ret = -EINVAL;
1056 break;
1057 case 0x0105: /* error while starting */
1058 CIO_CRW_EVENT(2, "Error while starting channel measurement\n");
1059 ret = -EIO;
1060 break;
1061 default:
1062 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1063 secm_area->response.code);
1064 ret = -EIO;
1065 }
1066 return ret;
1067}
1068
1069int
1070chsc_secm(struct channel_subsystem *css, int enable)
1071{
1072 void *secm_area;
1073 int ret;
1074
1075 secm_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1076 if (!secm_area)
1077 return -ENOMEM;
1078
1079 mutex_lock(&css->mutex);
1080 if (enable && !css->cm_enabled) {
1081 css->cub_addr1 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1082 css->cub_addr2 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1083 if (!css->cub_addr1 || !css->cub_addr2) {
1084 free_page((unsigned long)css->cub_addr1);
1085 free_page((unsigned long)css->cub_addr2);
1086 free_page((unsigned long)secm_area);
1087 mutex_unlock(&css->mutex);
1088 return -ENOMEM;
1089 }
1090 }
1091 ret = __chsc_do_secm(css, enable, secm_area);
1092 if (!ret) {
1093 css->cm_enabled = enable;
1094 if (css->cm_enabled) {
1095 ret = chsc_add_cmg_attr(css);
1096 if (ret) {
1097 memset(secm_area, 0, PAGE_SIZE);
1098 __chsc_do_secm(css, 0, secm_area);
1099 css->cm_enabled = 0;
1100 }
1101 } else
1102 chsc_remove_cmg_attr(css);
1103 }
1104 if (enable && !css->cm_enabled) {
1105 free_page((unsigned long)css->cub_addr1);
1106 free_page((unsigned long)css->cub_addr2);
1107 }
1108 mutex_unlock(&css->mutex);
1109 free_page((unsigned long)secm_area);
1110 return ret;
1111}
1112
1113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * Files for the channel path entries.
1115 */
1116static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001117chp_status_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1120
1121 if (!chp)
1122 return 0;
1123 return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
1124 sprintf(buf, "offline\n"));
1125}
1126
1127static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001128chp_status_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct channel_path *cp = container_of(dev, struct channel_path, dev);
1131 char cmd[10];
1132 int num_args;
1133 int error;
1134
1135 num_args = sscanf(buf, "%5s", cmd);
1136 if (!num_args)
1137 return count;
1138
1139 if (!strnicmp(cmd, "on", 2))
1140 error = s390_vary_chpid(cp->id, 1);
1141 else if (!strnicmp(cmd, "off", 3))
1142 error = s390_vary_chpid(cp->id, 0);
1143 else
1144 error = -EINVAL;
1145
1146 return error < 0 ? error : count;
1147
1148}
1149
1150static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
1151
1152static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001153chp_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1156
1157 if (!chp)
1158 return 0;
1159 return sprintf(buf, "%x\n", chp->desc.desc);
1160}
1161
1162static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
1163
Cornelia Huck495a5b42006-03-24 03:15:14 -08001164static ssize_t
1165chp_cmg_show(struct device *dev, struct device_attribute *attr, char *buf)
1166{
1167 struct channel_path *chp = to_channelpath(dev);
1168
1169 if (!chp)
1170 return 0;
1171 if (chp->cmg == -1) /* channel measurements not available */
1172 return sprintf(buf, "unknown\n");
1173 return sprintf(buf, "%x\n", chp->cmg);
1174}
1175
1176static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
1177
1178static ssize_t
1179chp_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
1180{
1181 struct channel_path *chp = to_channelpath(dev);
1182
1183 if (!chp)
1184 return 0;
1185 if (chp->shared == -1) /* channel measurements not available */
1186 return sprintf(buf, "unknown\n");
1187 return sprintf(buf, "%x\n", chp->shared);
1188}
1189
1190static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static struct attribute * chp_attrs[] = {
1193 &dev_attr_status.attr,
1194 &dev_attr_type.attr,
Cornelia Huck495a5b42006-03-24 03:15:14 -08001195 &dev_attr_cmg.attr,
1196 &dev_attr_shared.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 NULL,
1198};
1199
1200static struct attribute_group chp_attr_group = {
1201 .attrs = chp_attrs,
1202};
1203
1204static void
1205chp_release(struct device *dev)
1206{
1207 struct channel_path *cp;
1208
1209 cp = container_of(dev, struct channel_path, dev);
1210 kfree(cp);
1211}
1212
1213static int
1214chsc_determine_channel_path_description(int chpid,
1215 struct channel_path_desc *desc)
1216{
1217 int ccode, ret;
1218
1219 struct {
1220 struct chsc_header request;
1221 u32 : 24;
1222 u32 first_chpid : 8;
1223 u32 : 24;
1224 u32 last_chpid : 8;
1225 u32 zeroes1;
1226 struct chsc_header response;
1227 u32 zeroes2;
1228 struct channel_path_desc desc;
1229 } *scpd_area;
1230
1231 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1232 if (!scpd_area)
1233 return -ENOMEM;
1234
Cornelia Huck495a5b42006-03-24 03:15:14 -08001235 scpd_area->request.length = 0x0010;
1236 scpd_area->request.code = 0x0002;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 scpd_area->first_chpid = chpid;
1239 scpd_area->last_chpid = chpid;
1240
1241 ccode = chsc(scpd_area);
1242 if (ccode > 0) {
1243 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1244 goto out;
1245 }
1246
1247 switch (scpd_area->response.code) {
1248 case 0x0001: /* Success. */
1249 memcpy(desc, &scpd_area->desc,
1250 sizeof(struct channel_path_desc));
1251 ret = 0;
1252 break;
1253 case 0x0003: /* Invalid block. */
1254 case 0x0007: /* Invalid format. */
1255 case 0x0008: /* Other invalid block. */
1256 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1257 ret = -EINVAL;
1258 break;
1259 case 0x0004: /* Command not provided in model. */
1260 CIO_CRW_EVENT(2, "Model does not provide scpd\n");
1261 ret = -EOPNOTSUPP;
1262 break;
1263 default:
1264 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1265 scpd_area->response.code);
1266 ret = -EIO;
1267 }
1268out:
1269 free_page((unsigned long)scpd_area);
1270 return ret;
1271}
1272
Cornelia Huck495a5b42006-03-24 03:15:14 -08001273static void
1274chsc_initialize_cmg_chars(struct channel_path *chp, u8 cmcv,
1275 struct cmg_chars *chars)
1276{
1277 switch (chp->cmg) {
1278 case 2:
1279 case 3:
1280 chp->cmg_chars = kmalloc(sizeof(struct cmg_chars),
1281 GFP_KERNEL);
1282 if (chp->cmg_chars) {
1283 int i, mask;
1284 struct cmg_chars *cmg_chars;
1285
1286 cmg_chars = chp->cmg_chars;
1287 for (i = 0; i < NR_MEASUREMENT_CHARS; i++) {
1288 mask = 0x80 >> (i + 3);
1289 if (cmcv & mask)
1290 cmg_chars->values[i] = chars->values[i];
1291 else
1292 cmg_chars->values[i] = 0;
1293 }
1294 }
1295 break;
1296 default:
1297 /* No cmg-dependent data. */
1298 break;
1299 }
1300}
1301
1302static int
1303chsc_get_channel_measurement_chars(struct channel_path *chp)
1304{
1305 int ccode, ret;
1306
1307 struct {
1308 struct chsc_header request;
1309 u32 : 24;
1310 u32 first_chpid : 8;
1311 u32 : 24;
1312 u32 last_chpid : 8;
1313 u32 zeroes1;
1314 struct chsc_header response;
1315 u32 zeroes2;
1316 u32 not_valid : 1;
1317 u32 shared : 1;
1318 u32 : 22;
1319 u32 chpid : 8;
1320 u32 cmcv : 5;
1321 u32 : 11;
1322 u32 cmgq : 8;
1323 u32 cmg : 8;
1324 u32 zeroes3;
1325 u32 data[NR_MEASUREMENT_CHARS];
1326 } *scmc_area;
1327
1328 scmc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1329 if (!scmc_area)
1330 return -ENOMEM;
1331
1332 scmc_area->request.length = 0x0010;
1333 scmc_area->request.code = 0x0022;
1334
1335 scmc_area->first_chpid = chp->id;
1336 scmc_area->last_chpid = chp->id;
1337
1338 ccode = chsc(scmc_area);
1339 if (ccode > 0) {
1340 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1341 goto out;
1342 }
1343
1344 switch (scmc_area->response.code) {
1345 case 0x0001: /* Success. */
1346 if (!scmc_area->not_valid) {
1347 chp->cmg = scmc_area->cmg;
1348 chp->shared = scmc_area->shared;
1349 chsc_initialize_cmg_chars(chp, scmc_area->cmcv,
1350 (struct cmg_chars *)
1351 &scmc_area->data);
1352 } else {
1353 chp->cmg = -1;
1354 chp->shared = -1;
1355 }
1356 ret = 0;
1357 break;
1358 case 0x0003: /* Invalid block. */
1359 case 0x0007: /* Invalid format. */
1360 case 0x0008: /* Invalid bit combination. */
1361 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1362 ret = -EINVAL;
1363 break;
1364 case 0x0004: /* Command not provided. */
1365 CIO_CRW_EVENT(2, "Model does not provide scmc\n");
1366 ret = -EOPNOTSUPP;
1367 break;
1368 default:
1369 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1370 scmc_area->response.code);
1371 ret = -EIO;
1372 }
1373out:
1374 free_page((unsigned long)scmc_area);
1375 return ret;
1376}
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378/*
1379 * Entries for chpids on the system bus.
1380 * This replaces /proc/chpids.
1381 */
1382static int
1383new_channel_path(int chpid)
1384{
1385 struct channel_path *chp;
1386 int ret;
1387
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001388 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!chp)
1390 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* fill in status, etc. */
1393 chp->id = chpid;
1394 chp->state = 1;
1395 chp->dev = (struct device) {
Cornelia Hucka28c6942006-01-06 00:19:23 -08001396 .parent = &css[0]->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 .release = chp_release,
1398 };
1399 snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
1400
1401 /* Obtain channel path description and fill it in. */
1402 ret = chsc_determine_channel_path_description(chpid, &chp->desc);
1403 if (ret)
1404 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001405 /* Get channel-measurement characteristics. */
1406 if (css_characteristics_avail && css_chsc_characteristics.scmc
1407 && css_chsc_characteristics.secm) {
1408 ret = chsc_get_channel_measurement_chars(chp);
1409 if (ret)
1410 goto out_free;
1411 } else {
1412 static int msg_done;
1413
1414 if (!msg_done) {
1415 printk(KERN_WARNING "cio: Channel measurements not "
1416 "available, continuing.\n");
1417 msg_done = 1;
1418 }
1419 chp->cmg = -1;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* make it known to the system */
1423 ret = device_register(&chp->dev);
1424 if (ret) {
1425 printk(KERN_WARNING "%s: could not register %02x\n",
1426 __func__, chpid);
1427 goto out_free;
1428 }
1429 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
1430 if (ret) {
1431 device_unregister(&chp->dev);
1432 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001433 }
1434 mutex_lock(&css[0]->mutex);
1435 if (css[0]->cm_enabled) {
1436 ret = chsc_add_chp_cmg_attr(chp);
1437 if (ret) {
1438 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
1439 device_unregister(&chp->dev);
1440 mutex_unlock(&css[0]->mutex);
1441 goto out_free;
1442 }
1443 }
1444 css[0]->chps[chpid] = chp;
1445 mutex_unlock(&css[0]->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return ret;
1447out_free:
1448 kfree(chp);
1449 return ret;
1450}
1451
1452void *
1453chsc_get_chp_desc(struct subchannel *sch, int chp_no)
1454{
1455 struct channel_path *chp;
1456 struct channel_path_desc *desc;
1457
Cornelia Hucka28c6942006-01-06 00:19:23 -08001458 chp = css[0]->chps[sch->schib.pmcw.chpid[chp_no]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (!chp)
1460 return NULL;
1461 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
1462 if (!desc)
1463 return NULL;
1464 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
1465 return desc;
1466}
1467
1468
1469static int __init
1470chsc_alloc_sei_area(void)
1471{
1472 sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1473 if (!sei_page)
1474 printk(KERN_WARNING"Can't allocate page for processing of " \
1475 "chsc machine checks!\n");
1476 return (sei_page ? 0 : -ENOMEM);
1477}
1478
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001479int __init
1480chsc_enable_facility(int operation_code)
1481{
1482 int ret;
1483 struct {
1484 struct chsc_header request;
1485 u8 reserved1:4;
1486 u8 format:4;
1487 u8 reserved2;
1488 u16 operation_code;
1489 u32 reserved3;
1490 u32 reserved4;
1491 u32 operation_data_area[252];
1492 struct chsc_header response;
1493 u32 reserved5:4;
1494 u32 format2:4;
1495 u32 reserved6:24;
1496 } *sda_area;
1497
1498 sda_area = (void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
1499 if (!sda_area)
1500 return -ENOMEM;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001501 sda_area->request.length = 0x0400;
1502 sda_area->request.code = 0x0031;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001503 sda_area->operation_code = operation_code;
1504
1505 ret = chsc(sda_area);
1506 if (ret > 0) {
1507 ret = (ret == 3) ? -ENODEV : -EBUSY;
1508 goto out;
1509 }
1510 switch (sda_area->response.code) {
Cornelia Huck15730dd2006-03-06 15:43:02 -08001511 case 0x0001: /* everything ok */
1512 ret = 0;
1513 break;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001514 case 0x0003: /* invalid request block */
1515 case 0x0007:
1516 ret = -EINVAL;
1517 break;
1518 case 0x0004: /* command not provided */
1519 case 0x0101: /* facility not provided */
1520 ret = -EOPNOTSUPP;
1521 break;
Cornelia Huck15730dd2006-03-06 15:43:02 -08001522 default: /* something went wrong */
1523 ret = -EIO;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001524 }
1525 out:
1526 free_page((unsigned long)sda_area);
1527 return ret;
1528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530subsys_initcall(chsc_alloc_sei_area);
1531
1532struct css_general_char css_general_characteristics;
1533struct css_chsc_char css_chsc_characteristics;
1534
1535int __init
1536chsc_determine_css_characteristics(void)
1537{
1538 int result;
1539 struct {
1540 struct chsc_header request;
1541 u32 reserved1;
1542 u32 reserved2;
1543 u32 reserved3;
1544 struct chsc_header response;
1545 u32 reserved4;
1546 u32 general_char[510];
1547 u32 chsc_char[518];
1548 } *scsc_area;
1549
1550 scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1551 if (!scsc_area) {
1552 printk(KERN_WARNING"cio: Was not able to determine available" \
1553 "CHSCs due to no memory.\n");
1554 return -ENOMEM;
1555 }
1556
Cornelia Huck495a5b42006-03-24 03:15:14 -08001557 scsc_area->request.length = 0x0010;
1558 scsc_area->request.code = 0x0010;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 result = chsc(scsc_area);
1561 if (result) {
1562 printk(KERN_WARNING"cio: Was not able to determine " \
1563 "available CHSCs, cc=%i.\n", result);
1564 result = -EIO;
1565 goto exit;
1566 }
1567
1568 if (scsc_area->response.code != 1) {
1569 printk(KERN_WARNING"cio: Was not able to determine " \
1570 "available CHSCs.\n");
1571 result = -EIO;
1572 goto exit;
1573 }
1574 memcpy(&css_general_characteristics, scsc_area->general_char,
1575 sizeof(css_general_characteristics));
1576 memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
1577 sizeof(css_chsc_characteristics));
1578exit:
1579 free_page ((unsigned long) scsc_area);
1580 return result;
1581}
1582
1583EXPORT_SYMBOL_GPL(css_general_characteristics);
1584EXPORT_SYMBOL_GPL(css_chsc_characteristics);