blob: 07c7f19339d2228db33f77e6a2adfe89015433e0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/init.h>
15#include <linux/device.h>
16
17#include <asm/cio.h>
18
19#include "css.h"
20#include "cio.h"
21#include "cio_debug.h"
22#include "ioasm.h"
23#include "chsc.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static void *sei_page;
26
27static int new_channel_path(int chpid);
28
29static inline void
30set_chp_logically_online(int chp, int onoff)
31{
Cornelia Hucka28c6942006-01-06 00:19:23 -080032 css[0]->chps[chp]->state = onoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
35static int
36get_chp_status(int chp)
37{
Cornelia Hucka28c6942006-01-06 00:19:23 -080038 return (css[0]->chps[chp] ? css[0]->chps[chp]->state : -ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41void
42chsc_validate_chpids(struct subchannel *sch)
43{
44 int mask, chp;
45
46 for (chp = 0; chp <= 7; chp++) {
47 mask = 0x80 >> chp;
48 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
49 /* disable using this path */
50 sch->opm &= ~mask;
51 }
52}
53
54void
55chpid_is_actually_online(int chp)
56{
57 int state;
58
59 state = get_chp_status(chp);
60 if (state < 0) {
61 need_rescan = 1;
62 queue_work(slow_path_wq, &slow_path_work);
63 } else
64 WARN_ON(!state);
65}
66
67/* FIXME: this is _always_ called for every subchannel. shouldn't we
68 * process more than one at a time? */
69static int
70chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
71{
72 int ccode, j;
73
74 struct {
75 struct chsc_header request;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080076 u16 reserved1a:10;
77 u16 ssid:2;
78 u16 reserved1b:4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 u16 f_sch; /* first subchannel */
80 u16 reserved2;
81 u16 l_sch; /* last subchannel */
82 u32 reserved3;
83 struct chsc_header response;
84 u32 reserved4;
85 u8 sch_valid : 1;
86 u8 dev_valid : 1;
87 u8 st : 3; /* subchannel type */
88 u8 zeroes : 3;
89 u8 unit_addr; /* unit address */
90 u16 devno; /* device number */
91 u8 path_mask;
92 u8 fla_valid_mask;
93 u16 sch; /* subchannel */
94 u8 chpid[8]; /* chpids 0-7 */
95 u16 fla[8]; /* full link addresses 0-7 */
96 } *ssd_area;
97
98 ssd_area = page;
99
Cornelia Huck495a5b42006-03-24 03:15:14 -0800100 ssd_area->request.length = 0x0010;
101 ssd_area->request.code = 0x0004;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800103 ssd_area->ssid = sch->schid.ssid;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800104 ssd_area->f_sch = sch->schid.sch_no;
105 ssd_area->l_sch = sch->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 ccode = chsc(ssd_area);
108 if (ccode > 0) {
109 pr_debug("chsc returned with ccode = %d\n", ccode);
110 return (ccode == 3) ? -ENODEV : -EBUSY;
111 }
112
113 switch (ssd_area->response.code) {
114 case 0x0001: /* everything ok */
115 break;
116 case 0x0002:
117 CIO_CRW_EVENT(2, "Invalid command!\n");
118 return -EINVAL;
119 case 0x0003:
120 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
121 return -EINVAL;
122 case 0x0004:
123 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
124 return -EOPNOTSUPP;
125 default:
126 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
127 ssd_area->response.code);
128 return -EIO;
129 }
130
131 /*
132 * ssd_area->st stores the type of the detected
133 * subchannel, with the following definitions:
134 *
135 * 0: I/O subchannel: All fields have meaning
136 * 1: CHSC subchannel: Only sch_val, st and sch
137 * have meaning
138 * 2: Message subchannel: All fields except unit_addr
139 * have meaning
140 * 3: ADM subchannel: Only sch_val, st and sch
141 * have meaning
142 *
143 * Other types are currently undefined.
144 */
145 if (ssd_area->st > 3) { /* uhm, that looks strange... */
146 CIO_CRW_EVENT(0, "Strange subchannel type %d"
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800147 " for sch 0.%x.%04x\n", ssd_area->st,
148 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * There may have been a new subchannel type defined in the
151 * time since this code was written; since we don't know which
152 * fields have meaning and what to do with it we just jump out
153 */
154 return 0;
155 } else {
156 const char *type[4] = {"I/O", "chsc", "message", "ADM"};
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800157 CIO_CRW_EVENT(6, "ssd: sch 0.%x.%04x is %s subchannel\n",
158 sch->schid.ssid, sch->schid.sch_no,
159 type[ssd_area->st]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 sch->ssd_info.valid = 1;
162 sch->ssd_info.type = ssd_area->st;
163 }
164
165 if (ssd_area->st == 0 || ssd_area->st == 2) {
166 for (j = 0; j < 8; j++) {
167 if (!((0x80 >> j) & ssd_area->path_mask &
168 ssd_area->fla_valid_mask))
169 continue;
170 sch->ssd_info.chpid[j] = ssd_area->chpid[j];
171 sch->ssd_info.fla[j] = ssd_area->fla[j];
172 }
173 }
174 return 0;
175}
176
177int
178css_get_ssd_info(struct subchannel *sch)
179{
180 int ret;
181 void *page;
182
183 page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
184 if (!page)
185 return -ENOMEM;
186 spin_lock_irq(&sch->lock);
187 ret = chsc_get_sch_desc_irq(sch, page);
188 if (ret) {
189 static int cio_chsc_err_msg;
190
191 if (!cio_chsc_err_msg) {
192 printk(KERN_ERR
193 "chsc_get_sch_descriptions:"
194 " Error %d while doing chsc; "
195 "processing some machine checks may "
196 "not work\n", ret);
197 cio_chsc_err_msg = 1;
198 }
199 }
200 spin_unlock_irq(&sch->lock);
201 free_page((unsigned long)page);
202 if (!ret) {
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200203 int j, chpid, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Allocate channel path structures, if needed. */
205 for (j = 0; j < 8; j++) {
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200206 mask = 0x80 >> j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 chpid = sch->ssd_info.chpid[j];
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200208 if ((sch->schib.pmcw.pim & mask) &&
209 (get_chp_status(chpid) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 new_channel_path(chpid);
211 }
212 }
213 return ret;
214}
215
216static int
217s390_subchannel_remove_chpid(struct device *dev, void *data)
218{
219 int j;
220 int mask;
221 struct subchannel *sch;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800222 struct channel_path *chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct schib schib;
224
225 sch = to_subchannel(dev);
226 chpid = data;
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200227 for (j = 0; j < 8; j++) {
228 mask = 0x80 >> j;
229 if ((sch->schib.pmcw.pim & mask) &&
230 (sch->schib.pmcw.chpid[j] == chpid->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (j >= 8)
234 return 0;
235
Cornelia Huckc48d8652006-02-11 17:55:57 -0800236 spin_lock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800238 stsch(sch->schid, &schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!schib.pmcw.dnv)
240 goto out_unreg;
241 memcpy(&sch->schib, &schib, sizeof(struct schib));
242 /* Check for single path devices. */
243 if (sch->schib.pmcw.pim == 0x80)
244 goto out_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Peter Oberparleiter329b7852006-04-27 18:40:02 -0700246 if ((sch->schib.scsw.actl & SCSW_ACTL_DEVACT) &&
247 (sch->schib.scsw.actl & SCSW_ACTL_SCHACT) &&
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200248 (sch->schib.pmcw.lpum == mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int cc;
250
251 cc = cio_clear(sch);
252 if (cc == -ENODEV)
253 goto out_unreg;
254 /* Call handler. */
255 if (sch->driver && sch->driver->termination)
256 sch->driver->termination(&sch->dev);
257 goto out_unlock;
258 }
259
260 /* trigger path verification. */
261 if (sch->driver && sch->driver->verify)
262 sch->driver->verify(&sch->dev);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200263 else if (sch->lpm == mask)
Peter Oberparleiter3b885082006-08-30 14:33:37 +0200264 goto out_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265out_unlock:
Cornelia Huckc48d8652006-02-11 17:55:57 -0800266 spin_unlock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 0;
268out_unreg:
Cornelia Huckc48d8652006-02-11 17:55:57 -0800269 spin_unlock_irq(&sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 sch->lpm = 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800271 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 css_clear_subchannel_slow_list();
273 need_rescan = 1;
274 }
275 return 0;
276}
277
278static inline void
279s390_set_chpid_offline( __u8 chpid)
280{
281 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800282 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 sprintf(dbf_txt, "chpr%x", chpid);
285 CIO_TRACE_EVENT(2, dbf_txt);
286
287 if (get_chp_status(chpid) <= 0)
288 return;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800289 dev = get_device(&css[0]->chps[chpid]->dev);
290 bus_for_each_dev(&css_bus_type, NULL, to_channelpath(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 s390_subchannel_remove_chpid);
292
293 if (need_rescan || css_slow_subchannels_exist())
294 queue_work(slow_path_wq, &slow_path_work);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800295 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800298struct res_acc_data {
299 struct channel_path *chp;
300 u32 fla_mask;
301 u16 fla;
302};
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800305s390_process_res_acc_sch(struct res_acc_data *res_data, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int found;
308 int chp;
309 int ccode;
310
311 found = 0;
312 for (chp = 0; chp <= 7; chp++)
313 /*
314 * check if chpid is in information updated by ssd
315 */
316 if (sch->ssd_info.valid &&
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800317 sch->ssd_info.chpid[chp] == res_data->chp->id &&
318 (sch->ssd_info.fla[chp] & res_data->fla_mask)
319 == res_data->fla) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 found = 1;
321 break;
322 }
323
324 if (found == 0)
325 return 0;
326
327 /*
328 * Do a stsch to update our subchannel structure with the
329 * new path information and eventually check for logically
330 * offline chpids.
331 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800332 ccode = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (ccode > 0)
334 return 0;
335
336 return 0x80 >> chp;
337}
338
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800339static inline int
340s390_process_res_acc_new_sch(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800342 struct schib schib;
343 int ret;
344 /*
345 * We don't know the device yet, but since a path
346 * may be available now to the device we'll have
347 * to do recognition again.
348 * Since we don't have any idea about which chpid
349 * that beast may be on we'll have to do a stsch
350 * on all devices, grr...
351 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800352 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800353 /* We're through */
354 return need_rescan ? -EAGAIN : -ENXIO;
355
356 /* Put it on the slow path. */
357 ret = css_enqueue_subchannel_slow(schid);
358 if (ret) {
359 css_clear_subchannel_slow_list();
360 need_rescan = 1;
361 return -EAGAIN;
362 }
363 return 0;
364}
365
366static int
367__s390_process_res_acc(struct subchannel_id schid, void *data)
368{
369 int chp_mask, old_lpm;
370 struct res_acc_data *res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct subchannel *sch;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800372
373 res_data = (struct res_acc_data *)data;
374 sch = get_subchannel_by_schid(schid);
375 if (!sch)
376 /* Check if a subchannel is newly available. */
377 return s390_process_res_acc_new_sch(schid);
378
379 spin_lock_irq(&sch->lock);
380
381 chp_mask = s390_process_res_acc_sch(res_data, sch);
382
383 if (chp_mask == 0) {
384 spin_unlock_irq(&sch->lock);
Peter Oberparleiterdd9963f2006-09-20 15:59:54 +0200385 put_device(&sch->dev);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800386 return 0;
387 }
388 old_lpm = sch->lpm;
389 sch->lpm = ((sch->schib.pmcw.pim &
390 sch->schib.pmcw.pam &
391 sch->schib.pmcw.pom)
392 | chp_mask) & sch->opm;
393 if (!old_lpm && sch->lpm)
394 device_trigger_reprobe(sch);
395 else if (sch->driver && sch->driver->verify)
396 sch->driver->verify(&sch->dev);
397
398 spin_unlock_irq(&sch->lock);
399 put_device(&sch->dev);
Peter Oberparleiterdd9963f2006-09-20 15:59:54 +0200400 return 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800401}
402
403
404static int
405s390_process_res_acc (struct res_acc_data *res_data)
406{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800407 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 char dbf_txt[15];
409
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800410 sprintf(dbf_txt, "accpr%x", res_data->chp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 CIO_TRACE_EVENT( 2, dbf_txt);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800412 if (res_data->fla != 0) {
413 sprintf(dbf_txt, "fla%x", res_data->fla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 CIO_TRACE_EVENT( 2, dbf_txt);
415 }
416
417 /*
418 * I/O resources may have become accessible.
419 * Scan through all subchannels that may be concerned and
420 * do a validation on those.
421 * The more information we have (info), the less scanning
422 * will we have to do.
423 */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800424 rc = for_each_subchannel(__s390_process_res_acc, res_data);
425 if (css_slow_subchannels_exist())
426 rc = -EAGAIN;
427 else if (rc != -EAGAIN)
428 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return rc;
430}
431
432static int
433__get_chpid_from_lir(void *data)
434{
435 struct lir {
436 u8 iq;
437 u8 ic;
438 u16 sci;
439 /* incident-node descriptor */
440 u32 indesc[28];
441 /* attached-node descriptor */
442 u32 andesc[28];
443 /* incident-specific information */
444 u32 isinfo[28];
445 } *lir;
446
447 lir = (struct lir*) data;
448 if (!(lir->iq&0x80))
449 /* NULL link incident record */
450 return -EINVAL;
451 if (!(lir->indesc[0]&0xc0000000))
452 /* node descriptor not valid */
453 return -EINVAL;
454 if (!(lir->indesc[0]&0x10000000))
455 /* don't handle device-type nodes - FIXME */
456 return -EINVAL;
457 /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
458
459 return (u16) (lir->indesc[0]&0x000000ff);
460}
461
462int
463chsc_process_crw(void)
464{
465 int chpid, ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800466 struct res_acc_data res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct {
468 struct chsc_header request;
469 u32 reserved1;
470 u32 reserved2;
471 u32 reserved3;
472 struct chsc_header response;
473 u32 reserved4;
474 u8 flags;
475 u8 vf; /* validity flags */
476 u8 rs; /* reporting source */
477 u8 cc; /* content code */
478 u16 fla; /* full link address */
479 u16 rsid; /* reporting source id */
480 u32 reserved5;
481 u32 reserved6;
482 u32 ccdf[96]; /* content-code dependent field */
483 /* ccdf has to be big enough for a link-incident record */
484 } *sei_area;
485
486 if (!sei_page)
487 return 0;
488 /*
489 * build the chsc request block for store event information
490 * and do the call
491 * This function is only called by the machine check handler thread,
492 * so we don't need locking for the sei_page.
493 */
494 sei_area = sei_page;
495
496 CIO_TRACE_EVENT( 2, "prcss");
497 ret = 0;
498 do {
499 int ccode, status;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800500 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 memset(sei_area, 0, sizeof(*sei_area));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800502 memset(&res_data, 0, sizeof(struct res_acc_data));
Cornelia Huck495a5b42006-03-24 03:15:14 -0800503 sei_area->request.length = 0x0010;
504 sei_area->request.code = 0x000e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 ccode = chsc(sei_area);
507 if (ccode > 0)
508 return 0;
509
510 switch (sei_area->response.code) {
511 /* for debug purposes, check for problems */
512 case 0x0001:
513 CIO_CRW_EVENT(4, "chsc_process_crw: event information "
514 "successfully stored\n");
515 break; /* everything ok */
516 case 0x0002:
517 CIO_CRW_EVENT(2,
518 "chsc_process_crw: invalid command!\n");
519 return 0;
520 case 0x0003:
521 CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
522 "request block!\n");
523 return 0;
524 case 0x0005:
525 CIO_CRW_EVENT(2, "chsc_process_crw: no event "
526 "information stored\n");
527 return 0;
528 default:
529 CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
530 sei_area->response.code);
531 return 0;
532 }
533
534 /* Check if we might have lost some information. */
535 if (sei_area->flags & 0x40)
536 CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
537 "has been lost due to overflow!\n");
538
539 if (sei_area->rs != 4) {
540 CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
541 "(%04X) isn't a chpid!\n",
542 sei_area->rsid);
543 continue;
544 }
545
546 /* which kind of information was stored? */
547 switch (sei_area->cc) {
548 case 1: /* link incident*/
549 CIO_CRW_EVENT(4, "chsc_process_crw: "
550 "channel subsystem reports link incident,"
551 " reporting source is chpid %x\n",
552 sei_area->rsid);
553 chpid = __get_chpid_from_lir(sei_area->ccdf);
554 if (chpid < 0)
555 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
556 __FUNCTION__);
557 else
558 s390_set_chpid_offline(chpid);
559 break;
560
561 case 2: /* i/o resource accessibiliy */
562 CIO_CRW_EVENT(4, "chsc_process_crw: "
563 "channel subsystem reports some I/O "
564 "devices may have become accessible\n");
565 pr_debug("Data received after sei: \n");
566 pr_debug("Validity flags: %x\n", sei_area->vf);
567
568 /* allocate a new channel path structure, if needed */
569 status = get_chp_status(sei_area->rsid);
570 if (status < 0)
571 new_channel_path(sei_area->rsid);
572 else if (!status)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800573 break;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800574 dev = get_device(&css[0]->chps[sei_area->rsid]->dev);
575 res_data.chp = to_channelpath(dev);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800576 pr_debug("chpid: %x", sei_area->rsid);
577 if ((sei_area->vf & 0xc0) != 0) {
578 res_data.fla = sei_area->fla;
579 if ((sei_area->vf & 0xc0) == 0xc0) {
580 pr_debug(" full link addr: %x",
581 sei_area->fla);
582 res_data.fla_mask = 0xffff;
583 } else {
584 pr_debug(" link addr: %x",
585 sei_area->fla);
586 res_data.fla_mask = 0xff00;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800589 ret = s390_process_res_acc(&res_data);
590 pr_debug("\n\n");
Cornelia Hucka28c6942006-01-06 00:19:23 -0800591 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593
594 default: /* other stuff */
595 CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
596 sei_area->cc);
597 break;
598 }
599 } while (sei_area->flags & 0x80);
600 return ret;
601}
602
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800603static inline int
604__chp_add_new_sch(struct subchannel_id schid)
605{
606 struct schib schib;
607 int ret;
608
609 if (stsch(schid, &schib))
610 /* We're through */
611 return need_rescan ? -EAGAIN : -ENXIO;
612
613 /* Put it on the slow path. */
614 ret = css_enqueue_subchannel_slow(schid);
615 if (ret) {
616 css_clear_subchannel_slow_list();
617 need_rescan = 1;
618 return -EAGAIN;
619 }
620 return 0;
621}
622
623
624static int
625__chp_add(struct subchannel_id schid, void *data)
626{
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200627 int i, mask;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800628 struct channel_path *chp;
629 struct subchannel *sch;
630
631 chp = (struct channel_path *)data;
632 sch = get_subchannel_by_schid(schid);
633 if (!sch)
634 /* Check if the subchannel is now available. */
635 return __chp_add_new_sch(schid);
Cornelia Huckc48d8652006-02-11 17:55:57 -0800636 spin_lock_irq(&sch->lock);
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200637 for (i=0; i<8; i++) {
638 mask = 0x80 >> i;
639 if ((sch->schib.pmcw.pim & mask) &&
640 (sch->schib.pmcw.chpid[i] == chp->id)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800641 if (stsch(sch->schid, &sch->schib) != 0) {
642 /* Endgame. */
Stefan Bader6dcfca72006-04-27 18:40:04 -0700643 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800644 return -ENXIO;
645 }
646 break;
647 }
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200648 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800649 if (i==8) {
Stefan Bader6dcfca72006-04-27 18:40:04 -0700650 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800651 return 0;
652 }
653 sch->lpm = ((sch->schib.pmcw.pim &
654 sch->schib.pmcw.pam &
655 sch->schib.pmcw.pom)
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200656 | mask) & sch->opm;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800657
658 if (sch->driver && sch->driver->verify)
659 sch->driver->verify(&sch->dev);
660
Cornelia Huckc48d8652006-02-11 17:55:57 -0800661 spin_unlock_irq(&sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800662 put_device(&sch->dev);
663 return 0;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static int
667chp_add(int chpid)
668{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800669 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800671 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!get_chp_status(chpid))
674 return 0; /* no need to do the rest */
675
676 sprintf(dbf_txt, "cadd%x", chpid);
677 CIO_TRACE_EVENT(2, dbf_txt);
678
Cornelia Hucka28c6942006-01-06 00:19:23 -0800679 dev = get_device(&css[0]->chps[chpid]->dev);
680 rc = for_each_subchannel(__chp_add, to_channelpath(dev));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800681 if (css_slow_subchannels_exist())
682 rc = -EAGAIN;
683 if (rc != -EAGAIN)
684 rc = 0;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800685 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return rc;
687}
688
689/*
690 * Handling of crw machine checks with channel path source.
691 */
692int
693chp_process_crw(int chpid, int on)
694{
695 if (on == 0) {
696 /* Path has gone. We use the link incident routine.*/
697 s390_set_chpid_offline(chpid);
698 return 0; /* De-register is async anyway. */
699 }
700 /*
701 * Path has come. Allocate a new channel path structure,
702 * if needed.
703 */
704 if (get_chp_status(chpid) < 0)
705 new_channel_path(chpid);
706 /* Avoid the extra overhead in process_rec_acc. */
707 return chp_add(chpid);
708}
709
710static inline int
711__check_for_io_and_kill(struct subchannel *sch, int index)
712{
713 int cc;
714
715 if (!device_is_online(sch))
716 /* cio could be doing I/O. */
717 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800718 cc = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (cc)
720 return 0;
721 if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index)) {
722 device_set_waiting(sch);
723 return 1;
724 }
725 return 0;
726}
727
728static inline void
729__s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
730{
731 int chp, old_lpm;
732 unsigned long flags;
733
734 if (!sch->ssd_info.valid)
735 return;
736
737 spin_lock_irqsave(&sch->lock, flags);
738 old_lpm = sch->lpm;
739 for (chp = 0; chp < 8; chp++) {
740 if (sch->ssd_info.chpid[chp] != chpid)
741 continue;
742
743 if (on) {
744 sch->opm |= (0x80 >> chp);
745 sch->lpm |= (0x80 >> chp);
746 if (!old_lpm)
747 device_trigger_reprobe(sch);
748 else if (sch->driver && sch->driver->verify)
749 sch->driver->verify(&sch->dev);
750 } else {
751 sch->opm &= ~(0x80 >> chp);
752 sch->lpm &= ~(0x80 >> chp);
753 /*
754 * Give running I/O a grace period in which it
755 * can successfully terminate, even using the
756 * just varied off path. Then kill it.
757 */
758 if (!__check_for_io_and_kill(sch, chp) && !sch->lpm) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800759 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 css_clear_subchannel_slow_list();
761 need_rescan = 1;
762 }
763 } else if (sch->driver && sch->driver->verify)
764 sch->driver->verify(&sch->dev);
765 }
766 break;
767 }
768 spin_unlock_irqrestore(&sch->lock, flags);
769}
770
771static int
772s390_subchannel_vary_chpid_off(struct device *dev, void *data)
773{
774 struct subchannel *sch;
775 __u8 *chpid;
776
777 sch = to_subchannel(dev);
778 chpid = data;
779
780 __s390_subchannel_vary_chpid(sch, *chpid, 0);
781 return 0;
782}
783
784static int
785s390_subchannel_vary_chpid_on(struct device *dev, void *data)
786{
787 struct subchannel *sch;
788 __u8 *chpid;
789
790 sch = to_subchannel(dev);
791 chpid = data;
792
793 __s390_subchannel_vary_chpid(sch, *chpid, 1);
794 return 0;
795}
796
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800797static int
798__s390_vary_chpid_on(struct subchannel_id schid, void *data)
799{
800 struct schib schib;
801 struct subchannel *sch;
802
803 sch = get_subchannel_by_schid(schid);
804 if (sch) {
805 put_device(&sch->dev);
806 return 0;
807 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800808 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800809 /* We're through */
810 return -ENXIO;
811 /* Put it on the slow path. */
812 if (css_enqueue_subchannel_slow(schid)) {
813 css_clear_subchannel_slow_list();
814 need_rescan = 1;
815 return -EAGAIN;
816 }
817 return 0;
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
821 * Function: s390_vary_chpid
822 * Varies the specified chpid online or offline
823 */
824static int
825s390_vary_chpid( __u8 chpid, int on)
826{
827 char dbf_text[15];
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800828 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
831 CIO_TRACE_EVENT( 2, dbf_text);
832
833 status = get_chp_status(chpid);
834 if (status < 0) {
835 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
836 return -EINVAL;
837 }
838
839 if (!on && !status) {
840 printk(KERN_ERR "chpid %x is already offline\n", chpid);
841 return -EINVAL;
842 }
843
844 set_chp_logically_online(chpid, on);
845
846 /*
847 * Redo PathVerification on the devices the chpid connects to
848 */
849
850 bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
851 s390_subchannel_vary_chpid_on :
852 s390_subchannel_vary_chpid_off);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800853 if (on)
854 /* Scan for new devices on varied on path. */
855 for_each_subchannel(__s390_vary_chpid_on, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (need_rescan || css_slow_subchannels_exist())
857 queue_work(slow_path_wq, &slow_path_work);
858 return 0;
859}
860
861/*
Cornelia Huck495a5b42006-03-24 03:15:14 -0800862 * Channel measurement related functions
863 */
864static ssize_t
865chp_measurement_chars_read(struct kobject *kobj, char *buf, loff_t off,
866 size_t count)
867{
868 struct channel_path *chp;
869 unsigned int size;
870
871 chp = to_channelpath(container_of(kobj, struct device, kobj));
872 if (!chp->cmg_chars)
873 return 0;
874
875 size = sizeof(struct cmg_chars);
876
877 if (off > size)
878 return 0;
879 if (off + count > size)
880 count = size - off;
881 memcpy(buf, chp->cmg_chars + off, count);
882 return count;
883}
884
885static struct bin_attribute chp_measurement_chars_attr = {
886 .attr = {
887 .name = "measurement_chars",
888 .mode = S_IRUSR,
889 .owner = THIS_MODULE,
890 },
891 .size = sizeof(struct cmg_chars),
892 .read = chp_measurement_chars_read,
893};
894
895static void
896chp_measurement_copy_block(struct cmg_entry *buf,
897 struct channel_subsystem *css, int chpid)
898{
899 void *area;
900 struct cmg_entry *entry, reference_buf;
901 int idx;
902
903 if (chpid < 128) {
904 area = css->cub_addr1;
905 idx = chpid;
906 } else {
907 area = css->cub_addr2;
908 idx = chpid - 128;
909 }
910 entry = area + (idx * sizeof(struct cmg_entry));
911 do {
912 memcpy(buf, entry, sizeof(*entry));
913 memcpy(&reference_buf, entry, sizeof(*entry));
914 } while (reference_buf.values[0] != buf->values[0]);
915}
916
917static ssize_t
918chp_measurement_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
919{
920 struct channel_path *chp;
921 struct channel_subsystem *css;
922 unsigned int size;
923
924 chp = to_channelpath(container_of(kobj, struct device, kobj));
925 css = to_css(chp->dev.parent);
926
Cornelia Huck231caa12006-06-29 14:56:45 +0200927 size = sizeof(struct cmg_entry);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800928
929 /* Only allow single reads. */
930 if (off || count < size)
931 return 0;
932 chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->id);
Cornelia Huck231caa12006-06-29 14:56:45 +0200933 count = size;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800934 return count;
935}
936
937static struct bin_attribute chp_measurement_attr = {
938 .attr = {
939 .name = "measurement",
940 .mode = S_IRUSR,
941 .owner = THIS_MODULE,
942 },
943 .size = sizeof(struct cmg_entry),
944 .read = chp_measurement_read,
945};
946
947static void
948chsc_remove_chp_cmg_attr(struct channel_path *chp)
949{
950 sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_chars_attr);
951 sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_attr);
952}
953
954static int
955chsc_add_chp_cmg_attr(struct channel_path *chp)
956{
957 int ret;
958
959 ret = sysfs_create_bin_file(&chp->dev.kobj,
960 &chp_measurement_chars_attr);
961 if (ret)
962 return ret;
963 ret = sysfs_create_bin_file(&chp->dev.kobj, &chp_measurement_attr);
964 if (ret)
965 sysfs_remove_bin_file(&chp->dev.kobj,
966 &chp_measurement_chars_attr);
967 return ret;
968}
969
970static void
971chsc_remove_cmg_attr(struct channel_subsystem *css)
972{
973 int i;
974
975 for (i = 0; i <= __MAX_CHPID; i++) {
976 if (!css->chps[i])
977 continue;
978 chsc_remove_chp_cmg_attr(css->chps[i]);
979 }
980}
981
982static int
983chsc_add_cmg_attr(struct channel_subsystem *css)
984{
985 int i, ret;
986
987 ret = 0;
988 for (i = 0; i <= __MAX_CHPID; i++) {
989 if (!css->chps[i])
990 continue;
991 ret = chsc_add_chp_cmg_attr(css->chps[i]);
992 if (ret)
993 goto cleanup;
994 }
995 return ret;
996cleanup:
997 for (--i; i >= 0; i--) {
998 if (!css->chps[i])
999 continue;
1000 chsc_remove_chp_cmg_attr(css->chps[i]);
1001 }
1002 return ret;
1003}
1004
1005
1006static int
1007__chsc_do_secm(struct channel_subsystem *css, int enable, void *page)
1008{
1009 struct {
1010 struct chsc_header request;
1011 u32 operation_code : 2;
1012 u32 : 30;
1013 u32 key : 4;
1014 u32 : 28;
1015 u32 zeroes1;
1016 u32 cub_addr1;
1017 u32 zeroes2;
1018 u32 cub_addr2;
1019 u32 reserved[13];
1020 struct chsc_header response;
1021 u32 status : 8;
1022 u32 : 4;
1023 u32 fmt : 4;
1024 u32 : 16;
1025 } *secm_area;
1026 int ret, ccode;
1027
1028 secm_area = page;
1029 secm_area->request.length = 0x0050;
1030 secm_area->request.code = 0x0016;
1031
1032 secm_area->key = PAGE_DEFAULT_KEY;
1033 secm_area->cub_addr1 = (u64)(unsigned long)css->cub_addr1;
1034 secm_area->cub_addr2 = (u64)(unsigned long)css->cub_addr2;
1035
1036 secm_area->operation_code = enable ? 0 : 1;
1037
1038 ccode = chsc(secm_area);
1039 if (ccode > 0)
1040 return (ccode == 3) ? -ENODEV : -EBUSY;
1041
1042 switch (secm_area->response.code) {
1043 case 0x0001: /* Success. */
1044 ret = 0;
1045 break;
1046 case 0x0003: /* Invalid block. */
1047 case 0x0007: /* Invalid format. */
1048 case 0x0008: /* Other invalid block. */
1049 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1050 ret = -EINVAL;
1051 break;
1052 case 0x0004: /* Command not provided in model. */
1053 CIO_CRW_EVENT(2, "Model does not provide secm\n");
1054 ret = -EOPNOTSUPP;
1055 break;
1056 case 0x0102: /* cub adresses incorrect */
1057 CIO_CRW_EVENT(2, "Invalid addresses in chsc request block\n");
1058 ret = -EINVAL;
1059 break;
1060 case 0x0103: /* key error */
1061 CIO_CRW_EVENT(2, "Access key error in secm\n");
1062 ret = -EINVAL;
1063 break;
1064 case 0x0105: /* error while starting */
1065 CIO_CRW_EVENT(2, "Error while starting channel measurement\n");
1066 ret = -EIO;
1067 break;
1068 default:
1069 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1070 secm_area->response.code);
1071 ret = -EIO;
1072 }
1073 return ret;
1074}
1075
1076int
1077chsc_secm(struct channel_subsystem *css, int enable)
1078{
1079 void *secm_area;
1080 int ret;
1081
1082 secm_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1083 if (!secm_area)
1084 return -ENOMEM;
1085
1086 mutex_lock(&css->mutex);
1087 if (enable && !css->cm_enabled) {
1088 css->cub_addr1 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1089 css->cub_addr2 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1090 if (!css->cub_addr1 || !css->cub_addr2) {
1091 free_page((unsigned long)css->cub_addr1);
1092 free_page((unsigned long)css->cub_addr2);
1093 free_page((unsigned long)secm_area);
1094 mutex_unlock(&css->mutex);
1095 return -ENOMEM;
1096 }
1097 }
1098 ret = __chsc_do_secm(css, enable, secm_area);
1099 if (!ret) {
1100 css->cm_enabled = enable;
1101 if (css->cm_enabled) {
1102 ret = chsc_add_cmg_attr(css);
1103 if (ret) {
1104 memset(secm_area, 0, PAGE_SIZE);
1105 __chsc_do_secm(css, 0, secm_area);
1106 css->cm_enabled = 0;
1107 }
1108 } else
1109 chsc_remove_cmg_attr(css);
1110 }
1111 if (enable && !css->cm_enabled) {
1112 free_page((unsigned long)css->cub_addr1);
1113 free_page((unsigned long)css->cub_addr2);
1114 }
1115 mutex_unlock(&css->mutex);
1116 free_page((unsigned long)secm_area);
1117 return ret;
1118}
1119
1120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 * Files for the channel path entries.
1122 */
1123static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001124chp_status_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1127
1128 if (!chp)
1129 return 0;
1130 return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
1131 sprintf(buf, "offline\n"));
1132}
1133
1134static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001135chp_status_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct channel_path *cp = container_of(dev, struct channel_path, dev);
1138 char cmd[10];
1139 int num_args;
1140 int error;
1141
1142 num_args = sscanf(buf, "%5s", cmd);
1143 if (!num_args)
1144 return count;
1145
1146 if (!strnicmp(cmd, "on", 2))
1147 error = s390_vary_chpid(cp->id, 1);
1148 else if (!strnicmp(cmd, "off", 3))
1149 error = s390_vary_chpid(cp->id, 0);
1150 else
1151 error = -EINVAL;
1152
1153 return error < 0 ? error : count;
1154
1155}
1156
1157static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
1158
1159static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001160chp_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1163
1164 if (!chp)
1165 return 0;
1166 return sprintf(buf, "%x\n", chp->desc.desc);
1167}
1168
1169static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
1170
Cornelia Huck495a5b42006-03-24 03:15:14 -08001171static ssize_t
1172chp_cmg_show(struct device *dev, struct device_attribute *attr, char *buf)
1173{
1174 struct channel_path *chp = to_channelpath(dev);
1175
1176 if (!chp)
1177 return 0;
1178 if (chp->cmg == -1) /* channel measurements not available */
1179 return sprintf(buf, "unknown\n");
1180 return sprintf(buf, "%x\n", chp->cmg);
1181}
1182
1183static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
1184
1185static ssize_t
1186chp_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
1187{
1188 struct channel_path *chp = to_channelpath(dev);
1189
1190 if (!chp)
1191 return 0;
1192 if (chp->shared == -1) /* channel measurements not available */
1193 return sprintf(buf, "unknown\n");
1194 return sprintf(buf, "%x\n", chp->shared);
1195}
1196
1197static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static struct attribute * chp_attrs[] = {
1200 &dev_attr_status.attr,
1201 &dev_attr_type.attr,
Cornelia Huck495a5b42006-03-24 03:15:14 -08001202 &dev_attr_cmg.attr,
1203 &dev_attr_shared.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 NULL,
1205};
1206
1207static struct attribute_group chp_attr_group = {
1208 .attrs = chp_attrs,
1209};
1210
1211static void
1212chp_release(struct device *dev)
1213{
1214 struct channel_path *cp;
1215
1216 cp = container_of(dev, struct channel_path, dev);
1217 kfree(cp);
1218}
1219
1220static int
1221chsc_determine_channel_path_description(int chpid,
1222 struct channel_path_desc *desc)
1223{
1224 int ccode, ret;
1225
1226 struct {
1227 struct chsc_header request;
1228 u32 : 24;
1229 u32 first_chpid : 8;
1230 u32 : 24;
1231 u32 last_chpid : 8;
1232 u32 zeroes1;
1233 struct chsc_header response;
1234 u32 zeroes2;
1235 struct channel_path_desc desc;
1236 } *scpd_area;
1237
1238 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1239 if (!scpd_area)
1240 return -ENOMEM;
1241
Cornelia Huck495a5b42006-03-24 03:15:14 -08001242 scpd_area->request.length = 0x0010;
1243 scpd_area->request.code = 0x0002;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 scpd_area->first_chpid = chpid;
1246 scpd_area->last_chpid = chpid;
1247
1248 ccode = chsc(scpd_area);
1249 if (ccode > 0) {
1250 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1251 goto out;
1252 }
1253
1254 switch (scpd_area->response.code) {
1255 case 0x0001: /* Success. */
1256 memcpy(desc, &scpd_area->desc,
1257 sizeof(struct channel_path_desc));
1258 ret = 0;
1259 break;
1260 case 0x0003: /* Invalid block. */
1261 case 0x0007: /* Invalid format. */
1262 case 0x0008: /* Other invalid block. */
1263 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1264 ret = -EINVAL;
1265 break;
1266 case 0x0004: /* Command not provided in model. */
1267 CIO_CRW_EVENT(2, "Model does not provide scpd\n");
1268 ret = -EOPNOTSUPP;
1269 break;
1270 default:
1271 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1272 scpd_area->response.code);
1273 ret = -EIO;
1274 }
1275out:
1276 free_page((unsigned long)scpd_area);
1277 return ret;
1278}
1279
Cornelia Huck495a5b42006-03-24 03:15:14 -08001280static void
1281chsc_initialize_cmg_chars(struct channel_path *chp, u8 cmcv,
1282 struct cmg_chars *chars)
1283{
1284 switch (chp->cmg) {
1285 case 2:
1286 case 3:
1287 chp->cmg_chars = kmalloc(sizeof(struct cmg_chars),
1288 GFP_KERNEL);
1289 if (chp->cmg_chars) {
1290 int i, mask;
1291 struct cmg_chars *cmg_chars;
1292
1293 cmg_chars = chp->cmg_chars;
1294 for (i = 0; i < NR_MEASUREMENT_CHARS; i++) {
1295 mask = 0x80 >> (i + 3);
1296 if (cmcv & mask)
1297 cmg_chars->values[i] = chars->values[i];
1298 else
1299 cmg_chars->values[i] = 0;
1300 }
1301 }
1302 break;
1303 default:
1304 /* No cmg-dependent data. */
1305 break;
1306 }
1307}
1308
1309static int
1310chsc_get_channel_measurement_chars(struct channel_path *chp)
1311{
1312 int ccode, ret;
1313
1314 struct {
1315 struct chsc_header request;
1316 u32 : 24;
1317 u32 first_chpid : 8;
1318 u32 : 24;
1319 u32 last_chpid : 8;
1320 u32 zeroes1;
1321 struct chsc_header response;
1322 u32 zeroes2;
1323 u32 not_valid : 1;
1324 u32 shared : 1;
1325 u32 : 22;
1326 u32 chpid : 8;
1327 u32 cmcv : 5;
1328 u32 : 11;
1329 u32 cmgq : 8;
1330 u32 cmg : 8;
1331 u32 zeroes3;
1332 u32 data[NR_MEASUREMENT_CHARS];
1333 } *scmc_area;
1334
1335 scmc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1336 if (!scmc_area)
1337 return -ENOMEM;
1338
1339 scmc_area->request.length = 0x0010;
1340 scmc_area->request.code = 0x0022;
1341
1342 scmc_area->first_chpid = chp->id;
1343 scmc_area->last_chpid = chp->id;
1344
1345 ccode = chsc(scmc_area);
1346 if (ccode > 0) {
1347 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1348 goto out;
1349 }
1350
1351 switch (scmc_area->response.code) {
1352 case 0x0001: /* Success. */
1353 if (!scmc_area->not_valid) {
1354 chp->cmg = scmc_area->cmg;
1355 chp->shared = scmc_area->shared;
1356 chsc_initialize_cmg_chars(chp, scmc_area->cmcv,
1357 (struct cmg_chars *)
1358 &scmc_area->data);
1359 } else {
1360 chp->cmg = -1;
1361 chp->shared = -1;
1362 }
1363 ret = 0;
1364 break;
1365 case 0x0003: /* Invalid block. */
1366 case 0x0007: /* Invalid format. */
1367 case 0x0008: /* Invalid bit combination. */
1368 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1369 ret = -EINVAL;
1370 break;
1371 case 0x0004: /* Command not provided. */
1372 CIO_CRW_EVENT(2, "Model does not provide scmc\n");
1373 ret = -EOPNOTSUPP;
1374 break;
1375 default:
1376 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1377 scmc_area->response.code);
1378 ret = -EIO;
1379 }
1380out:
1381 free_page((unsigned long)scmc_area);
1382 return ret;
1383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*
1386 * Entries for chpids on the system bus.
1387 * This replaces /proc/chpids.
1388 */
1389static int
1390new_channel_path(int chpid)
1391{
1392 struct channel_path *chp;
1393 int ret;
1394
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001395 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (!chp)
1397 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 /* fill in status, etc. */
1400 chp->id = chpid;
1401 chp->state = 1;
Heiko Carstens292888c2006-08-30 14:33:35 +02001402 chp->dev.parent = &css[0]->device;
1403 chp->dev.release = chp_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
1405
1406 /* Obtain channel path description and fill it in. */
1407 ret = chsc_determine_channel_path_description(chpid, &chp->desc);
1408 if (ret)
1409 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001410 /* Get channel-measurement characteristics. */
1411 if (css_characteristics_avail && css_chsc_characteristics.scmc
1412 && css_chsc_characteristics.secm) {
1413 ret = chsc_get_channel_measurement_chars(chp);
1414 if (ret)
1415 goto out_free;
1416 } else {
1417 static int msg_done;
1418
1419 if (!msg_done) {
1420 printk(KERN_WARNING "cio: Channel measurements not "
1421 "available, continuing.\n");
1422 msg_done = 1;
1423 }
1424 chp->cmg = -1;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /* make it known to the system */
1428 ret = device_register(&chp->dev);
1429 if (ret) {
1430 printk(KERN_WARNING "%s: could not register %02x\n",
1431 __func__, chpid);
1432 goto out_free;
1433 }
1434 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
1435 if (ret) {
1436 device_unregister(&chp->dev);
1437 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001438 }
1439 mutex_lock(&css[0]->mutex);
1440 if (css[0]->cm_enabled) {
1441 ret = chsc_add_chp_cmg_attr(chp);
1442 if (ret) {
1443 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
1444 device_unregister(&chp->dev);
1445 mutex_unlock(&css[0]->mutex);
1446 goto out_free;
1447 }
1448 }
1449 css[0]->chps[chpid] = chp;
1450 mutex_unlock(&css[0]->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return ret;
1452out_free:
1453 kfree(chp);
1454 return ret;
1455}
1456
1457void *
1458chsc_get_chp_desc(struct subchannel *sch, int chp_no)
1459{
1460 struct channel_path *chp;
1461 struct channel_path_desc *desc;
1462
Cornelia Hucka28c6942006-01-06 00:19:23 -08001463 chp = css[0]->chps[sch->schib.pmcw.chpid[chp_no]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!chp)
1465 return NULL;
1466 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
1467 if (!desc)
1468 return NULL;
1469 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
1470 return desc;
1471}
1472
Cornelia Huck7e560812006-07-12 16:40:19 +02001473static int reset_channel_path(struct channel_path *chp)
1474{
1475 int cc;
1476
1477 cc = rchp(chp->id);
1478 switch (cc) {
1479 case 0:
1480 return 0;
1481 case 2:
1482 return -EBUSY;
1483 default:
1484 return -ENODEV;
1485 }
1486}
1487
1488static void reset_channel_paths_css(struct channel_subsystem *css)
1489{
1490 int i;
1491
1492 for (i = 0; i <= __MAX_CHPID; i++) {
1493 if (css->chps[i])
1494 reset_channel_path(css->chps[i]);
1495 }
1496}
1497
1498void cio_reset_channel_paths(void)
1499{
1500 int i;
1501
1502 for (i = 0; i <= __MAX_CSSID; i++) {
1503 if (css[i] && css[i]->valid)
1504 reset_channel_paths_css(css[i]);
1505 }
1506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508static int __init
1509chsc_alloc_sei_area(void)
1510{
1511 sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1512 if (!sei_page)
1513 printk(KERN_WARNING"Can't allocate page for processing of " \
1514 "chsc machine checks!\n");
1515 return (sei_page ? 0 : -ENOMEM);
1516}
1517
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001518int __init
1519chsc_enable_facility(int operation_code)
1520{
1521 int ret;
1522 struct {
1523 struct chsc_header request;
1524 u8 reserved1:4;
1525 u8 format:4;
1526 u8 reserved2;
1527 u16 operation_code;
1528 u32 reserved3;
1529 u32 reserved4;
1530 u32 operation_data_area[252];
1531 struct chsc_header response;
1532 u32 reserved5:4;
1533 u32 format2:4;
1534 u32 reserved6:24;
1535 } *sda_area;
1536
1537 sda_area = (void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
1538 if (!sda_area)
1539 return -ENOMEM;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001540 sda_area->request.length = 0x0400;
1541 sda_area->request.code = 0x0031;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001542 sda_area->operation_code = operation_code;
1543
1544 ret = chsc(sda_area);
1545 if (ret > 0) {
1546 ret = (ret == 3) ? -ENODEV : -EBUSY;
1547 goto out;
1548 }
1549 switch (sda_area->response.code) {
Cornelia Huck15730dd2006-03-06 15:43:02 -08001550 case 0x0001: /* everything ok */
1551 ret = 0;
1552 break;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001553 case 0x0003: /* invalid request block */
1554 case 0x0007:
1555 ret = -EINVAL;
1556 break;
1557 case 0x0004: /* command not provided */
1558 case 0x0101: /* facility not provided */
1559 ret = -EOPNOTSUPP;
1560 break;
Cornelia Huck15730dd2006-03-06 15:43:02 -08001561 default: /* something went wrong */
1562 ret = -EIO;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001563 }
1564 out:
1565 free_page((unsigned long)sda_area);
1566 return ret;
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569subsys_initcall(chsc_alloc_sei_area);
1570
1571struct css_general_char css_general_characteristics;
1572struct css_chsc_char css_chsc_characteristics;
1573
1574int __init
1575chsc_determine_css_characteristics(void)
1576{
1577 int result;
1578 struct {
1579 struct chsc_header request;
1580 u32 reserved1;
1581 u32 reserved2;
1582 u32 reserved3;
1583 struct chsc_header response;
1584 u32 reserved4;
1585 u32 general_char[510];
1586 u32 chsc_char[518];
1587 } *scsc_area;
1588
1589 scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1590 if (!scsc_area) {
1591 printk(KERN_WARNING"cio: Was not able to determine available" \
1592 "CHSCs due to no memory.\n");
1593 return -ENOMEM;
1594 }
1595
Cornelia Huck495a5b42006-03-24 03:15:14 -08001596 scsc_area->request.length = 0x0010;
1597 scsc_area->request.code = 0x0010;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 result = chsc(scsc_area);
1600 if (result) {
1601 printk(KERN_WARNING"cio: Was not able to determine " \
1602 "available CHSCs, cc=%i.\n", result);
1603 result = -EIO;
1604 goto exit;
1605 }
1606
1607 if (scsc_area->response.code != 1) {
1608 printk(KERN_WARNING"cio: Was not able to determine " \
1609 "available CHSCs.\n");
1610 result = -EIO;
1611 goto exit;
1612 }
1613 memcpy(&css_general_characteristics, scsc_area->general_char,
1614 sizeof(css_general_characteristics));
1615 memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
1616 sizeof(css_chsc_characteristics));
1617exit:
1618 free_page ((unsigned long) scsc_area);
1619 return result;
1620}
1621
1622EXPORT_SYMBOL_GPL(css_general_characteristics);
1623EXPORT_SYMBOL_GPL(css_chsc_characteristics);