blob: 15b0e6329cf3f913dd1689724fa27ab9a9f10162 [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;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100186 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100200 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 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 Huck2ec22982006-12-08 15:54:26 +0100236 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;
Cornelia Huckd23861f2006-12-04 15:41:04 +0100254 /* Request retry of internal operation. */
255 device_set_intretry(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Call handler. */
257 if (sch->driver && sch->driver->termination)
258 sch->driver->termination(&sch->dev);
259 goto out_unlock;
260 }
261
262 /* trigger path verification. */
263 if (sch->driver && sch->driver->verify)
264 sch->driver->verify(&sch->dev);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200265 else if (sch->lpm == mask)
Peter Oberparleiter3b885082006-08-30 14:33:37 +0200266 goto out_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267out_unlock:
Cornelia Huck2ec22982006-12-08 15:54:26 +0100268 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270out_unreg:
Cornelia Huck2ec22982006-12-08 15:54:26 +0100271 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sch->lpm = 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800273 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 css_clear_subchannel_slow_list();
275 need_rescan = 1;
276 }
277 return 0;
278}
279
280static inline void
281s390_set_chpid_offline( __u8 chpid)
282{
283 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800284 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 sprintf(dbf_txt, "chpr%x", chpid);
287 CIO_TRACE_EVENT(2, dbf_txt);
288
289 if (get_chp_status(chpid) <= 0)
290 return;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800291 dev = get_device(&css[0]->chps[chpid]->dev);
292 bus_for_each_dev(&css_bus_type, NULL, to_channelpath(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 s390_subchannel_remove_chpid);
294
295 if (need_rescan || css_slow_subchannels_exist())
296 queue_work(slow_path_wq, &slow_path_work);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800297 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800300struct res_acc_data {
301 struct channel_path *chp;
302 u32 fla_mask;
303 u16 fla;
304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800307s390_process_res_acc_sch(struct res_acc_data *res_data, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 int found;
310 int chp;
311 int ccode;
312
313 found = 0;
314 for (chp = 0; chp <= 7; chp++)
315 /*
316 * check if chpid is in information updated by ssd
317 */
318 if (sch->ssd_info.valid &&
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800319 sch->ssd_info.chpid[chp] == res_data->chp->id &&
320 (sch->ssd_info.fla[chp] & res_data->fla_mask)
321 == res_data->fla) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 found = 1;
323 break;
324 }
325
326 if (found == 0)
327 return 0;
328
329 /*
330 * Do a stsch to update our subchannel structure with the
331 * new path information and eventually check for logically
332 * offline chpids.
333 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800334 ccode = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ccode > 0)
336 return 0;
337
338 return 0x80 >> chp;
339}
340
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800341static inline int
342s390_process_res_acc_new_sch(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800344 struct schib schib;
345 int ret;
346 /*
347 * We don't know the device yet, but since a path
348 * may be available now to the device we'll have
349 * to do recognition again.
350 * Since we don't have any idea about which chpid
351 * that beast may be on we'll have to do a stsch
352 * on all devices, grr...
353 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800354 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800355 /* We're through */
356 return need_rescan ? -EAGAIN : -ENXIO;
357
358 /* Put it on the slow path. */
359 ret = css_enqueue_subchannel_slow(schid);
360 if (ret) {
361 css_clear_subchannel_slow_list();
362 need_rescan = 1;
363 return -EAGAIN;
364 }
365 return 0;
366}
367
368static int
369__s390_process_res_acc(struct subchannel_id schid, void *data)
370{
371 int chp_mask, old_lpm;
372 struct res_acc_data *res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct subchannel *sch;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800374
Cornelia Huck12975ae2006-10-11 15:31:47 +0200375 res_data = data;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800376 sch = get_subchannel_by_schid(schid);
377 if (!sch)
378 /* Check if a subchannel is newly available. */
379 return s390_process_res_acc_new_sch(schid);
380
Cornelia Huck2ec22982006-12-08 15:54:26 +0100381 spin_lock_irq(sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800382
383 chp_mask = s390_process_res_acc_sch(res_data, sch);
384
385 if (chp_mask == 0) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100386 spin_unlock_irq(sch->lock);
Peter Oberparleiterdd9963f2006-09-20 15:59:54 +0200387 put_device(&sch->dev);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800388 return 0;
389 }
390 old_lpm = sch->lpm;
391 sch->lpm = ((sch->schib.pmcw.pim &
392 sch->schib.pmcw.pam &
393 sch->schib.pmcw.pom)
394 | chp_mask) & sch->opm;
395 if (!old_lpm && sch->lpm)
396 device_trigger_reprobe(sch);
397 else if (sch->driver && sch->driver->verify)
398 sch->driver->verify(&sch->dev);
399
Cornelia Huck2ec22982006-12-08 15:54:26 +0100400 spin_unlock_irq(sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800401 put_device(&sch->dev);
Peter Oberparleiterdd9963f2006-09-20 15:59:54 +0200402 return 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800403}
404
405
406static int
407s390_process_res_acc (struct res_acc_data *res_data)
408{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800409 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 char dbf_txt[15];
411
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800412 sprintf(dbf_txt, "accpr%x", res_data->chp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 CIO_TRACE_EVENT( 2, dbf_txt);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800414 if (res_data->fla != 0) {
415 sprintf(dbf_txt, "fla%x", res_data->fla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 CIO_TRACE_EVENT( 2, dbf_txt);
417 }
418
419 /*
420 * I/O resources may have become accessible.
421 * Scan through all subchannels that may be concerned and
422 * do a validation on those.
423 * The more information we have (info), the less scanning
424 * will we have to do.
425 */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800426 rc = for_each_subchannel(__s390_process_res_acc, res_data);
427 if (css_slow_subchannels_exist())
428 rc = -EAGAIN;
429 else if (rc != -EAGAIN)
430 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return rc;
432}
433
434static int
435__get_chpid_from_lir(void *data)
436{
437 struct lir {
438 u8 iq;
439 u8 ic;
440 u16 sci;
441 /* incident-node descriptor */
442 u32 indesc[28];
443 /* attached-node descriptor */
444 u32 andesc[28];
445 /* incident-specific information */
446 u32 isinfo[28];
447 } *lir;
448
Cornelia Huck12975ae2006-10-11 15:31:47 +0200449 lir = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!(lir->iq&0x80))
451 /* NULL link incident record */
452 return -EINVAL;
453 if (!(lir->indesc[0]&0xc0000000))
454 /* node descriptor not valid */
455 return -EINVAL;
456 if (!(lir->indesc[0]&0x10000000))
457 /* don't handle device-type nodes - FIXME */
458 return -EINVAL;
459 /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
460
461 return (u16) (lir->indesc[0]&0x000000ff);
462}
463
464int
465chsc_process_crw(void)
466{
467 int chpid, ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800468 struct res_acc_data res_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct {
470 struct chsc_header request;
471 u32 reserved1;
472 u32 reserved2;
473 u32 reserved3;
474 struct chsc_header response;
475 u32 reserved4;
476 u8 flags;
477 u8 vf; /* validity flags */
478 u8 rs; /* reporting source */
479 u8 cc; /* content code */
480 u16 fla; /* full link address */
481 u16 rsid; /* reporting source id */
482 u32 reserved5;
483 u32 reserved6;
484 u32 ccdf[96]; /* content-code dependent field */
485 /* ccdf has to be big enough for a link-incident record */
486 } *sei_area;
487
488 if (!sei_page)
489 return 0;
490 /*
491 * build the chsc request block for store event information
492 * and do the call
493 * This function is only called by the machine check handler thread,
494 * so we don't need locking for the sei_page.
495 */
496 sei_area = sei_page;
497
498 CIO_TRACE_EVENT( 2, "prcss");
499 ret = 0;
500 do {
501 int ccode, status;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800502 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 memset(sei_area, 0, sizeof(*sei_area));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800504 memset(&res_data, 0, sizeof(struct res_acc_data));
Cornelia Huck495a5b42006-03-24 03:15:14 -0800505 sei_area->request.length = 0x0010;
506 sei_area->request.code = 0x000e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 ccode = chsc(sei_area);
509 if (ccode > 0)
510 return 0;
511
512 switch (sei_area->response.code) {
513 /* for debug purposes, check for problems */
514 case 0x0001:
515 CIO_CRW_EVENT(4, "chsc_process_crw: event information "
516 "successfully stored\n");
517 break; /* everything ok */
518 case 0x0002:
519 CIO_CRW_EVENT(2,
520 "chsc_process_crw: invalid command!\n");
521 return 0;
522 case 0x0003:
523 CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
524 "request block!\n");
525 return 0;
526 case 0x0005:
527 CIO_CRW_EVENT(2, "chsc_process_crw: no event "
528 "information stored\n");
529 return 0;
530 default:
531 CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
532 sei_area->response.code);
533 return 0;
534 }
535
536 /* Check if we might have lost some information. */
537 if (sei_area->flags & 0x40)
538 CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
539 "has been lost due to overflow!\n");
540
541 if (sei_area->rs != 4) {
542 CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
543 "(%04X) isn't a chpid!\n",
544 sei_area->rsid);
545 continue;
546 }
547
548 /* which kind of information was stored? */
549 switch (sei_area->cc) {
550 case 1: /* link incident*/
551 CIO_CRW_EVENT(4, "chsc_process_crw: "
552 "channel subsystem reports link incident,"
553 " reporting source is chpid %x\n",
554 sei_area->rsid);
555 chpid = __get_chpid_from_lir(sei_area->ccdf);
556 if (chpid < 0)
557 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
558 __FUNCTION__);
559 else
560 s390_set_chpid_offline(chpid);
561 break;
562
563 case 2: /* i/o resource accessibiliy */
564 CIO_CRW_EVENT(4, "chsc_process_crw: "
565 "channel subsystem reports some I/O "
566 "devices may have become accessible\n");
567 pr_debug("Data received after sei: \n");
568 pr_debug("Validity flags: %x\n", sei_area->vf);
569
570 /* allocate a new channel path structure, if needed */
571 status = get_chp_status(sei_area->rsid);
572 if (status < 0)
573 new_channel_path(sei_area->rsid);
574 else if (!status)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800575 break;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800576 dev = get_device(&css[0]->chps[sei_area->rsid]->dev);
577 res_data.chp = to_channelpath(dev);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800578 pr_debug("chpid: %x", sei_area->rsid);
579 if ((sei_area->vf & 0xc0) != 0) {
580 res_data.fla = sei_area->fla;
581 if ((sei_area->vf & 0xc0) == 0xc0) {
582 pr_debug(" full link addr: %x",
583 sei_area->fla);
584 res_data.fla_mask = 0xffff;
585 } else {
586 pr_debug(" link addr: %x",
587 sei_area->fla);
588 res_data.fla_mask = 0xff00;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800591 ret = s390_process_res_acc(&res_data);
592 pr_debug("\n\n");
Cornelia Hucka28c6942006-01-06 00:19:23 -0800593 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595
596 default: /* other stuff */
597 CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
598 sei_area->cc);
599 break;
600 }
601 } while (sei_area->flags & 0x80);
602 return ret;
603}
604
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800605static inline int
606__chp_add_new_sch(struct subchannel_id schid)
607{
608 struct schib schib;
609 int ret;
610
611 if (stsch(schid, &schib))
612 /* We're through */
613 return need_rescan ? -EAGAIN : -ENXIO;
614
615 /* Put it on the slow path. */
616 ret = css_enqueue_subchannel_slow(schid);
617 if (ret) {
618 css_clear_subchannel_slow_list();
619 need_rescan = 1;
620 return -EAGAIN;
621 }
622 return 0;
623}
624
625
626static int
627__chp_add(struct subchannel_id schid, void *data)
628{
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200629 int i, mask;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800630 struct channel_path *chp;
631 struct subchannel *sch;
632
Cornelia Huck12975ae2006-10-11 15:31:47 +0200633 chp = data;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800634 sch = get_subchannel_by_schid(schid);
635 if (!sch)
636 /* Check if the subchannel is now available. */
637 return __chp_add_new_sch(schid);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100638 spin_lock_irq(sch->lock);
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200639 for (i=0; i<8; i++) {
640 mask = 0x80 >> i;
641 if ((sch->schib.pmcw.pim & mask) &&
642 (sch->schib.pmcw.chpid[i] == chp->id)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800643 if (stsch(sch->schid, &sch->schib) != 0) {
644 /* Endgame. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100645 spin_unlock_irq(sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800646 return -ENXIO;
647 }
648 break;
649 }
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200650 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800651 if (i==8) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100652 spin_unlock_irq(sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800653 return 0;
654 }
655 sch->lpm = ((sch->schib.pmcw.pim &
656 sch->schib.pmcw.pam &
657 sch->schib.pmcw.pom)
Cornelia Huck7e8ae7b2006-10-06 16:38:29 +0200658 | mask) & sch->opm;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800659
660 if (sch->driver && sch->driver->verify)
661 sch->driver->verify(&sch->dev);
662
Cornelia Huck2ec22982006-12-08 15:54:26 +0100663 spin_unlock_irq(sch->lock);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800664 put_device(&sch->dev);
665 return 0;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int
669chp_add(int chpid)
670{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800671 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 char dbf_txt[15];
Cornelia Hucka28c6942006-01-06 00:19:23 -0800673 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (!get_chp_status(chpid))
676 return 0; /* no need to do the rest */
677
678 sprintf(dbf_txt, "cadd%x", chpid);
679 CIO_TRACE_EVENT(2, dbf_txt);
680
Cornelia Hucka28c6942006-01-06 00:19:23 -0800681 dev = get_device(&css[0]->chps[chpid]->dev);
682 rc = for_each_subchannel(__chp_add, to_channelpath(dev));
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800683 if (css_slow_subchannels_exist())
684 rc = -EAGAIN;
685 if (rc != -EAGAIN)
686 rc = 0;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800687 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return rc;
689}
690
691/*
692 * Handling of crw machine checks with channel path source.
693 */
694int
695chp_process_crw(int chpid, int on)
696{
697 if (on == 0) {
698 /* Path has gone. We use the link incident routine.*/
699 s390_set_chpid_offline(chpid);
700 return 0; /* De-register is async anyway. */
701 }
702 /*
703 * Path has come. Allocate a new channel path structure,
704 * if needed.
705 */
706 if (get_chp_status(chpid) < 0)
707 new_channel_path(chpid);
708 /* Avoid the extra overhead in process_rec_acc. */
709 return chp_add(chpid);
710}
711
Cornelia Hucke7769b42006-10-11 15:31:41 +0200712static inline int check_for_io_on_path(struct subchannel *sch, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int cc;
715
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800716 cc = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (cc)
718 return 0;
Cornelia Hucke7769b42006-10-11 15:31:41 +0200719 if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
723
Cornelia Huckd23861f2006-12-04 15:41:04 +0100724static void terminate_internal_io(struct subchannel *sch)
725{
726 if (cio_clear(sch)) {
727 /* Recheck device in case clear failed. */
728 sch->lpm = 0;
729 if (device_trigger_verify(sch) != 0) {
730 if(css_enqueue_subchannel_slow(sch->schid)) {
731 css_clear_subchannel_slow_list();
732 need_rescan = 1;
733 }
734 }
735 return;
736 }
737 /* Request retry of internal operation. */
738 device_set_intretry(sch);
739 /* Call handler. */
740 if (sch->driver && sch->driver->termination)
741 sch->driver->termination(&sch->dev);
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static inline void
745__s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
746{
747 int chp, old_lpm;
748 unsigned long flags;
749
750 if (!sch->ssd_info.valid)
751 return;
752
Cornelia Huck2ec22982006-12-08 15:54:26 +0100753 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 old_lpm = sch->lpm;
755 for (chp = 0; chp < 8; chp++) {
756 if (sch->ssd_info.chpid[chp] != chpid)
757 continue;
758
759 if (on) {
760 sch->opm |= (0x80 >> chp);
761 sch->lpm |= (0x80 >> chp);
762 if (!old_lpm)
763 device_trigger_reprobe(sch);
764 else if (sch->driver && sch->driver->verify)
765 sch->driver->verify(&sch->dev);
Cornelia Huck24cb5b42006-12-04 15:41:01 +0100766 break;
767 }
768 sch->opm &= ~(0x80 >> chp);
769 sch->lpm &= ~(0x80 >> chp);
Cornelia Huckd23861f2006-12-04 15:41:04 +0100770 if (check_for_io_on_path(sch, chp)) {
771 if (device_is_online(sch))
772 /* Path verification is done after killing. */
773 device_kill_io(sch);
774 else
775 /* Kill and retry internal I/O. */
776 terminate_internal_io(sch);
777 } else if (!sch->lpm) {
Cornelia Huck24cb5b42006-12-04 15:41:01 +0100778 if (device_trigger_verify(sch) != 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800779 if (css_enqueue_subchannel_slow(sch->schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 css_clear_subchannel_slow_list();
781 need_rescan = 1;
782 }
Cornelia Huck24cb5b42006-12-04 15:41:01 +0100783 }
784 } else if (sch->driver && sch->driver->verify)
785 sch->driver->verify(&sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
787 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100788 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static int
792s390_subchannel_vary_chpid_off(struct device *dev, void *data)
793{
794 struct subchannel *sch;
795 __u8 *chpid;
796
797 sch = to_subchannel(dev);
798 chpid = data;
799
800 __s390_subchannel_vary_chpid(sch, *chpid, 0);
801 return 0;
802}
803
804static int
805s390_subchannel_vary_chpid_on(struct device *dev, void *data)
806{
807 struct subchannel *sch;
808 __u8 *chpid;
809
810 sch = to_subchannel(dev);
811 chpid = data;
812
813 __s390_subchannel_vary_chpid(sch, *chpid, 1);
814 return 0;
815}
816
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800817static int
818__s390_vary_chpid_on(struct subchannel_id schid, void *data)
819{
820 struct schib schib;
821 struct subchannel *sch;
822
823 sch = get_subchannel_by_schid(schid);
824 if (sch) {
825 put_device(&sch->dev);
826 return 0;
827 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800828 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800829 /* We're through */
830 return -ENXIO;
831 /* Put it on the slow path. */
832 if (css_enqueue_subchannel_slow(schid)) {
833 css_clear_subchannel_slow_list();
834 need_rescan = 1;
835 return -EAGAIN;
836 }
837 return 0;
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
841 * Function: s390_vary_chpid
842 * Varies the specified chpid online or offline
843 */
844static int
845s390_vary_chpid( __u8 chpid, int on)
846{
847 char dbf_text[15];
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800848 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
851 CIO_TRACE_EVENT( 2, dbf_text);
852
853 status = get_chp_status(chpid);
854 if (status < 0) {
855 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
856 return -EINVAL;
857 }
858
859 if (!on && !status) {
860 printk(KERN_ERR "chpid %x is already offline\n", chpid);
861 return -EINVAL;
862 }
863
864 set_chp_logically_online(chpid, on);
865
866 /*
867 * Redo PathVerification on the devices the chpid connects to
868 */
869
870 bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
871 s390_subchannel_vary_chpid_on :
872 s390_subchannel_vary_chpid_off);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800873 if (on)
874 /* Scan for new devices on varied on path. */
875 for_each_subchannel(__s390_vary_chpid_on, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (need_rescan || css_slow_subchannels_exist())
877 queue_work(slow_path_wq, &slow_path_work);
878 return 0;
879}
880
881/*
Cornelia Huck495a5b42006-03-24 03:15:14 -0800882 * Channel measurement related functions
883 */
884static ssize_t
885chp_measurement_chars_read(struct kobject *kobj, char *buf, loff_t off,
886 size_t count)
887{
888 struct channel_path *chp;
889 unsigned int size;
890
891 chp = to_channelpath(container_of(kobj, struct device, kobj));
892 if (!chp->cmg_chars)
893 return 0;
894
895 size = sizeof(struct cmg_chars);
896
897 if (off > size)
898 return 0;
899 if (off + count > size)
900 count = size - off;
901 memcpy(buf, chp->cmg_chars + off, count);
902 return count;
903}
904
905static struct bin_attribute chp_measurement_chars_attr = {
906 .attr = {
907 .name = "measurement_chars",
908 .mode = S_IRUSR,
909 .owner = THIS_MODULE,
910 },
911 .size = sizeof(struct cmg_chars),
912 .read = chp_measurement_chars_read,
913};
914
915static void
916chp_measurement_copy_block(struct cmg_entry *buf,
917 struct channel_subsystem *css, int chpid)
918{
919 void *area;
920 struct cmg_entry *entry, reference_buf;
921 int idx;
922
923 if (chpid < 128) {
924 area = css->cub_addr1;
925 idx = chpid;
926 } else {
927 area = css->cub_addr2;
928 idx = chpid - 128;
929 }
930 entry = area + (idx * sizeof(struct cmg_entry));
931 do {
932 memcpy(buf, entry, sizeof(*entry));
933 memcpy(&reference_buf, entry, sizeof(*entry));
934 } while (reference_buf.values[0] != buf->values[0]);
935}
936
937static ssize_t
938chp_measurement_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
939{
940 struct channel_path *chp;
941 struct channel_subsystem *css;
942 unsigned int size;
943
944 chp = to_channelpath(container_of(kobj, struct device, kobj));
945 css = to_css(chp->dev.parent);
946
Cornelia Huck231caa12006-06-29 14:56:45 +0200947 size = sizeof(struct cmg_entry);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800948
949 /* Only allow single reads. */
950 if (off || count < size)
951 return 0;
952 chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->id);
Cornelia Huck231caa12006-06-29 14:56:45 +0200953 count = size;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800954 return count;
955}
956
957static struct bin_attribute chp_measurement_attr = {
958 .attr = {
959 .name = "measurement",
960 .mode = S_IRUSR,
961 .owner = THIS_MODULE,
962 },
963 .size = sizeof(struct cmg_entry),
964 .read = chp_measurement_read,
965};
966
967static void
968chsc_remove_chp_cmg_attr(struct channel_path *chp)
969{
Cornelia Huck1125b462007-02-05 21:17:18 +0100970 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
971 device_remove_bin_file(&chp->dev, &chp_measurement_attr);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800972}
973
974static int
975chsc_add_chp_cmg_attr(struct channel_path *chp)
976{
977 int ret;
978
Cornelia Huck1125b462007-02-05 21:17:18 +0100979 ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800980 if (ret)
981 return ret;
Cornelia Huck1125b462007-02-05 21:17:18 +0100982 ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800983 if (ret)
Cornelia Huck1125b462007-02-05 21:17:18 +0100984 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800985 return ret;
986}
987
988static void
989chsc_remove_cmg_attr(struct channel_subsystem *css)
990{
991 int i;
992
993 for (i = 0; i <= __MAX_CHPID; i++) {
994 if (!css->chps[i])
995 continue;
996 chsc_remove_chp_cmg_attr(css->chps[i]);
997 }
998}
999
1000static int
1001chsc_add_cmg_attr(struct channel_subsystem *css)
1002{
1003 int i, ret;
1004
1005 ret = 0;
1006 for (i = 0; i <= __MAX_CHPID; i++) {
1007 if (!css->chps[i])
1008 continue;
1009 ret = chsc_add_chp_cmg_attr(css->chps[i]);
1010 if (ret)
1011 goto cleanup;
1012 }
1013 return ret;
1014cleanup:
1015 for (--i; i >= 0; i--) {
1016 if (!css->chps[i])
1017 continue;
1018 chsc_remove_chp_cmg_attr(css->chps[i]);
1019 }
1020 return ret;
1021}
1022
1023
1024static int
1025__chsc_do_secm(struct channel_subsystem *css, int enable, void *page)
1026{
1027 struct {
1028 struct chsc_header request;
1029 u32 operation_code : 2;
1030 u32 : 30;
1031 u32 key : 4;
1032 u32 : 28;
1033 u32 zeroes1;
1034 u32 cub_addr1;
1035 u32 zeroes2;
1036 u32 cub_addr2;
1037 u32 reserved[13];
1038 struct chsc_header response;
1039 u32 status : 8;
1040 u32 : 4;
1041 u32 fmt : 4;
1042 u32 : 16;
1043 } *secm_area;
1044 int ret, ccode;
1045
1046 secm_area = page;
1047 secm_area->request.length = 0x0050;
1048 secm_area->request.code = 0x0016;
1049
1050 secm_area->key = PAGE_DEFAULT_KEY;
1051 secm_area->cub_addr1 = (u64)(unsigned long)css->cub_addr1;
1052 secm_area->cub_addr2 = (u64)(unsigned long)css->cub_addr2;
1053
1054 secm_area->operation_code = enable ? 0 : 1;
1055
1056 ccode = chsc(secm_area);
1057 if (ccode > 0)
1058 return (ccode == 3) ? -ENODEV : -EBUSY;
1059
1060 switch (secm_area->response.code) {
1061 case 0x0001: /* Success. */
1062 ret = 0;
1063 break;
1064 case 0x0003: /* Invalid block. */
1065 case 0x0007: /* Invalid format. */
1066 case 0x0008: /* Other invalid block. */
1067 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1068 ret = -EINVAL;
1069 break;
1070 case 0x0004: /* Command not provided in model. */
1071 CIO_CRW_EVENT(2, "Model does not provide secm\n");
1072 ret = -EOPNOTSUPP;
1073 break;
1074 case 0x0102: /* cub adresses incorrect */
1075 CIO_CRW_EVENT(2, "Invalid addresses in chsc request block\n");
1076 ret = -EINVAL;
1077 break;
1078 case 0x0103: /* key error */
1079 CIO_CRW_EVENT(2, "Access key error in secm\n");
1080 ret = -EINVAL;
1081 break;
1082 case 0x0105: /* error while starting */
1083 CIO_CRW_EVENT(2, "Error while starting channel measurement\n");
1084 ret = -EIO;
1085 break;
1086 default:
1087 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1088 secm_area->response.code);
1089 ret = -EIO;
1090 }
1091 return ret;
1092}
1093
1094int
1095chsc_secm(struct channel_subsystem *css, int enable)
1096{
1097 void *secm_area;
1098 int ret;
1099
1100 secm_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1101 if (!secm_area)
1102 return -ENOMEM;
1103
1104 mutex_lock(&css->mutex);
1105 if (enable && !css->cm_enabled) {
1106 css->cub_addr1 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1107 css->cub_addr2 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1108 if (!css->cub_addr1 || !css->cub_addr2) {
1109 free_page((unsigned long)css->cub_addr1);
1110 free_page((unsigned long)css->cub_addr2);
1111 free_page((unsigned long)secm_area);
1112 mutex_unlock(&css->mutex);
1113 return -ENOMEM;
1114 }
1115 }
1116 ret = __chsc_do_secm(css, enable, secm_area);
1117 if (!ret) {
1118 css->cm_enabled = enable;
1119 if (css->cm_enabled) {
1120 ret = chsc_add_cmg_attr(css);
1121 if (ret) {
1122 memset(secm_area, 0, PAGE_SIZE);
1123 __chsc_do_secm(css, 0, secm_area);
1124 css->cm_enabled = 0;
1125 }
1126 } else
1127 chsc_remove_cmg_attr(css);
1128 }
1129 if (enable && !css->cm_enabled) {
1130 free_page((unsigned long)css->cub_addr1);
1131 free_page((unsigned long)css->cub_addr2);
1132 }
1133 mutex_unlock(&css->mutex);
1134 free_page((unsigned long)secm_area);
1135 return ret;
1136}
1137
1138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * Files for the channel path entries.
1140 */
1141static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001142chp_status_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1145
1146 if (!chp)
1147 return 0;
1148 return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
1149 sprintf(buf, "offline\n"));
1150}
1151
1152static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001153chp_status_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct channel_path *cp = container_of(dev, struct channel_path, dev);
1156 char cmd[10];
1157 int num_args;
1158 int error;
1159
1160 num_args = sscanf(buf, "%5s", cmd);
1161 if (!num_args)
1162 return count;
1163
1164 if (!strnicmp(cmd, "on", 2))
1165 error = s390_vary_chpid(cp->id, 1);
1166 else if (!strnicmp(cmd, "off", 3))
1167 error = s390_vary_chpid(cp->id, 0);
1168 else
1169 error = -EINVAL;
1170
1171 return error < 0 ? error : count;
1172
1173}
1174
1175static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
1176
1177static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001178chp_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct channel_path *chp = container_of(dev, struct channel_path, dev);
1181
1182 if (!chp)
1183 return 0;
1184 return sprintf(buf, "%x\n", chp->desc.desc);
1185}
1186
1187static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
1188
Cornelia Huck495a5b42006-03-24 03:15:14 -08001189static ssize_t
1190chp_cmg_show(struct device *dev, struct device_attribute *attr, char *buf)
1191{
1192 struct channel_path *chp = to_channelpath(dev);
1193
1194 if (!chp)
1195 return 0;
1196 if (chp->cmg == -1) /* channel measurements not available */
1197 return sprintf(buf, "unknown\n");
1198 return sprintf(buf, "%x\n", chp->cmg);
1199}
1200
1201static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
1202
1203static ssize_t
1204chp_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
1205{
1206 struct channel_path *chp = to_channelpath(dev);
1207
1208 if (!chp)
1209 return 0;
1210 if (chp->shared == -1) /* channel measurements not available */
1211 return sprintf(buf, "unknown\n");
1212 return sprintf(buf, "%x\n", chp->shared);
1213}
1214
1215static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217static struct attribute * chp_attrs[] = {
1218 &dev_attr_status.attr,
1219 &dev_attr_type.attr,
Cornelia Huck495a5b42006-03-24 03:15:14 -08001220 &dev_attr_cmg.attr,
1221 &dev_attr_shared.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 NULL,
1223};
1224
1225static struct attribute_group chp_attr_group = {
1226 .attrs = chp_attrs,
1227};
1228
1229static void
1230chp_release(struct device *dev)
1231{
1232 struct channel_path *cp;
1233
1234 cp = container_of(dev, struct channel_path, dev);
1235 kfree(cp);
1236}
1237
1238static int
1239chsc_determine_channel_path_description(int chpid,
1240 struct channel_path_desc *desc)
1241{
1242 int ccode, ret;
1243
1244 struct {
1245 struct chsc_header request;
1246 u32 : 24;
1247 u32 first_chpid : 8;
1248 u32 : 24;
1249 u32 last_chpid : 8;
1250 u32 zeroes1;
1251 struct chsc_header response;
1252 u32 zeroes2;
1253 struct channel_path_desc desc;
1254 } *scpd_area;
1255
1256 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1257 if (!scpd_area)
1258 return -ENOMEM;
1259
Cornelia Huck495a5b42006-03-24 03:15:14 -08001260 scpd_area->request.length = 0x0010;
1261 scpd_area->request.code = 0x0002;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 scpd_area->first_chpid = chpid;
1264 scpd_area->last_chpid = chpid;
1265
1266 ccode = chsc(scpd_area);
1267 if (ccode > 0) {
1268 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1269 goto out;
1270 }
1271
1272 switch (scpd_area->response.code) {
1273 case 0x0001: /* Success. */
1274 memcpy(desc, &scpd_area->desc,
1275 sizeof(struct channel_path_desc));
1276 ret = 0;
1277 break;
1278 case 0x0003: /* Invalid block. */
1279 case 0x0007: /* Invalid format. */
1280 case 0x0008: /* Other invalid block. */
1281 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1282 ret = -EINVAL;
1283 break;
1284 case 0x0004: /* Command not provided in model. */
1285 CIO_CRW_EVENT(2, "Model does not provide scpd\n");
1286 ret = -EOPNOTSUPP;
1287 break;
1288 default:
1289 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1290 scpd_area->response.code);
1291 ret = -EIO;
1292 }
1293out:
1294 free_page((unsigned long)scpd_area);
1295 return ret;
1296}
1297
Cornelia Huck495a5b42006-03-24 03:15:14 -08001298static void
1299chsc_initialize_cmg_chars(struct channel_path *chp, u8 cmcv,
1300 struct cmg_chars *chars)
1301{
1302 switch (chp->cmg) {
1303 case 2:
1304 case 3:
1305 chp->cmg_chars = kmalloc(sizeof(struct cmg_chars),
1306 GFP_KERNEL);
1307 if (chp->cmg_chars) {
1308 int i, mask;
1309 struct cmg_chars *cmg_chars;
1310
1311 cmg_chars = chp->cmg_chars;
1312 for (i = 0; i < NR_MEASUREMENT_CHARS; i++) {
1313 mask = 0x80 >> (i + 3);
1314 if (cmcv & mask)
1315 cmg_chars->values[i] = chars->values[i];
1316 else
1317 cmg_chars->values[i] = 0;
1318 }
1319 }
1320 break;
1321 default:
1322 /* No cmg-dependent data. */
1323 break;
1324 }
1325}
1326
1327static int
1328chsc_get_channel_measurement_chars(struct channel_path *chp)
1329{
1330 int ccode, ret;
1331
1332 struct {
1333 struct chsc_header request;
1334 u32 : 24;
1335 u32 first_chpid : 8;
1336 u32 : 24;
1337 u32 last_chpid : 8;
1338 u32 zeroes1;
1339 struct chsc_header response;
1340 u32 zeroes2;
1341 u32 not_valid : 1;
1342 u32 shared : 1;
1343 u32 : 22;
1344 u32 chpid : 8;
1345 u32 cmcv : 5;
1346 u32 : 11;
1347 u32 cmgq : 8;
1348 u32 cmg : 8;
1349 u32 zeroes3;
1350 u32 data[NR_MEASUREMENT_CHARS];
1351 } *scmc_area;
1352
1353 scmc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1354 if (!scmc_area)
1355 return -ENOMEM;
1356
1357 scmc_area->request.length = 0x0010;
1358 scmc_area->request.code = 0x0022;
1359
1360 scmc_area->first_chpid = chp->id;
1361 scmc_area->last_chpid = chp->id;
1362
1363 ccode = chsc(scmc_area);
1364 if (ccode > 0) {
1365 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1366 goto out;
1367 }
1368
1369 switch (scmc_area->response.code) {
1370 case 0x0001: /* Success. */
1371 if (!scmc_area->not_valid) {
1372 chp->cmg = scmc_area->cmg;
1373 chp->shared = scmc_area->shared;
1374 chsc_initialize_cmg_chars(chp, scmc_area->cmcv,
1375 (struct cmg_chars *)
1376 &scmc_area->data);
1377 } else {
1378 chp->cmg = -1;
1379 chp->shared = -1;
1380 }
1381 ret = 0;
1382 break;
1383 case 0x0003: /* Invalid block. */
1384 case 0x0007: /* Invalid format. */
1385 case 0x0008: /* Invalid bit combination. */
1386 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1387 ret = -EINVAL;
1388 break;
1389 case 0x0004: /* Command not provided. */
1390 CIO_CRW_EVENT(2, "Model does not provide scmc\n");
1391 ret = -EOPNOTSUPP;
1392 break;
1393 default:
1394 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1395 scmc_area->response.code);
1396 ret = -EIO;
1397 }
1398out:
1399 free_page((unsigned long)scmc_area);
1400 return ret;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * Entries for chpids on the system bus.
1405 * This replaces /proc/chpids.
1406 */
1407static int
1408new_channel_path(int chpid)
1409{
1410 struct channel_path *chp;
1411 int ret;
1412
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001413 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!chp)
1415 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* fill in status, etc. */
1418 chp->id = chpid;
1419 chp->state = 1;
Heiko Carstens292888c2006-08-30 14:33:35 +02001420 chp->dev.parent = &css[0]->device;
1421 chp->dev.release = chp_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
1423
1424 /* Obtain channel path description and fill it in. */
1425 ret = chsc_determine_channel_path_description(chpid, &chp->desc);
1426 if (ret)
1427 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001428 /* Get channel-measurement characteristics. */
1429 if (css_characteristics_avail && css_chsc_characteristics.scmc
1430 && css_chsc_characteristics.secm) {
1431 ret = chsc_get_channel_measurement_chars(chp);
1432 if (ret)
1433 goto out_free;
1434 } else {
1435 static int msg_done;
1436
1437 if (!msg_done) {
1438 printk(KERN_WARNING "cio: Channel measurements not "
1439 "available, continuing.\n");
1440 msg_done = 1;
1441 }
1442 chp->cmg = -1;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /* make it known to the system */
1446 ret = device_register(&chp->dev);
1447 if (ret) {
1448 printk(KERN_WARNING "%s: could not register %02x\n",
1449 __func__, chpid);
1450 goto out_free;
1451 }
1452 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
1453 if (ret) {
1454 device_unregister(&chp->dev);
1455 goto out_free;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001456 }
1457 mutex_lock(&css[0]->mutex);
1458 if (css[0]->cm_enabled) {
1459 ret = chsc_add_chp_cmg_attr(chp);
1460 if (ret) {
1461 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
1462 device_unregister(&chp->dev);
1463 mutex_unlock(&css[0]->mutex);
1464 goto out_free;
1465 }
1466 }
1467 css[0]->chps[chpid] = chp;
1468 mutex_unlock(&css[0]->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return ret;
1470out_free:
1471 kfree(chp);
1472 return ret;
1473}
1474
1475void *
1476chsc_get_chp_desc(struct subchannel *sch, int chp_no)
1477{
1478 struct channel_path *chp;
1479 struct channel_path_desc *desc;
1480
Cornelia Hucka28c6942006-01-06 00:19:23 -08001481 chp = css[0]->chps[sch->schib.pmcw.chpid[chp_no]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (!chp)
1483 return NULL;
1484 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
1485 if (!desc)
1486 return NULL;
1487 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
1488 return desc;
1489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491static int __init
1492chsc_alloc_sei_area(void)
1493{
1494 sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1495 if (!sei_page)
1496 printk(KERN_WARNING"Can't allocate page for processing of " \
1497 "chsc machine checks!\n");
1498 return (sei_page ? 0 : -ENOMEM);
1499}
1500
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001501int __init
1502chsc_enable_facility(int operation_code)
1503{
1504 int ret;
1505 struct {
1506 struct chsc_header request;
1507 u8 reserved1:4;
1508 u8 format:4;
1509 u8 reserved2;
1510 u16 operation_code;
1511 u32 reserved3;
1512 u32 reserved4;
1513 u32 operation_data_area[252];
1514 struct chsc_header response;
1515 u32 reserved5:4;
1516 u32 format2:4;
1517 u32 reserved6:24;
1518 } *sda_area;
1519
1520 sda_area = (void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
1521 if (!sda_area)
1522 return -ENOMEM;
Cornelia Huck495a5b42006-03-24 03:15:14 -08001523 sda_area->request.length = 0x0400;
1524 sda_area->request.code = 0x0031;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001525 sda_area->operation_code = operation_code;
1526
1527 ret = chsc(sda_area);
1528 if (ret > 0) {
1529 ret = (ret == 3) ? -ENODEV : -EBUSY;
1530 goto out;
1531 }
1532 switch (sda_area->response.code) {
Cornelia Huck15730dd2006-03-06 15:43:02 -08001533 case 0x0001: /* everything ok */
1534 ret = 0;
1535 break;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001536 case 0x0003: /* invalid request block */
1537 case 0x0007:
1538 ret = -EINVAL;
1539 break;
1540 case 0x0004: /* command not provided */
1541 case 0x0101: /* facility not provided */
1542 ret = -EOPNOTSUPP;
1543 break;
Cornelia Huck15730dd2006-03-06 15:43:02 -08001544 default: /* something went wrong */
1545 ret = -EIO;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001546 }
1547 out:
1548 free_page((unsigned long)sda_area);
1549 return ret;
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552subsys_initcall(chsc_alloc_sei_area);
1553
1554struct css_general_char css_general_characteristics;
1555struct css_chsc_char css_chsc_characteristics;
1556
1557int __init
1558chsc_determine_css_characteristics(void)
1559{
1560 int result;
1561 struct {
1562 struct chsc_header request;
1563 u32 reserved1;
1564 u32 reserved2;
1565 u32 reserved3;
1566 struct chsc_header response;
1567 u32 reserved4;
1568 u32 general_char[510];
1569 u32 chsc_char[518];
1570 } *scsc_area;
1571
1572 scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1573 if (!scsc_area) {
1574 printk(KERN_WARNING"cio: Was not able to determine available" \
1575 "CHSCs due to no memory.\n");
1576 return -ENOMEM;
1577 }
1578
Cornelia Huck495a5b42006-03-24 03:15:14 -08001579 scsc_area->request.length = 0x0010;
1580 scsc_area->request.code = 0x0010;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 result = chsc(scsc_area);
1583 if (result) {
1584 printk(KERN_WARNING"cio: Was not able to determine " \
1585 "available CHSCs, cc=%i.\n", result);
1586 result = -EIO;
1587 goto exit;
1588 }
1589
1590 if (scsc_area->response.code != 1) {
1591 printk(KERN_WARNING"cio: Was not able to determine " \
1592 "available CHSCs.\n");
1593 result = -EIO;
1594 goto exit;
1595 }
1596 memcpy(&css_general_characteristics, scsc_area->general_char,
1597 sizeof(css_general_characteristics));
1598 memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
1599 sizeof(css_chsc_characteristics));
1600exit:
1601 free_page ((unsigned long) scsc_area);
1602 return result;
1603}
1604
1605EXPORT_SYMBOL_GPL(css_general_characteristics);
1606EXPORT_SYMBOL_GPL(css_chsc_characteristics);