linux/drivers/s390/block/dasd_eckd.c
<<
>>
Prefs
   1/*
   2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
   3 *                  Horst Hummel <Horst.Hummel@de.ibm.com>
   4 *                  Carsten Otte <Cotte@de.ibm.com>
   5 *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
   6 * Bugreports.to..: <Linux390@de.ibm.com>
   7 * Copyright IBM Corp. 1999, 2009
   8 * EMC Symmetrix ioctl Copyright EMC Corporation, 2008
   9 * Author.........: Nigel Hislop <hislop_nigel@emc.com>
  10 */
  11
  12#define KMSG_COMPONENT "dasd-eckd"
  13
  14#include <linux/stddef.h>
  15#include <linux/kernel.h>
  16#include <linux/slab.h>
  17#include <linux/hdreg.h>        /* HDIO_GETGEO                      */
  18#include <linux/bio.h>
  19#include <linux/module.h>
  20#include <linux/compat.h>
  21#include <linux/init.h>
  22#include <linux/seq_file.h>
  23
  24#include <asm/css_chars.h>
  25#include <asm/debug.h>
  26#include <asm/idals.h>
  27#include <asm/ebcdic.h>
  28#include <asm/io.h>
  29#include <asm/uaccess.h>
  30#include <asm/cio.h>
  31#include <asm/ccwdev.h>
  32#include <asm/itcw.h>
  33#include <asm/schid.h>
  34#include <asm/chpid.h>
  35
  36#include "dasd_int.h"
  37#include "dasd_eckd.h"
  38
  39#ifdef PRINTK_HEADER
  40#undef PRINTK_HEADER
  41#endif                          /* PRINTK_HEADER */
  42#define PRINTK_HEADER "dasd(eckd):"
  43
  44#define ECKD_C0(i) (i->home_bytes)
  45#define ECKD_F(i) (i->formula)
  46#define ECKD_F1(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f1):\
  47                    (i->factors.f_0x02.f1))
  48#define ECKD_F2(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f2):\
  49                    (i->factors.f_0x02.f2))
  50#define ECKD_F3(i) (ECKD_F(i)==0x01?(i->factors.f_0x01.f3):\
  51                    (i->factors.f_0x02.f3))
  52#define ECKD_F4(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f4):0)
  53#define ECKD_F5(i) (ECKD_F(i)==0x02?(i->factors.f_0x02.f5):0)
  54#define ECKD_F6(i) (i->factor6)
  55#define ECKD_F7(i) (i->factor7)
  56#define ECKD_F8(i) (i->factor8)
  57
  58/*
  59 * raw track access always map to 64k in memory
  60 * so it maps to 16 blocks of 4k per track
  61 */
  62#define DASD_RAW_BLOCK_PER_TRACK 16
  63#define DASD_RAW_BLOCKSIZE 4096
  64/* 64k are 128 x 512 byte sectors  */
  65#define DASD_RAW_SECTORS_PER_TRACK 128
  66
  67MODULE_LICENSE("GPL");
  68
  69static struct dasd_discipline dasd_eckd_discipline;
  70
  71/* The ccw bus type uses this table to find devices that it sends to
  72 * dasd_eckd_probe */
  73static struct ccw_device_id dasd_eckd_ids[] = {
  74        { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3390, 0), .driver_info = 0x1},
  75        { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3390, 0), .driver_info = 0x2},
  76        { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3380, 0), .driver_info = 0x3},
  77        { CCW_DEVICE_DEVTYPE (0x3990, 0, 0x3380, 0), .driver_info = 0x4},
  78        { CCW_DEVICE_DEVTYPE (0x2105, 0, 0x3380, 0), .driver_info = 0x5},
  79        { CCW_DEVICE_DEVTYPE (0x9343, 0, 0x9345, 0), .driver_info = 0x6},
  80        { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3390, 0), .driver_info = 0x7},
  81        { CCW_DEVICE_DEVTYPE (0x2107, 0, 0x3380, 0), .driver_info = 0x8},
  82        { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3390, 0), .driver_info = 0x9},
  83        { CCW_DEVICE_DEVTYPE (0x1750, 0, 0x3380, 0), .driver_info = 0xa},
  84        { /* end of list */ },
  85};
  86
  87MODULE_DEVICE_TABLE(ccw, dasd_eckd_ids);
  88
  89static struct ccw_driver dasd_eckd_driver; /* see below */
  90
  91static void *rawpadpage;
  92
  93#define INIT_CQR_OK 0
  94#define INIT_CQR_UNFORMATTED 1
  95#define INIT_CQR_ERROR 2
  96
  97/* emergency request for reserve/release */
  98static struct {
  99        struct dasd_ccw_req cqr;
 100        struct ccw1 ccw;
 101        char data[32];
 102} *dasd_reserve_req;
 103static DEFINE_MUTEX(dasd_reserve_mutex);
 104
 105/* definitions for the path verification worker */
 106struct path_verification_work_data {
 107        struct work_struct worker;
 108        struct dasd_device *device;
 109        struct dasd_ccw_req cqr;
 110        struct ccw1 ccw;
 111        __u8 rcd_buffer[DASD_ECKD_RCD_DATA_SIZE];
 112        int isglobal;
 113        __u8 tbvpm;
 114};
 115static struct path_verification_work_data *path_verification_worker;
 116static DEFINE_MUTEX(dasd_path_verification_mutex);
 117
 118struct check_attention_work_data {
 119        struct work_struct worker;
 120        struct dasd_device *device;
 121        __u8 lpum;
 122};
 123
 124static int prepare_itcw(struct itcw *, unsigned int, unsigned int, int,
 125                        struct dasd_device *, struct dasd_device *,
 126                        unsigned int, int, unsigned int, unsigned int,
 127                        unsigned int, unsigned int);
 128
 129/* initial attempt at a probe function. this can be simplified once
 130 * the other detection code is gone */
 131static int
 132dasd_eckd_probe (struct ccw_device *cdev)
 133{
 134        int ret;
 135
 136        /* set ECKD specific ccw-device options */
 137        ret = ccw_device_set_options(cdev, CCWDEV_ALLOW_FORCE |
 138                                     CCWDEV_DO_PATHGROUP | CCWDEV_DO_MULTIPATH);
 139        if (ret) {
 140                DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
 141                                "dasd_eckd_probe: could not set "
 142                                "ccw-device options");
 143                return ret;
 144        }
 145        ret = dasd_generic_probe(cdev, &dasd_eckd_discipline);
 146        return ret;
 147}
 148
 149static int
 150dasd_eckd_set_online(struct ccw_device *cdev)
 151{
 152        return dasd_generic_set_online(cdev, &dasd_eckd_discipline);
 153}
 154
 155static const int sizes_trk0[] = { 28, 148, 84 };
 156#define LABEL_SIZE 140
 157
 158/* head and record addresses of count_area read in analysis ccw */
 159static const int count_area_head[] = { 0, 0, 0, 0, 2 };
 160static const int count_area_rec[] = { 1, 2, 3, 4, 1 };
 161
 162static inline unsigned int
 163round_up_multiple(unsigned int no, unsigned int mult)
 164{
 165        int rem = no % mult;
 166        return (rem ? no - rem + mult : no);
 167}
 168
 169static inline unsigned int
 170ceil_quot(unsigned int d1, unsigned int d2)
 171{
 172        return (d1 + (d2 - 1)) / d2;
 173}
 174
 175static unsigned int
 176recs_per_track(struct dasd_eckd_characteristics * rdc,
 177               unsigned int kl, unsigned int dl)
 178{
 179        int dn, kn;
 180
 181        switch (rdc->dev_type) {
 182        case 0x3380:
 183                if (kl)
 184                        return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
 185                                       ceil_quot(dl + 12, 32));
 186                else
 187                        return 1499 / (15 + ceil_quot(dl + 12, 32));
 188        case 0x3390:
 189                dn = ceil_quot(dl + 6, 232) + 1;
 190                if (kl) {
 191                        kn = ceil_quot(kl + 6, 232) + 1;
 192                        return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
 193                                       9 + ceil_quot(dl + 6 * dn, 34));
 194                } else
 195                        return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
 196        case 0x9345:
 197                dn = ceil_quot(dl + 6, 232) + 1;
 198                if (kl) {
 199                        kn = ceil_quot(kl + 6, 232) + 1;
 200                        return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
 201                                       ceil_quot(dl + 6 * dn, 34));
 202                } else
 203                        return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
 204        }
 205        return 0;
 206}
 207
 208static void set_ch_t(struct ch_t *geo, __u32 cyl, __u8 head)
 209{
 210        geo->cyl = (__u16) cyl;
 211        geo->head = cyl >> 16;
 212        geo->head <<= 4;
 213        geo->head |= head;
 214}
 215
 216static int
 217check_XRC (struct ccw1         *de_ccw,
 218           struct DE_eckd_data *data,
 219           struct dasd_device  *device)
 220{
 221        struct dasd_eckd_private *private = device->private;
 222        int rc;
 223
 224        if (!private->rdc_data.facilities.XRC_supported)
 225                return 0;
 226
 227        /* switch on System Time Stamp - needed for XRC Support */
 228        data->ga_extended |= 0x08; /* switch on 'Time Stamp Valid'   */
 229        data->ga_extended |= 0x02; /* switch on 'Extended Parameter' */
 230
 231        rc = get_phys_clock(&data->ep_sys_time);
 232        /* Ignore return code if sync clock is switched off. */
 233        if (rc == -EOPNOTSUPP || rc == -EACCES)
 234                rc = 0;
 235
 236        de_ccw->count = sizeof(struct DE_eckd_data);
 237        de_ccw->flags |= CCW_FLAG_SLI;
 238        return rc;
 239}
 240
 241static int
 242define_extent(struct ccw1 *ccw, struct DE_eckd_data *data, unsigned int trk,
 243              unsigned int totrk, int cmd, struct dasd_device *device)
 244{
 245        struct dasd_eckd_private *private = device->private;
 246        u32 begcyl, endcyl;
 247        u16 heads, beghead, endhead;
 248        int rc = 0;
 249
 250        ccw->cmd_code = DASD_ECKD_CCW_DEFINE_EXTENT;
 251        ccw->flags = 0;
 252        ccw->count = 16;
 253        ccw->cda = (__u32) __pa(data);
 254
 255        memset(data, 0, sizeof(struct DE_eckd_data));
 256        switch (cmd) {
 257        case DASD_ECKD_CCW_READ_HOME_ADDRESS:
 258        case DASD_ECKD_CCW_READ_RECORD_ZERO:
 259        case DASD_ECKD_CCW_READ:
 260        case DASD_ECKD_CCW_READ_MT:
 261        case DASD_ECKD_CCW_READ_CKD:
 262        case DASD_ECKD_CCW_READ_CKD_MT:
 263        case DASD_ECKD_CCW_READ_KD:
 264        case DASD_ECKD_CCW_READ_KD_MT:
 265                data->mask.perm = 0x1;
 266                data->attributes.operation = private->attrib.operation;
 267                break;
 268        case DASD_ECKD_CCW_READ_COUNT:
 269                data->mask.perm = 0x1;
 270                data->attributes.operation = DASD_BYPASS_CACHE;
 271                break;
 272        case DASD_ECKD_CCW_WRITE:
 273        case DASD_ECKD_CCW_WRITE_MT:
 274        case DASD_ECKD_CCW_WRITE_KD:
 275        case DASD_ECKD_CCW_WRITE_KD_MT:
 276                data->mask.perm = 0x02;
 277                data->attributes.operation = private->attrib.operation;
 278                rc = check_XRC (ccw, data, device);
 279                break;
 280        case DASD_ECKD_CCW_WRITE_CKD:
 281        case DASD_ECKD_CCW_WRITE_CKD_MT:
 282                data->attributes.operation = DASD_BYPASS_CACHE;
 283                rc = check_XRC (ccw, data, device);
 284                break;
 285        case DASD_ECKD_CCW_ERASE:
 286        case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
 287        case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
 288                data->mask.perm = 0x3;
 289                data->mask.auth = 0x1;
 290                data->attributes.operation = DASD_BYPASS_CACHE;
 291                rc = check_XRC (ccw, data, device);
 292                break;
 293        default:
 294                dev_err(&device->cdev->dev,
 295                        "0x%x is not a known command\n", cmd);
 296                break;
 297        }
 298
 299        data->attributes.mode = 0x3;    /* ECKD */
 300
 301        if ((private->rdc_data.cu_type == 0x2105 ||
 302             private->rdc_data.cu_type == 0x2107 ||
 303             private->rdc_data.cu_type == 0x1750)
 304            && !(private->uses_cdl && trk < 2))
 305                data->ga_extended |= 0x40; /* Regular Data Format Mode */
 306
 307        heads = private->rdc_data.trk_per_cyl;
 308        begcyl = trk / heads;
 309        beghead = trk % heads;
 310        endcyl = totrk / heads;
 311        endhead = totrk % heads;
 312
 313        /* check for sequential prestage - enhance cylinder range */
 314        if (data->attributes.operation == DASD_SEQ_PRESTAGE ||
 315            data->attributes.operation == DASD_SEQ_ACCESS) {
 316
 317                if (endcyl + private->attrib.nr_cyl < private->real_cyl)
 318                        endcyl += private->attrib.nr_cyl;
 319                else
 320                        endcyl = (private->real_cyl - 1);
 321        }
 322
 323        set_ch_t(&data->beg_ext, begcyl, beghead);
 324        set_ch_t(&data->end_ext, endcyl, endhead);
 325        return rc;
 326}
 327
 328static int check_XRC_on_prefix(struct PFX_eckd_data *pfxdata,
 329                               struct dasd_device  *device)
 330{
 331        struct dasd_eckd_private *private = device->private;
 332        int rc;
 333
 334        if (!private->rdc_data.facilities.XRC_supported)
 335                return 0;
 336
 337        /* switch on System Time Stamp - needed for XRC Support */
 338        pfxdata->define_extent.ga_extended |= 0x08; /* 'Time Stamp Valid'   */
 339        pfxdata->define_extent.ga_extended |= 0x02; /* 'Extended Parameter' */
 340        pfxdata->validity.time_stamp = 1;           /* 'Time Stamp Valid'   */
 341
 342        rc = get_phys_clock(&pfxdata->define_extent.ep_sys_time);
 343        /* Ignore return code if sync clock is switched off. */
 344        if (rc == -EOPNOTSUPP || rc == -EACCES)
 345                rc = 0;
 346        return rc;
 347}
 348
 349static void fill_LRE_data(struct LRE_eckd_data *data, unsigned int trk,
 350                          unsigned int rec_on_trk, int count, int cmd,
 351                          struct dasd_device *device, unsigned int reclen,
 352                          unsigned int tlf)
 353{
 354        struct dasd_eckd_private *private = device->private;
 355        int sector;
 356        int dn, d;
 357
 358        memset(data, 0, sizeof(*data));
 359        sector = 0;
 360        if (rec_on_trk) {
 361                switch (private->rdc_data.dev_type) {
 362                case 0x3390:
 363                        dn = ceil_quot(reclen + 6, 232);
 364                        d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
 365                        sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
 366                        break;
 367                case 0x3380:
 368                        d = 7 + ceil_quot(reclen + 12, 32);
 369                        sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
 370                        break;
 371                }
 372        }
 373        data->sector = sector;
 374        /* note: meaning of count depends on the operation
 375         *       for record based I/O it's the number of records, but for
 376         *       track based I/O it's the number of tracks
 377         */
 378        data->count = count;
 379        switch (cmd) {
 380        case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
 381                data->operation.orientation = 0x3;
 382                data->operation.operation = 0x03;
 383                break;
 384        case DASD_ECKD_CCW_READ_HOME_ADDRESS:
 385                data->operation.orientation = 0x3;
 386                data->operation.operation = 0x16;
 387                break;
 388        case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
 389                data->operation.orientation = 0x1;
 390                data->operation.operation = 0x03;
 391                data->count++;
 392                break;
 393        case DASD_ECKD_CCW_READ_RECORD_ZERO:
 394                data->operation.orientation = 0x3;
 395                data->operation.operation = 0x16;
 396                data->count++;
 397                break;
 398        case DASD_ECKD_CCW_WRITE:
 399        case DASD_ECKD_CCW_WRITE_MT:
 400        case DASD_ECKD_CCW_WRITE_KD:
 401        case DASD_ECKD_CCW_WRITE_KD_MT:
 402                data->auxiliary.length_valid = 0x1;
 403                data->length = reclen;
 404                data->operation.operation = 0x01;
 405                break;
 406        case DASD_ECKD_CCW_WRITE_CKD:
 407        case DASD_ECKD_CCW_WRITE_CKD_MT:
 408                data->auxiliary.length_valid = 0x1;
 409                data->length = reclen;
 410                data->operation.operation = 0x03;
 411                break;
 412        case DASD_ECKD_CCW_WRITE_FULL_TRACK:
 413                data->operation.orientation = 0x0;
 414                data->operation.operation = 0x3F;
 415                data->extended_operation = 0x11;
 416                data->length = 0;
 417                data->extended_parameter_length = 0x02;
 418                if (data->count > 8) {
 419                        data->extended_parameter[0] = 0xFF;
 420                        data->extended_parameter[1] = 0xFF;
 421                        data->extended_parameter[1] <<= (16 - count);
 422                } else {
 423                        data->extended_parameter[0] = 0xFF;
 424                        data->extended_parameter[0] <<= (8 - count);
 425                        data->extended_parameter[1] = 0x00;
 426                }
 427                data->sector = 0xFF;
 428                break;
 429        case DASD_ECKD_CCW_WRITE_TRACK_DATA:
 430                data->auxiliary.length_valid = 0x1;
 431                data->length = reclen;  /* not tlf, as one might think */
 432                data->operation.operation = 0x3F;
 433                data->extended_operation = 0x23;
 434                break;
 435        case DASD_ECKD_CCW_READ:
 436        case DASD_ECKD_CCW_READ_MT:
 437        case DASD_ECKD_CCW_READ_KD:
 438        case DASD_ECKD_CCW_READ_KD_MT:
 439                data->auxiliary.length_valid = 0x1;
 440                data->length = reclen;
 441                data->operation.operation = 0x06;
 442                break;
 443        case DASD_ECKD_CCW_READ_CKD:
 444        case DASD_ECKD_CCW_READ_CKD_MT:
 445                data->auxiliary.length_valid = 0x1;
 446                data->length = reclen;
 447                data->operation.operation = 0x16;
 448                break;
 449        case DASD_ECKD_CCW_READ_COUNT:
 450                data->operation.operation = 0x06;
 451                break;
 452        case DASD_ECKD_CCW_READ_TRACK:
 453                data->operation.orientation = 0x1;
 454                data->operation.operation = 0x0C;
 455                data->extended_parameter_length = 0;
 456                data->sector = 0xFF;
 457                break;
 458        case DASD_ECKD_CCW_READ_TRACK_DATA:
 459                data->auxiliary.length_valid = 0x1;
 460                data->length = tlf;
 461                data->operation.operation = 0x0C;
 462                break;
 463        case DASD_ECKD_CCW_ERASE:
 464                data->length = reclen;
 465                data->auxiliary.length_valid = 0x1;
 466                data->operation.operation = 0x0b;
 467                break;
 468        default:
 469                DBF_DEV_EVENT(DBF_ERR, device,
 470                            "fill LRE unknown opcode 0x%x", cmd);
 471                BUG();
 472        }
 473        set_ch_t(&data->seek_addr,
 474                 trk / private->rdc_data.trk_per_cyl,
 475                 trk % private->rdc_data.trk_per_cyl);
 476        data->search_arg.cyl = data->seek_addr.cyl;
 477        data->search_arg.head = data->seek_addr.head;
 478        data->search_arg.record = rec_on_trk;
 479}
 480
 481static int prefix_LRE(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
 482                      unsigned int trk, unsigned int totrk, int cmd,
 483                      struct dasd_device *basedev, struct dasd_device *startdev,
 484                      unsigned char format, unsigned int rec_on_trk, int count,
 485                      unsigned int blksize, unsigned int tlf)
 486{
 487        struct dasd_eckd_private *basepriv, *startpriv;
 488        struct DE_eckd_data *dedata;
 489        struct LRE_eckd_data *lredata;
 490        u32 begcyl, endcyl;
 491        u16 heads, beghead, endhead;
 492        int rc = 0;
 493
 494        basepriv = basedev->private;
 495        startpriv = startdev->private;
 496        dedata = &pfxdata->define_extent;
 497        lredata = &pfxdata->locate_record;
 498
 499        ccw->cmd_code = DASD_ECKD_CCW_PFX;
 500        ccw->flags = 0;
 501        if (cmd == DASD_ECKD_CCW_WRITE_FULL_TRACK) {
 502                ccw->count = sizeof(*pfxdata) + 2;
 503                ccw->cda = (__u32) __pa(pfxdata);
 504                memset(pfxdata, 0, sizeof(*pfxdata) + 2);
 505        } else {
 506                ccw->count = sizeof(*pfxdata);
 507                ccw->cda = (__u32) __pa(pfxdata);
 508                memset(pfxdata, 0, sizeof(*pfxdata));
 509        }
 510
 511        /* prefix data */
 512        if (format > 1) {
 513                DBF_DEV_EVENT(DBF_ERR, basedev,
 514                              "PFX LRE unknown format 0x%x", format);
 515                BUG();
 516                return -EINVAL;
 517        }
 518        pfxdata->format = format;
 519        pfxdata->base_address = basepriv->ned->unit_addr;
 520        pfxdata->base_lss = basepriv->ned->ID;
 521        pfxdata->validity.define_extent = 1;
 522
 523        /* private uid is kept up to date, conf_data may be outdated */
 524        if (startpriv->uid.type != UA_BASE_DEVICE) {
 525                pfxdata->validity.verify_base = 1;
 526                if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
 527                        pfxdata->validity.hyper_pav = 1;
 528        }
 529
 530        /* define extend data (mostly)*/
 531        switch (cmd) {
 532        case DASD_ECKD_CCW_READ_HOME_ADDRESS:
 533        case DASD_ECKD_CCW_READ_RECORD_ZERO:
 534        case DASD_ECKD_CCW_READ:
 535        case DASD_ECKD_CCW_READ_MT:
 536        case DASD_ECKD_CCW_READ_CKD:
 537        case DASD_ECKD_CCW_READ_CKD_MT:
 538        case DASD_ECKD_CCW_READ_KD:
 539        case DASD_ECKD_CCW_READ_KD_MT:
 540                dedata->mask.perm = 0x1;
 541                dedata->attributes.operation = basepriv->attrib.operation;
 542                break;
 543        case DASD_ECKD_CCW_READ_COUNT:
 544                dedata->mask.perm = 0x1;
 545                dedata->attributes.operation = DASD_BYPASS_CACHE;
 546                break;
 547        case DASD_ECKD_CCW_READ_TRACK:
 548        case DASD_ECKD_CCW_READ_TRACK_DATA:
 549                dedata->mask.perm = 0x1;
 550                dedata->attributes.operation = basepriv->attrib.operation;
 551                dedata->blk_size = 0;
 552                break;
 553        case DASD_ECKD_CCW_WRITE:
 554        case DASD_ECKD_CCW_WRITE_MT:
 555        case DASD_ECKD_CCW_WRITE_KD:
 556        case DASD_ECKD_CCW_WRITE_KD_MT:
 557                dedata->mask.perm = 0x02;
 558                dedata->attributes.operation = basepriv->attrib.operation;
 559                rc = check_XRC_on_prefix(pfxdata, basedev);
 560                break;
 561        case DASD_ECKD_CCW_WRITE_CKD:
 562        case DASD_ECKD_CCW_WRITE_CKD_MT:
 563                dedata->attributes.operation = DASD_BYPASS_CACHE;
 564                rc = check_XRC_on_prefix(pfxdata, basedev);
 565                break;
 566        case DASD_ECKD_CCW_ERASE:
 567        case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
 568        case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
 569                dedata->mask.perm = 0x3;
 570                dedata->mask.auth = 0x1;
 571                dedata->attributes.operation = DASD_BYPASS_CACHE;
 572                rc = check_XRC_on_prefix(pfxdata, basedev);
 573                break;
 574        case DASD_ECKD_CCW_WRITE_FULL_TRACK:
 575                dedata->mask.perm = 0x03;
 576                dedata->attributes.operation = basepriv->attrib.operation;
 577                dedata->blk_size = 0;
 578                break;
 579        case DASD_ECKD_CCW_WRITE_TRACK_DATA:
 580                dedata->mask.perm = 0x02;
 581                dedata->attributes.operation = basepriv->attrib.operation;
 582                dedata->blk_size = blksize;
 583                rc = check_XRC_on_prefix(pfxdata, basedev);
 584                break;
 585        default:
 586                DBF_DEV_EVENT(DBF_ERR, basedev,
 587                            "PFX LRE unknown opcode 0x%x", cmd);
 588                BUG();
 589                return -EINVAL;
 590        }
 591
 592        dedata->attributes.mode = 0x3;  /* ECKD */
 593
 594        if ((basepriv->rdc_data.cu_type == 0x2105 ||
 595             basepriv->rdc_data.cu_type == 0x2107 ||
 596             basepriv->rdc_data.cu_type == 0x1750)
 597            && !(basepriv->uses_cdl && trk < 2))
 598                dedata->ga_extended |= 0x40; /* Regular Data Format Mode */
 599
 600        heads = basepriv->rdc_data.trk_per_cyl;
 601        begcyl = trk / heads;
 602        beghead = trk % heads;
 603        endcyl = totrk / heads;
 604        endhead = totrk % heads;
 605
 606        /* check for sequential prestage - enhance cylinder range */
 607        if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
 608            dedata->attributes.operation == DASD_SEQ_ACCESS) {
 609
 610                if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
 611                        endcyl += basepriv->attrib.nr_cyl;
 612                else
 613                        endcyl = (basepriv->real_cyl - 1);
 614        }
 615
 616        set_ch_t(&dedata->beg_ext, begcyl, beghead);
 617        set_ch_t(&dedata->end_ext, endcyl, endhead);
 618
 619        if (format == 1) {
 620                fill_LRE_data(lredata, trk, rec_on_trk, count, cmd,
 621                              basedev, blksize, tlf);
 622        }
 623
 624        return rc;
 625}
 626
 627static int prefix(struct ccw1 *ccw, struct PFX_eckd_data *pfxdata,
 628                  unsigned int trk, unsigned int totrk, int cmd,
 629                  struct dasd_device *basedev, struct dasd_device *startdev)
 630{
 631        return prefix_LRE(ccw, pfxdata, trk, totrk, cmd, basedev, startdev,
 632                          0, 0, 0, 0, 0);
 633}
 634
 635static void
 636locate_record(struct ccw1 *ccw, struct LO_eckd_data *data, unsigned int trk,
 637              unsigned int rec_on_trk, int no_rec, int cmd,
 638              struct dasd_device * device, int reclen)
 639{
 640        struct dasd_eckd_private *private = device->private;
 641        int sector;
 642        int dn, d;
 643
 644        DBF_DEV_EVENT(DBF_INFO, device,
 645                  "Locate: trk %d, rec %d, no_rec %d, cmd %d, reclen %d",
 646                  trk, rec_on_trk, no_rec, cmd, reclen);
 647
 648        ccw->cmd_code = DASD_ECKD_CCW_LOCATE_RECORD;
 649        ccw->flags = 0;
 650        ccw->count = 16;
 651        ccw->cda = (__u32) __pa(data);
 652
 653        memset(data, 0, sizeof(struct LO_eckd_data));
 654        sector = 0;
 655        if (rec_on_trk) {
 656                switch (private->rdc_data.dev_type) {
 657                case 0x3390:
 658                        dn = ceil_quot(reclen + 6, 232);
 659                        d = 9 + ceil_quot(reclen + 6 * (dn + 1), 34);
 660                        sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
 661                        break;
 662                case 0x3380:
 663                        d = 7 + ceil_quot(reclen + 12, 32);
 664                        sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
 665                        break;
 666                }
 667        }
 668        data->sector = sector;
 669        data->count = no_rec;
 670        switch (cmd) {
 671        case DASD_ECKD_CCW_WRITE_HOME_ADDRESS:
 672                data->operation.orientation = 0x3;
 673                data->operation.operation = 0x03;
 674                break;
 675        case DASD_ECKD_CCW_READ_HOME_ADDRESS:
 676                data->operation.orientation = 0x3;
 677                data->operation.operation = 0x16;
 678                break;
 679        case DASD_ECKD_CCW_WRITE_RECORD_ZERO:
 680                data->operation.orientation = 0x1;
 681                data->operation.operation = 0x03;
 682                data->count++;
 683                break;
 684        case DASD_ECKD_CCW_READ_RECORD_ZERO:
 685                data->operation.orientation = 0x3;
 686                data->operation.operation = 0x16;
 687                data->count++;
 688                break;
 689        case DASD_ECKD_CCW_WRITE:
 690        case DASD_ECKD_CCW_WRITE_MT:
 691        case DASD_ECKD_CCW_WRITE_KD:
 692        case DASD_ECKD_CCW_WRITE_KD_MT:
 693                data->auxiliary.last_bytes_used = 0x1;
 694                data->length = reclen;
 695                data->operation.operation = 0x01;
 696                break;
 697        case DASD_ECKD_CCW_WRITE_CKD:
 698        case DASD_ECKD_CCW_WRITE_CKD_MT:
 699                data->auxiliary.last_bytes_used = 0x1;
 700                data->length = reclen;
 701                data->operation.operation = 0x03;
 702                break;
 703        case DASD_ECKD_CCW_READ:
 704        case DASD_ECKD_CCW_READ_MT:
 705        case DASD_ECKD_CCW_READ_KD:
 706        case DASD_ECKD_CCW_READ_KD_MT:
 707                data->auxiliary.last_bytes_used = 0x1;
 708                data->length = reclen;
 709                data->operation.operation = 0x06;
 710                break;
 711        case DASD_ECKD_CCW_READ_CKD:
 712        case DASD_ECKD_CCW_READ_CKD_MT:
 713                data->auxiliary.last_bytes_used = 0x1;
 714                data->length = reclen;
 715                data->operation.operation = 0x16;
 716                break;
 717        case DASD_ECKD_CCW_READ_COUNT:
 718                data->operation.operation = 0x06;
 719                break;
 720        case DASD_ECKD_CCW_ERASE:
 721                data->length = reclen;
 722                data->auxiliary.last_bytes_used = 0x1;
 723                data->operation.operation = 0x0b;
 724                break;
 725        default:
 726                DBF_DEV_EVENT(DBF_ERR, device, "unknown locate record "
 727                              "opcode 0x%x", cmd);
 728        }
 729        set_ch_t(&data->seek_addr,
 730                 trk / private->rdc_data.trk_per_cyl,
 731                 trk % private->rdc_data.trk_per_cyl);
 732        data->search_arg.cyl = data->seek_addr.cyl;
 733        data->search_arg.head = data->seek_addr.head;
 734        data->search_arg.record = rec_on_trk;
 735}
 736
 737/*
 738 * Returns 1 if the block is one of the special blocks that needs
 739 * to get read/written with the KD variant of the command.
 740 * That is DASD_ECKD_READ_KD_MT instead of DASD_ECKD_READ_MT and
 741 * DASD_ECKD_WRITE_KD_MT instead of DASD_ECKD_WRITE_MT.
 742 * Luckily the KD variants differ only by one bit (0x08) from the
 743 * normal variant. So don't wonder about code like:
 744 * if (dasd_eckd_cdl_special(blk_per_trk, recid))
 745 *         ccw->cmd_code |= 0x8;
 746 */
 747static inline int
 748dasd_eckd_cdl_special(int blk_per_trk, int recid)
 749{
 750        if (recid < 3)
 751                return 1;
 752        if (recid < blk_per_trk)
 753                return 0;
 754        if (recid < 2 * blk_per_trk)
 755                return 1;
 756        return 0;
 757}
 758
 759/*
 760 * Returns the record size for the special blocks of the cdl format.
 761 * Only returns something useful if dasd_eckd_cdl_special is true
 762 * for the recid.
 763 */
 764static inline int
 765dasd_eckd_cdl_reclen(int recid)
 766{
 767        if (recid < 3)
 768                return sizes_trk0[recid];
 769        return LABEL_SIZE;
 770}
 771/* create unique id from private structure. */
 772static void create_uid(struct dasd_eckd_private *private)
 773{
 774        int count;
 775        struct dasd_uid *uid;
 776
 777        uid = &private->uid;
 778        memset(uid, 0, sizeof(struct dasd_uid));
 779        memcpy(uid->vendor, private->ned->HDA_manufacturer,
 780               sizeof(uid->vendor) - 1);
 781        EBCASC(uid->vendor, sizeof(uid->vendor) - 1);
 782        memcpy(uid->serial, private->ned->HDA_location,
 783               sizeof(uid->serial) - 1);
 784        EBCASC(uid->serial, sizeof(uid->serial) - 1);
 785        uid->ssid = private->gneq->subsystemID;
 786        uid->real_unit_addr = private->ned->unit_addr;
 787        if (private->sneq) {
 788                uid->type = private->sneq->sua_flags;
 789                if (uid->type == UA_BASE_PAV_ALIAS)
 790                        uid->base_unit_addr = private->sneq->base_unit_addr;
 791        } else {
 792                uid->type = UA_BASE_DEVICE;
 793        }
 794        if (private->vdsneq) {
 795                for (count = 0; count < 16; count++) {
 796                        sprintf(uid->vduit+2*count, "%02x",
 797                                private->vdsneq->uit[count]);
 798                }
 799        }
 800}
 801
 802/*
 803 * Generate device unique id that specifies the physical device.
 804 */
 805static int dasd_eckd_generate_uid(struct dasd_device *device)
 806{
 807        struct dasd_eckd_private *private = device->private;
 808        unsigned long flags;
 809
 810        if (!private)
 811                return -ENODEV;
 812        if (!private->ned || !private->gneq)
 813                return -ENODEV;
 814        spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
 815        create_uid(private);
 816        spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
 817        return 0;
 818}
 819
 820static int dasd_eckd_get_uid(struct dasd_device *device, struct dasd_uid *uid)
 821{
 822        struct dasd_eckd_private *private = device->private;
 823        unsigned long flags;
 824
 825        if (private) {
 826                spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
 827                *uid = private->uid;
 828                spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
 829                return 0;
 830        }
 831        return -EINVAL;
 832}
 833
 834/*
 835 * compare device UID with data of a given dasd_eckd_private structure
 836 * return 0 for match
 837 */
 838static int dasd_eckd_compare_path_uid(struct dasd_device *device,
 839                                      struct dasd_eckd_private *private)
 840{
 841        struct dasd_uid device_uid;
 842
 843        create_uid(private);
 844        dasd_eckd_get_uid(device, &device_uid);
 845
 846        return memcmp(&device_uid, &private->uid, sizeof(struct dasd_uid));
 847}
 848
 849static void dasd_eckd_fill_rcd_cqr(struct dasd_device *device,
 850                                   struct dasd_ccw_req *cqr,
 851                                   __u8 *rcd_buffer,
 852                                   __u8 lpm)
 853{
 854        struct ccw1 *ccw;
 855        /*
 856         * buffer has to start with EBCDIC "V1.0" to show
 857         * support for virtual device SNEQ
 858         */
 859        rcd_buffer[0] = 0xE5;
 860        rcd_buffer[1] = 0xF1;
 861        rcd_buffer[2] = 0x4B;
 862        rcd_buffer[3] = 0xF0;
 863
 864        ccw = cqr->cpaddr;
 865        ccw->cmd_code = DASD_ECKD_CCW_RCD;
 866        ccw->flags = 0;
 867        ccw->cda = (__u32)(addr_t)rcd_buffer;
 868        ccw->count = DASD_ECKD_RCD_DATA_SIZE;
 869        cqr->magic = DASD_ECKD_MAGIC;
 870
 871        cqr->startdev = device;
 872        cqr->memdev = device;
 873        cqr->block = NULL;
 874        cqr->expires = 10*HZ;
 875        cqr->lpm = lpm;
 876        cqr->retries = 256;
 877        cqr->buildclk = get_tod_clock();
 878        cqr->status = DASD_CQR_FILLED;
 879        set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
 880}
 881
 882/*
 883 * Wakeup helper for read_conf
 884 * if the cqr is not done and needs some error recovery
 885 * the buffer has to be re-initialized with the EBCDIC "V1.0"
 886 * to show support for virtual device SNEQ
 887 */
 888static void read_conf_cb(struct dasd_ccw_req *cqr, void *data)
 889{
 890        struct ccw1 *ccw;
 891        __u8 *rcd_buffer;
 892
 893        if (cqr->status !=  DASD_CQR_DONE) {
 894                ccw = cqr->cpaddr;
 895                rcd_buffer = (__u8 *)((addr_t) ccw->cda);
 896                memset(rcd_buffer, 0, sizeof(*rcd_buffer));
 897
 898                rcd_buffer[0] = 0xE5;
 899                rcd_buffer[1] = 0xF1;
 900                rcd_buffer[2] = 0x4B;
 901                rcd_buffer[3] = 0xF0;
 902        }
 903        dasd_wakeup_cb(cqr, data);
 904}
 905
 906static int dasd_eckd_read_conf_immediately(struct dasd_device *device,
 907                                           struct dasd_ccw_req *cqr,
 908                                           __u8 *rcd_buffer,
 909                                           __u8 lpm)
 910{
 911        struct ciw *ciw;
 912        int rc;
 913        /*
 914         * sanity check: scan for RCD command in extended SenseID data
 915         * some devices do not support RCD
 916         */
 917        ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
 918        if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD)
 919                return -EOPNOTSUPP;
 920
 921        dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buffer, lpm);
 922        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
 923        set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
 924        cqr->retries = 5;
 925        cqr->callback = read_conf_cb;
 926        rc = dasd_sleep_on_immediatly(cqr);
 927        return rc;
 928}
 929
 930static int dasd_eckd_read_conf_lpm(struct dasd_device *device,
 931                                   void **rcd_buffer,
 932                                   int *rcd_buffer_size, __u8 lpm)
 933{
 934        struct ciw *ciw;
 935        char *rcd_buf = NULL;
 936        int ret;
 937        struct dasd_ccw_req *cqr;
 938
 939        /*
 940         * sanity check: scan for RCD command in extended SenseID data
 941         * some devices do not support RCD
 942         */
 943        ciw = ccw_device_get_ciw(device->cdev, CIW_TYPE_RCD);
 944        if (!ciw || ciw->cmd != DASD_ECKD_CCW_RCD) {
 945                ret = -EOPNOTSUPP;
 946                goto out_error;
 947        }
 948        rcd_buf = kzalloc(DASD_ECKD_RCD_DATA_SIZE, GFP_KERNEL | GFP_DMA);
 949        if (!rcd_buf) {
 950                ret = -ENOMEM;
 951                goto out_error;
 952        }
 953        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* RCD */,
 954                                   0, /* use rcd_buf as data ara */
 955                                   device);
 956        if (IS_ERR(cqr)) {
 957                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
 958                              "Could not allocate RCD request");
 959                ret = -ENOMEM;
 960                goto out_error;
 961        }
 962        dasd_eckd_fill_rcd_cqr(device, cqr, rcd_buf, lpm);
 963        cqr->callback = read_conf_cb;
 964        ret = dasd_sleep_on(cqr);
 965        /*
 966         * on success we update the user input parms
 967         */
 968        dasd_sfree_request(cqr, cqr->memdev);
 969        if (ret)
 970                goto out_error;
 971
 972        *rcd_buffer_size = DASD_ECKD_RCD_DATA_SIZE;
 973        *rcd_buffer = rcd_buf;
 974        return 0;
 975out_error:
 976        kfree(rcd_buf);
 977        *rcd_buffer = NULL;
 978        *rcd_buffer_size = 0;
 979        return ret;
 980}
 981
 982static int dasd_eckd_identify_conf_parts(struct dasd_eckd_private *private)
 983{
 984
 985        struct dasd_sneq *sneq;
 986        int i, count;
 987
 988        private->ned = NULL;
 989        private->sneq = NULL;
 990        private->vdsneq = NULL;
 991        private->gneq = NULL;
 992        count = private->conf_len / sizeof(struct dasd_sneq);
 993        sneq = (struct dasd_sneq *)private->conf_data;
 994        for (i = 0; i < count; ++i) {
 995                if (sneq->flags.identifier == 1 && sneq->format == 1)
 996                        private->sneq = sneq;
 997                else if (sneq->flags.identifier == 1 && sneq->format == 4)
 998                        private->vdsneq = (struct vd_sneq *)sneq;
 999                else if (sneq->flags.identifier == 2)
1000                        private->gneq = (struct dasd_gneq *)sneq;
1001                else if (sneq->flags.identifier == 3 && sneq->res1 == 1)
1002                        private->ned = (struct dasd_ned *)sneq;
1003                sneq++;
1004        }
1005        if (!private->ned || !private->gneq) {
1006                private->ned = NULL;
1007                private->sneq = NULL;
1008                private->vdsneq = NULL;
1009                private->gneq = NULL;
1010                return -EINVAL;
1011        }
1012        return 0;
1013
1014};
1015
1016static unsigned char dasd_eckd_path_access(void *conf_data, int conf_len)
1017{
1018        struct dasd_gneq *gneq;
1019        int i, count, found;
1020
1021        count = conf_len / sizeof(*gneq);
1022        gneq = (struct dasd_gneq *)conf_data;
1023        found = 0;
1024        for (i = 0; i < count; ++i) {
1025                if (gneq->flags.identifier == 2) {
1026                        found = 1;
1027                        break;
1028                }
1029                gneq++;
1030        }
1031        if (found)
1032                return ((char *)gneq)[18] & 0x07;
1033        else
1034                return 0;
1035}
1036
1037static void dasd_eckd_clear_conf_data(struct dasd_device *device)
1038{
1039        struct dasd_eckd_private *private = device->private;
1040        int i;
1041
1042        private->conf_data = NULL;
1043        private->conf_len = 0;
1044        for (i = 0; i < 8; i++) {
1045                kfree(private->path_conf_data[i]);
1046                private->path_conf_data[i] = NULL;
1047        }
1048}
1049
1050
1051static int dasd_eckd_read_conf(struct dasd_device *device)
1052{
1053        void *conf_data;
1054        int conf_len, conf_data_saved;
1055        int rc, path_err, pos;
1056        __u8 lpm, opm;
1057        struct dasd_eckd_private *private, path_private;
1058        struct dasd_path *path_data;
1059        struct dasd_uid *uid;
1060        char print_path_uid[60], print_device_uid[60];
1061
1062        private = device->private;
1063        path_data = &device->path_data;
1064        opm = ccw_device_get_path_mask(device->cdev);
1065        conf_data_saved = 0;
1066        path_err = 0;
1067        /* get configuration data per operational path */
1068        for (lpm = 0x80; lpm; lpm>>= 1) {
1069                if (!(lpm & opm))
1070                        continue;
1071                rc = dasd_eckd_read_conf_lpm(device, &conf_data,
1072                                             &conf_len, lpm);
1073                if (rc && rc != -EOPNOTSUPP) {  /* -EOPNOTSUPP is ok */
1074                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1075                                        "Read configuration data returned "
1076                                        "error %d", rc);
1077                        return rc;
1078                }
1079                if (conf_data == NULL) {
1080                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1081                                        "No configuration data "
1082                                        "retrieved");
1083                        /* no further analysis possible */
1084                        path_data->opm |= lpm;
1085                        continue;       /* no error */
1086                }
1087                /* save first valid configuration data */
1088                if (!conf_data_saved) {
1089                        /* initially clear previously stored conf_data */
1090                        dasd_eckd_clear_conf_data(device);
1091                        private->conf_data = conf_data;
1092                        private->conf_len = conf_len;
1093                        if (dasd_eckd_identify_conf_parts(private)) {
1094                                private->conf_data = NULL;
1095                                private->conf_len = 0;
1096                                kfree(conf_data);
1097                                continue;
1098                        }
1099                        pos = pathmask_to_pos(lpm);
1100                        /* store per path conf_data */
1101                        private->path_conf_data[pos] =
1102                                (struct dasd_conf_data *) conf_data;
1103                        /*
1104                         * build device UID that other path data
1105                         * can be compared to it
1106                         */
1107                        dasd_eckd_generate_uid(device);
1108                        conf_data_saved++;
1109                } else {
1110                        path_private.conf_data = conf_data;
1111                        path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1112                        if (dasd_eckd_identify_conf_parts(
1113                                    &path_private)) {
1114                                path_private.conf_data = NULL;
1115                                path_private.conf_len = 0;
1116                                kfree(conf_data);
1117                                continue;
1118                        }
1119                        if (dasd_eckd_compare_path_uid(
1120                                    device, &path_private)) {
1121                                uid = &path_private.uid;
1122                                if (strlen(uid->vduit) > 0)
1123                                        snprintf(print_path_uid,
1124                                                 sizeof(print_path_uid),
1125                                                 "%s.%s.%04x.%02x.%s",
1126                                                 uid->vendor, uid->serial,
1127                                                 uid->ssid, uid->real_unit_addr,
1128                                                 uid->vduit);
1129                                else
1130                                        snprintf(print_path_uid,
1131                                                 sizeof(print_path_uid),
1132                                                 "%s.%s.%04x.%02x",
1133                                                 uid->vendor, uid->serial,
1134                                                 uid->ssid,
1135                                                 uid->real_unit_addr);
1136                                uid = &private->uid;
1137                                if (strlen(uid->vduit) > 0)
1138                                        snprintf(print_device_uid,
1139                                                 sizeof(print_device_uid),
1140                                                 "%s.%s.%04x.%02x.%s",
1141                                                 uid->vendor, uid->serial,
1142                                                 uid->ssid, uid->real_unit_addr,
1143                                                 uid->vduit);
1144                                else
1145                                        snprintf(print_device_uid,
1146                                                 sizeof(print_device_uid),
1147                                                 "%s.%s.%04x.%02x",
1148                                                 uid->vendor, uid->serial,
1149                                                 uid->ssid,
1150                                                 uid->real_unit_addr);
1151                                dev_err(&device->cdev->dev,
1152                                        "Not all channel paths lead to "
1153                                        "the same device, path %02X leads to "
1154                                        "device %s instead of %s\n", lpm,
1155                                        print_path_uid, print_device_uid);
1156                                path_err = -EINVAL;
1157                                path_data->cablepm |= lpm;
1158                                continue;
1159                        }
1160                        pos = pathmask_to_pos(lpm);
1161                        /* store per path conf_data */
1162                        private->path_conf_data[pos] =
1163                                (struct dasd_conf_data *) conf_data;
1164                        path_private.conf_data = NULL;
1165                        path_private.conf_len = 0;
1166                }
1167                switch (dasd_eckd_path_access(conf_data, conf_len)) {
1168                case 0x02:
1169                        path_data->npm |= lpm;
1170                        break;
1171                case 0x03:
1172                        path_data->ppm |= lpm;
1173                        break;
1174                }
1175                if (!path_data->opm) {
1176                        path_data->opm = lpm;
1177                        dasd_generic_path_operational(device);
1178                } else {
1179                        path_data->opm |= lpm;
1180                }
1181                /*
1182                 * if the path is used
1183                 * it should not be in one of the negative lists
1184                 */
1185                path_data->cablepm &= ~lpm;
1186                path_data->hpfpm &= ~lpm;
1187                path_data->cuirpm &= ~lpm;
1188        }
1189
1190        return path_err;
1191}
1192
1193static int verify_fcx_max_data(struct dasd_device *device, __u8 lpm)
1194{
1195        struct dasd_eckd_private *private = device->private;
1196        int mdc;
1197        u32 fcx_max_data;
1198
1199        if (private->fcx_max_data) {
1200                mdc = ccw_device_get_mdc(device->cdev, lpm);
1201                if ((mdc < 0)) {
1202                        dev_warn(&device->cdev->dev,
1203                                 "Detecting the maximum data size for zHPF "
1204                                 "requests failed (rc=%d) for a new path %x\n",
1205                                 mdc, lpm);
1206                        return mdc;
1207                }
1208                fcx_max_data = mdc * FCX_MAX_DATA_FACTOR;
1209                if (fcx_max_data < private->fcx_max_data) {
1210                        dev_warn(&device->cdev->dev,
1211                                 "The maximum data size for zHPF requests %u "
1212                                 "on a new path %x is below the active maximum "
1213                                 "%u\n", fcx_max_data, lpm,
1214                                 private->fcx_max_data);
1215                        return -EACCES;
1216                }
1217        }
1218        return 0;
1219}
1220
1221static int rebuild_device_uid(struct dasd_device *device,
1222                              struct path_verification_work_data *data)
1223{
1224        struct dasd_eckd_private *private = device->private;
1225        struct dasd_path *path_data = &device->path_data;
1226        __u8 lpm, opm = path_data->opm;
1227        int rc = -ENODEV;
1228
1229        for (lpm = 0x80; lpm; lpm >>= 1) {
1230                if (!(lpm & opm))
1231                        continue;
1232                memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1233                memset(&data->cqr, 0, sizeof(data->cqr));
1234                data->cqr.cpaddr = &data->ccw;
1235                rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1236                                                     data->rcd_buffer,
1237                                                     lpm);
1238
1239                if (rc) {
1240                        if (rc == -EOPNOTSUPP) /* -EOPNOTSUPP is ok */
1241                                continue;
1242                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1243                                        "Read configuration data "
1244                                        "returned error %d", rc);
1245                        break;
1246                }
1247                memcpy(private->conf_data, data->rcd_buffer,
1248                       DASD_ECKD_RCD_DATA_SIZE);
1249                if (dasd_eckd_identify_conf_parts(private)) {
1250                        rc = -ENODEV;
1251                } else /* first valid path is enough */
1252                        break;
1253        }
1254
1255        if (!rc)
1256                rc = dasd_eckd_generate_uid(device);
1257
1258        return rc;
1259}
1260
1261static void do_path_verification_work(struct work_struct *work)
1262{
1263        struct path_verification_work_data *data;
1264        struct dasd_device *device;
1265        struct dasd_eckd_private path_private;
1266        struct dasd_uid *uid;
1267        __u8 path_rcd_buf[DASD_ECKD_RCD_DATA_SIZE];
1268        __u8 lpm, opm, npm, ppm, epm, hpfpm, cablepm;
1269        unsigned long flags;
1270        char print_uid[60];
1271        int rc;
1272
1273        data = container_of(work, struct path_verification_work_data, worker);
1274        device = data->device;
1275
1276        /* delay path verification until device was resumed */
1277        if (test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
1278                schedule_work(work);
1279                return;
1280        }
1281        /* check if path verification already running and delay if so */
1282        if (test_and_set_bit(DASD_FLAG_PATH_VERIFY, &device->flags)) {
1283                schedule_work(work);
1284                return;
1285        }
1286        opm = 0;
1287        npm = 0;
1288        ppm = 0;
1289        epm = 0;
1290        hpfpm = 0;
1291        cablepm = 0;
1292
1293        for (lpm = 0x80; lpm; lpm >>= 1) {
1294                if (!(lpm & data->tbvpm))
1295                        continue;
1296                memset(&data->rcd_buffer, 0, sizeof(data->rcd_buffer));
1297                memset(&data->cqr, 0, sizeof(data->cqr));
1298                data->cqr.cpaddr = &data->ccw;
1299                rc = dasd_eckd_read_conf_immediately(device, &data->cqr,
1300                                                     data->rcd_buffer,
1301                                                     lpm);
1302                if (!rc) {
1303                        switch (dasd_eckd_path_access(data->rcd_buffer,
1304                                                      DASD_ECKD_RCD_DATA_SIZE)
1305                                ) {
1306                        case 0x02:
1307                                npm |= lpm;
1308                                break;
1309                        case 0x03:
1310                                ppm |= lpm;
1311                                break;
1312                        }
1313                        opm |= lpm;
1314                } else if (rc == -EOPNOTSUPP) {
1315                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1316                                        "path verification: No configuration "
1317                                        "data retrieved");
1318                        opm |= lpm;
1319                } else if (rc == -EAGAIN) {
1320                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1321                                        "path verification: device is stopped,"
1322                                        " try again later");
1323                        epm |= lpm;
1324                } else {
1325                        dev_warn(&device->cdev->dev,
1326                                 "Reading device feature codes failed "
1327                                 "(rc=%d) for new path %x\n", rc, lpm);
1328                        continue;
1329                }
1330                if (verify_fcx_max_data(device, lpm)) {
1331                        opm &= ~lpm;
1332                        npm &= ~lpm;
1333                        ppm &= ~lpm;
1334                        hpfpm |= lpm;
1335                        continue;
1336                }
1337
1338                /*
1339                 * save conf_data for comparison after
1340                 * rebuild_device_uid may have changed
1341                 * the original data
1342                 */
1343                memcpy(&path_rcd_buf, data->rcd_buffer,
1344                       DASD_ECKD_RCD_DATA_SIZE);
1345                path_private.conf_data = (void *) &path_rcd_buf;
1346                path_private.conf_len = DASD_ECKD_RCD_DATA_SIZE;
1347                if (dasd_eckd_identify_conf_parts(&path_private)) {
1348                        path_private.conf_data = NULL;
1349                        path_private.conf_len = 0;
1350                        continue;
1351                }
1352
1353                /*
1354                 * compare path UID with device UID only if at least
1355                 * one valid path is left
1356                 * in other case the device UID may have changed and
1357                 * the first working path UID will be used as device UID
1358                 */
1359                if (device->path_data.opm &&
1360                    dasd_eckd_compare_path_uid(device, &path_private)) {
1361                        /*
1362                         * the comparison was not successful
1363                         * rebuild the device UID with at least one
1364                         * known path in case a z/VM hyperswap command
1365                         * has changed the device
1366                         *
1367                         * after this compare again
1368                         *
1369                         * if either the rebuild or the recompare fails
1370                         * the path can not be used
1371                         */
1372                        if (rebuild_device_uid(device, data) ||
1373                            dasd_eckd_compare_path_uid(
1374                                    device, &path_private)) {
1375                                uid = &path_private.uid;
1376                                if (strlen(uid->vduit) > 0)
1377                                        snprintf(print_uid, sizeof(print_uid),
1378                                                 "%s.%s.%04x.%02x.%s",
1379                                                 uid->vendor, uid->serial,
1380                                                 uid->ssid, uid->real_unit_addr,
1381                                                 uid->vduit);
1382                                else
1383                                        snprintf(print_uid, sizeof(print_uid),
1384                                                 "%s.%s.%04x.%02x",
1385                                                 uid->vendor, uid->serial,
1386                                                 uid->ssid,
1387                                                 uid->real_unit_addr);
1388                                dev_err(&device->cdev->dev,
1389                                        "The newly added channel path %02X "
1390                                        "will not be used because it leads "
1391                                        "to a different device %s\n",
1392                                        lpm, print_uid);
1393                                opm &= ~lpm;
1394                                npm &= ~lpm;
1395                                ppm &= ~lpm;
1396                                cablepm |= lpm;
1397                                continue;
1398                        }
1399                }
1400
1401                /*
1402                 * There is a small chance that a path is lost again between
1403                 * above path verification and the following modification of
1404                 * the device opm mask. We could avoid that race here by using
1405                 * yet another path mask, but we rather deal with this unlikely
1406                 * situation in dasd_start_IO.
1407                 */
1408                spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1409                if (!device->path_data.opm && opm) {
1410                        device->path_data.opm = opm;
1411                        device->path_data.cablepm &= ~opm;
1412                        device->path_data.cuirpm &= ~opm;
1413                        device->path_data.hpfpm &= ~opm;
1414                        dasd_generic_path_operational(device);
1415                } else {
1416                        device->path_data.opm |= opm;
1417                        device->path_data.cablepm &= ~opm;
1418                        device->path_data.cuirpm &= ~opm;
1419                        device->path_data.hpfpm &= ~opm;
1420                }
1421                device->path_data.npm |= npm;
1422                device->path_data.ppm |= ppm;
1423                device->path_data.tbvpm |= epm;
1424                device->path_data.cablepm |= cablepm;
1425                device->path_data.hpfpm |= hpfpm;
1426                spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1427        }
1428        clear_bit(DASD_FLAG_PATH_VERIFY, &device->flags);
1429        dasd_put_device(device);
1430        if (data->isglobal)
1431                mutex_unlock(&dasd_path_verification_mutex);
1432        else
1433                kfree(data);
1434}
1435
1436static int dasd_eckd_verify_path(struct dasd_device *device, __u8 lpm)
1437{
1438        struct path_verification_work_data *data;
1439
1440        data = kmalloc(sizeof(*data), GFP_ATOMIC | GFP_DMA);
1441        if (!data) {
1442                if (mutex_trylock(&dasd_path_verification_mutex)) {
1443                        data = path_verification_worker;
1444                        data->isglobal = 1;
1445                } else
1446                        return -ENOMEM;
1447        } else {
1448                memset(data, 0, sizeof(*data));
1449                data->isglobal = 0;
1450        }
1451        INIT_WORK(&data->worker, do_path_verification_work);
1452        dasd_get_device(device);
1453        data->device = device;
1454        data->tbvpm = lpm;
1455        schedule_work(&data->worker);
1456        return 0;
1457}
1458
1459static int dasd_eckd_read_features(struct dasd_device *device)
1460{
1461        struct dasd_eckd_private *private = device->private;
1462        struct dasd_psf_prssd_data *prssdp;
1463        struct dasd_rssd_features *features;
1464        struct dasd_ccw_req *cqr;
1465        struct ccw1 *ccw;
1466        int rc;
1467
1468        memset(&private->features, 0, sizeof(struct dasd_rssd_features));
1469        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
1470                                   (sizeof(struct dasd_psf_prssd_data) +
1471                                    sizeof(struct dasd_rssd_features)),
1472                                   device);
1473        if (IS_ERR(cqr)) {
1474                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s", "Could not "
1475                                "allocate initialization request");
1476                return PTR_ERR(cqr);
1477        }
1478        cqr->startdev = device;
1479        cqr->memdev = device;
1480        cqr->block = NULL;
1481        cqr->retries = 256;
1482        cqr->expires = 10 * HZ;
1483
1484        /* Prepare for Read Subsystem Data */
1485        prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1486        memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
1487        prssdp->order = PSF_ORDER_PRSSD;
1488        prssdp->suborder = 0x41;        /* Read Feature Codes */
1489        /* all other bytes of prssdp must be zero */
1490
1491        ccw = cqr->cpaddr;
1492        ccw->cmd_code = DASD_ECKD_CCW_PSF;
1493        ccw->count = sizeof(struct dasd_psf_prssd_data);
1494        ccw->flags |= CCW_FLAG_CC;
1495        ccw->cda = (__u32)(addr_t) prssdp;
1496
1497        /* Read Subsystem Data - feature codes */
1498        features = (struct dasd_rssd_features *) (prssdp + 1);
1499        memset(features, 0, sizeof(struct dasd_rssd_features));
1500
1501        ccw++;
1502        ccw->cmd_code = DASD_ECKD_CCW_RSSD;
1503        ccw->count = sizeof(struct dasd_rssd_features);
1504        ccw->cda = (__u32)(addr_t) features;
1505
1506        cqr->buildclk = get_tod_clock();
1507        cqr->status = DASD_CQR_FILLED;
1508        rc = dasd_sleep_on(cqr);
1509        if (rc == 0) {
1510                prssdp = (struct dasd_psf_prssd_data *) cqr->data;
1511                features = (struct dasd_rssd_features *) (prssdp + 1);
1512                memcpy(&private->features, features,
1513                       sizeof(struct dasd_rssd_features));
1514        } else
1515                dev_warn(&device->cdev->dev, "Reading device feature codes"
1516                         " failed with rc=%d\n", rc);
1517        dasd_sfree_request(cqr, cqr->memdev);
1518        return rc;
1519}
1520
1521
1522/*
1523 * Build CP for Perform Subsystem Function - SSC.
1524 */
1525static struct dasd_ccw_req *dasd_eckd_build_psf_ssc(struct dasd_device *device,
1526                                                    int enable_pav)
1527{
1528        struct dasd_ccw_req *cqr;
1529        struct dasd_psf_ssc_data *psf_ssc_data;
1530        struct ccw1 *ccw;
1531
1532        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
1533                                  sizeof(struct dasd_psf_ssc_data),
1534                                  device);
1535
1536        if (IS_ERR(cqr)) {
1537                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1538                           "Could not allocate PSF-SSC request");
1539                return cqr;
1540        }
1541        psf_ssc_data = (struct dasd_psf_ssc_data *)cqr->data;
1542        psf_ssc_data->order = PSF_ORDER_SSC;
1543        psf_ssc_data->suborder = 0xc0;
1544        if (enable_pav) {
1545                psf_ssc_data->suborder |= 0x08;
1546                psf_ssc_data->reserved[0] = 0x88;
1547        }
1548        ccw = cqr->cpaddr;
1549        ccw->cmd_code = DASD_ECKD_CCW_PSF;
1550        ccw->cda = (__u32)(addr_t)psf_ssc_data;
1551        ccw->count = 66;
1552
1553        cqr->startdev = device;
1554        cqr->memdev = device;
1555        cqr->block = NULL;
1556        cqr->retries = 256;
1557        cqr->expires = 10*HZ;
1558        cqr->buildclk = get_tod_clock();
1559        cqr->status = DASD_CQR_FILLED;
1560        return cqr;
1561}
1562
1563/*
1564 * Perform Subsystem Function.
1565 * It is necessary to trigger CIO for channel revalidation since this
1566 * call might change behaviour of DASD devices.
1567 */
1568static int
1569dasd_eckd_psf_ssc(struct dasd_device *device, int enable_pav,
1570                  unsigned long flags)
1571{
1572        struct dasd_ccw_req *cqr;
1573        int rc;
1574
1575        cqr = dasd_eckd_build_psf_ssc(device, enable_pav);
1576        if (IS_ERR(cqr))
1577                return PTR_ERR(cqr);
1578
1579        /*
1580         * set flags e.g. turn on failfast, to prevent blocking
1581         * the calling function should handle failed requests
1582         */
1583        cqr->flags |= flags;
1584
1585        rc = dasd_sleep_on(cqr);
1586        if (!rc)
1587                /* trigger CIO to reprobe devices */
1588                css_schedule_reprobe();
1589        else if (cqr->intrc == -EAGAIN)
1590                rc = -EAGAIN;
1591
1592        dasd_sfree_request(cqr, cqr->memdev);
1593        return rc;
1594}
1595
1596/*
1597 * Valide storage server of current device.
1598 */
1599static int dasd_eckd_validate_server(struct dasd_device *device,
1600                                     unsigned long flags)
1601{
1602        struct dasd_eckd_private *private = device->private;
1603        int enable_pav, rc;
1604
1605        if (private->uid.type == UA_BASE_PAV_ALIAS ||
1606            private->uid.type == UA_HYPER_PAV_ALIAS)
1607                return 0;
1608        if (dasd_nopav || MACHINE_IS_VM)
1609                enable_pav = 0;
1610        else
1611                enable_pav = 1;
1612        rc = dasd_eckd_psf_ssc(device, enable_pav, flags);
1613
1614        /* may be requested feature is not available on server,
1615         * therefore just report error and go ahead */
1616        DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "PSF-SSC for SSID %04x "
1617                        "returned rc=%d", private->uid.ssid, rc);
1618        return rc;
1619}
1620
1621/*
1622 * worker to do a validate server in case of a lost pathgroup
1623 */
1624static void dasd_eckd_do_validate_server(struct work_struct *work)
1625{
1626        struct dasd_device *device = container_of(work, struct dasd_device,
1627                                                  kick_validate);
1628        unsigned long flags = 0;
1629
1630        set_bit(DASD_CQR_FLAGS_FAILFAST, &flags);
1631        if (dasd_eckd_validate_server(device, flags)
1632            == -EAGAIN) {
1633                /* schedule worker again if failed */
1634                schedule_work(&device->kick_validate);
1635                return;
1636        }
1637
1638        dasd_put_device(device);
1639}
1640
1641static void dasd_eckd_kick_validate_server(struct dasd_device *device)
1642{
1643        dasd_get_device(device);
1644        /* exit if device not online or in offline processing */
1645        if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1646           device->state < DASD_STATE_ONLINE) {
1647                dasd_put_device(device);
1648                return;
1649        }
1650        /* queue call to do_validate_server to the kernel event daemon. */
1651        if (!schedule_work(&device->kick_validate))
1652                dasd_put_device(device);
1653}
1654
1655static u32 get_fcx_max_data(struct dasd_device *device)
1656{
1657        struct dasd_eckd_private *private = device->private;
1658        int fcx_in_css, fcx_in_gneq, fcx_in_features;
1659        int tpm, mdc;
1660
1661        if (dasd_nofcx)
1662                return 0;
1663        /* is transport mode supported? */
1664        fcx_in_css = css_general_characteristics.fcx;
1665        fcx_in_gneq = private->gneq->reserved2[7] & 0x04;
1666        fcx_in_features = private->features.feature[40] & 0x80;
1667        tpm = fcx_in_css && fcx_in_gneq && fcx_in_features;
1668
1669        if (!tpm)
1670                return 0;
1671
1672        mdc = ccw_device_get_mdc(device->cdev, 0);
1673        if (mdc < 0) {
1674                dev_warn(&device->cdev->dev, "Detecting the maximum supported"
1675                         " data size for zHPF requests failed\n");
1676                return 0;
1677        } else
1678                return mdc * FCX_MAX_DATA_FACTOR;
1679}
1680
1681/*
1682 * Check device characteristics.
1683 * If the device is accessible using ECKD discipline, the device is enabled.
1684 */
1685static int
1686dasd_eckd_check_characteristics(struct dasd_device *device)
1687{
1688        struct dasd_eckd_private *private = device->private;
1689        struct dasd_block *block;
1690        struct dasd_uid temp_uid;
1691        int rc, i;
1692        int readonly;
1693        unsigned long value;
1694
1695        /* setup work queue for validate server*/
1696        INIT_WORK(&device->kick_validate, dasd_eckd_do_validate_server);
1697        /* setup work queue for summary unit check */
1698        INIT_WORK(&device->suc_work, dasd_alias_handle_summary_unit_check);
1699
1700        if (!ccw_device_is_pathgroup(device->cdev)) {
1701                dev_warn(&device->cdev->dev,
1702                         "A channel path group could not be established\n");
1703                return -EIO;
1704        }
1705        if (!ccw_device_is_multipath(device->cdev)) {
1706                dev_info(&device->cdev->dev,
1707                         "The DASD is not operating in multipath mode\n");
1708        }
1709        if (!private) {
1710                private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
1711                if (!private) {
1712                        dev_warn(&device->cdev->dev,
1713                                 "Allocating memory for private DASD data "
1714                                 "failed\n");
1715                        return -ENOMEM;
1716                }
1717                device->private = private;
1718        } else {
1719                memset(private, 0, sizeof(*private));
1720        }
1721        /* Invalidate status of initial analysis. */
1722        private->init_cqr_status = -1;
1723        /* Set default cache operations. */
1724        private->attrib.operation = DASD_NORMAL_CACHE;
1725        private->attrib.nr_cyl = 0;
1726
1727        /* Read Configuration Data */
1728        rc = dasd_eckd_read_conf(device);
1729        if (rc)
1730                goto out_err1;
1731
1732        /* set default timeout */
1733        device->default_expires = DASD_EXPIRES;
1734        /* set default retry count */
1735        device->default_retries = DASD_RETRIES;
1736
1737        if (private->gneq) {
1738                value = 1;
1739                for (i = 0; i < private->gneq->timeout.value; i++)
1740                        value = 10 * value;
1741                value = value * private->gneq->timeout.number;
1742                /* do not accept useless values */
1743                if (value != 0 && value <= DASD_EXPIRES_MAX)
1744                        device->default_expires = value;
1745        }
1746
1747        dasd_eckd_get_uid(device, &temp_uid);
1748        if (temp_uid.type == UA_BASE_DEVICE) {
1749                block = dasd_alloc_block();
1750                if (IS_ERR(block)) {
1751                        DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
1752                                        "could not allocate dasd "
1753                                        "block structure");
1754                        rc = PTR_ERR(block);
1755                        goto out_err1;
1756                }
1757                device->block = block;
1758                block->base = device;
1759        }
1760
1761        /* register lcu with alias handling, enable PAV */
1762        rc = dasd_alias_make_device_known_to_lcu(device);
1763        if (rc)
1764                goto out_err2;
1765
1766        dasd_eckd_validate_server(device, 0);
1767
1768        /* device may report different configuration data after LCU setup */
1769        rc = dasd_eckd_read_conf(device);
1770        if (rc)
1771                goto out_err3;
1772
1773        /* Read Feature Codes */
1774        dasd_eckd_read_features(device);
1775
1776        /* Read Device Characteristics */
1777        rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
1778                                         &private->rdc_data, 64);
1779        if (rc) {
1780                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
1781                                "Read device characteristic failed, rc=%d", rc);
1782                goto out_err3;
1783        }
1784
1785        if ((device->features & DASD_FEATURE_USERAW) &&
1786            !(private->rdc_data.facilities.RT_in_LR)) {
1787                dev_err(&device->cdev->dev, "The storage server does not "
1788                        "support raw-track access\n");
1789                rc = -EINVAL;
1790                goto out_err3;
1791        }
1792
1793        /* find the valid cylinder size */
1794        if (private->rdc_data.no_cyl == LV_COMPAT_CYL &&
1795            private->rdc_data.long_no_cyl)
1796                private->real_cyl = private->rdc_data.long_no_cyl;
1797        else
1798                private->real_cyl = private->rdc_data.no_cyl;
1799
1800        private->fcx_max_data = get_fcx_max_data(device);
1801
1802        readonly = dasd_device_is_ro(device);
1803        if (readonly)
1804                set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
1805
1806        dev_info(&device->cdev->dev, "New DASD %04X/%02X (CU %04X/%02X) "
1807                 "with %d cylinders, %d heads, %d sectors%s\n",
1808                 private->rdc_data.dev_type,
1809                 private->rdc_data.dev_model,
1810                 private->rdc_data.cu_type,
1811                 private->rdc_data.cu_model.model,
1812                 private->real_cyl,
1813                 private->rdc_data.trk_per_cyl,
1814                 private->rdc_data.sec_per_trk,
1815                 readonly ? ", read-only device" : "");
1816        return 0;
1817
1818out_err3:
1819        dasd_alias_disconnect_device_from_lcu(device);
1820out_err2:
1821        dasd_free_block(device->block);
1822        device->block = NULL;
1823out_err1:
1824        kfree(private->conf_data);
1825        kfree(device->private);
1826        device->private = NULL;
1827        return rc;
1828}
1829
1830static void dasd_eckd_uncheck_device(struct dasd_device *device)
1831{
1832        struct dasd_eckd_private *private = device->private;
1833        int i;
1834
1835        dasd_alias_disconnect_device_from_lcu(device);
1836        private->ned = NULL;
1837        private->sneq = NULL;
1838        private->vdsneq = NULL;
1839        private->gneq = NULL;
1840        private->conf_len = 0;
1841        for (i = 0; i < 8; i++) {
1842                kfree(private->path_conf_data[i]);
1843                if ((__u8 *)private->path_conf_data[i] ==
1844                    private->conf_data) {
1845                        private->conf_data = NULL;
1846                        private->conf_len = 0;
1847                }
1848                private->path_conf_data[i] = NULL;
1849        }
1850        kfree(private->conf_data);
1851        private->conf_data = NULL;
1852}
1853
1854static struct dasd_ccw_req *
1855dasd_eckd_analysis_ccw(struct dasd_device *device)
1856{
1857        struct dasd_eckd_private *private = device->private;
1858        struct eckd_count *count_data;
1859        struct LO_eckd_data *LO_data;
1860        struct dasd_ccw_req *cqr;
1861        struct ccw1 *ccw;
1862        int cplength, datasize;
1863        int i;
1864
1865        cplength = 8;
1866        datasize = sizeof(struct DE_eckd_data) + 2*sizeof(struct LO_eckd_data);
1867        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize, device);
1868        if (IS_ERR(cqr))
1869                return cqr;
1870        ccw = cqr->cpaddr;
1871        /* Define extent for the first 3 tracks. */
1872        define_extent(ccw++, cqr->data, 0, 2,
1873                      DASD_ECKD_CCW_READ_COUNT, device);
1874        LO_data = cqr->data + sizeof(struct DE_eckd_data);
1875        /* Locate record for the first 4 records on track 0. */
1876        ccw[-1].flags |= CCW_FLAG_CC;
1877        locate_record(ccw++, LO_data++, 0, 0, 4,
1878                      DASD_ECKD_CCW_READ_COUNT, device, 0);
1879
1880        count_data = private->count_area;
1881        for (i = 0; i < 4; i++) {
1882                ccw[-1].flags |= CCW_FLAG_CC;
1883                ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1884                ccw->flags = 0;
1885                ccw->count = 8;
1886                ccw->cda = (__u32)(addr_t) count_data;
1887                ccw++;
1888                count_data++;
1889        }
1890
1891        /* Locate record for the first record on track 2. */
1892        ccw[-1].flags |= CCW_FLAG_CC;
1893        locate_record(ccw++, LO_data++, 2, 0, 1,
1894                      DASD_ECKD_CCW_READ_COUNT, device, 0);
1895        /* Read count ccw. */
1896        ccw[-1].flags |= CCW_FLAG_CC;
1897        ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
1898        ccw->flags = 0;
1899        ccw->count = 8;
1900        ccw->cda = (__u32)(addr_t) count_data;
1901
1902        cqr->block = NULL;
1903        cqr->startdev = device;
1904        cqr->memdev = device;
1905        cqr->retries = 255;
1906        cqr->buildclk = get_tod_clock();
1907        cqr->status = DASD_CQR_FILLED;
1908        return cqr;
1909}
1910
1911/* differentiate between 'no record found' and any other error */
1912static int dasd_eckd_analysis_evaluation(struct dasd_ccw_req *init_cqr)
1913{
1914        char *sense;
1915        if (init_cqr->status == DASD_CQR_DONE)
1916                return INIT_CQR_OK;
1917        else if (init_cqr->status == DASD_CQR_NEED_ERP ||
1918                 init_cqr->status == DASD_CQR_FAILED) {
1919                sense = dasd_get_sense(&init_cqr->irb);
1920                if (sense && (sense[1] & SNS1_NO_REC_FOUND))
1921                        return INIT_CQR_UNFORMATTED;
1922                else
1923                        return INIT_CQR_ERROR;
1924        } else
1925                return INIT_CQR_ERROR;
1926}
1927
1928/*
1929 * This is the callback function for the init_analysis cqr. It saves
1930 * the status of the initial analysis ccw before it frees it and kicks
1931 * the device to continue the startup sequence. This will call
1932 * dasd_eckd_do_analysis again (if the devices has not been marked
1933 * for deletion in the meantime).
1934 */
1935static void dasd_eckd_analysis_callback(struct dasd_ccw_req *init_cqr,
1936                                        void *data)
1937{
1938        struct dasd_device *device = init_cqr->startdev;
1939        struct dasd_eckd_private *private = device->private;
1940
1941        private->init_cqr_status = dasd_eckd_analysis_evaluation(init_cqr);
1942        dasd_sfree_request(init_cqr, device);
1943        dasd_kick_device(device);
1944}
1945
1946static int dasd_eckd_start_analysis(struct dasd_block *block)
1947{
1948        struct dasd_ccw_req *init_cqr;
1949
1950        init_cqr = dasd_eckd_analysis_ccw(block->base);
1951        if (IS_ERR(init_cqr))
1952                return PTR_ERR(init_cqr);
1953        init_cqr->callback = dasd_eckd_analysis_callback;
1954        init_cqr->callback_data = NULL;
1955        init_cqr->expires = 5*HZ;
1956        /* first try without ERP, so we can later handle unformatted
1957         * devices as special case
1958         */
1959        clear_bit(DASD_CQR_FLAGS_USE_ERP, &init_cqr->flags);
1960        init_cqr->retries = 0;
1961        dasd_add_request_head(init_cqr);
1962        return -EAGAIN;
1963}
1964
1965static int dasd_eckd_end_analysis(struct dasd_block *block)
1966{
1967        struct dasd_device *device = block->base;
1968        struct dasd_eckd_private *private = device->private;
1969        struct eckd_count *count_area;
1970        unsigned int sb, blk_per_trk;
1971        int status, i;
1972        struct dasd_ccw_req *init_cqr;
1973
1974        status = private->init_cqr_status;
1975        private->init_cqr_status = -1;
1976        if (status == INIT_CQR_ERROR) {
1977                /* try again, this time with full ERP */
1978                init_cqr = dasd_eckd_analysis_ccw(device);
1979                dasd_sleep_on(init_cqr);
1980                status = dasd_eckd_analysis_evaluation(init_cqr);
1981                dasd_sfree_request(init_cqr, device);
1982        }
1983
1984        if (device->features & DASD_FEATURE_USERAW) {
1985                block->bp_block = DASD_RAW_BLOCKSIZE;
1986                blk_per_trk = DASD_RAW_BLOCK_PER_TRACK;
1987                block->s2b_shift = 3;
1988                goto raw;
1989        }
1990
1991        if (status == INIT_CQR_UNFORMATTED) {
1992                dev_warn(&device->cdev->dev, "The DASD is not formatted\n");
1993                return -EMEDIUMTYPE;
1994        } else if (status == INIT_CQR_ERROR) {
1995                dev_err(&device->cdev->dev,
1996                        "Detecting the DASD disk layout failed because "
1997                        "of an I/O error\n");
1998                return -EIO;
1999        }
2000
2001        private->uses_cdl = 1;
2002        /* Check Track 0 for Compatible Disk Layout */
2003        count_area = NULL;
2004        for (i = 0; i < 3; i++) {
2005                if (private->count_area[i].kl != 4 ||
2006                    private->count_area[i].dl != dasd_eckd_cdl_reclen(i) - 4 ||
2007                    private->count_area[i].cyl != 0 ||
2008                    private->count_area[i].head != count_area_head[i] ||
2009                    private->count_area[i].record != count_area_rec[i]) {
2010                        private->uses_cdl = 0;
2011                        break;
2012                }
2013        }
2014        if (i == 3)
2015                count_area = &private->count_area[4];
2016
2017        if (private->uses_cdl == 0) {
2018                for (i = 0; i < 5; i++) {
2019                        if ((private->count_area[i].kl != 0) ||
2020                            (private->count_area[i].dl !=
2021                             private->count_area[0].dl) ||
2022                            private->count_area[i].cyl !=  0 ||
2023                            private->count_area[i].head != count_area_head[i] ||
2024                            private->count_area[i].record != count_area_rec[i])
2025                                break;
2026                }
2027                if (i == 5)
2028                        count_area = &private->count_area[0];
2029        } else {
2030                if (private->count_area[3].record == 1)
2031                        dev_warn(&device->cdev->dev,
2032                                 "Track 0 has no records following the VTOC\n");
2033        }
2034
2035        if (count_area != NULL && count_area->kl == 0) {
2036                /* we found notthing violating our disk layout */
2037                if (dasd_check_blocksize(count_area->dl) == 0)
2038                        block->bp_block = count_area->dl;
2039        }
2040        if (block->bp_block == 0) {
2041                dev_warn(&device->cdev->dev,
2042                         "The disk layout of the DASD is not supported\n");
2043                return -EMEDIUMTYPE;
2044        }
2045        block->s2b_shift = 0;   /* bits to shift 512 to get a block */
2046        for (sb = 512; sb < block->bp_block; sb = sb << 1)
2047                block->s2b_shift++;
2048
2049        blk_per_trk = recs_per_track(&private->rdc_data, 0, block->bp_block);
2050
2051raw:
2052        block->blocks = (private->real_cyl *
2053                          private->rdc_data.trk_per_cyl *
2054                          blk_per_trk);
2055
2056        dev_info(&device->cdev->dev,
2057                 "DASD with %d KB/block, %d KB total size, %d KB/track, "
2058                 "%s\n", (block->bp_block >> 10),
2059                 ((private->real_cyl *
2060                   private->rdc_data.trk_per_cyl *
2061                   blk_per_trk * (block->bp_block >> 9)) >> 1),
2062                 ((blk_per_trk * block->bp_block) >> 10),
2063                 private->uses_cdl ?
2064                 "compatible disk layout" : "linux disk layout");
2065
2066        return 0;
2067}
2068
2069static int dasd_eckd_do_analysis(struct dasd_block *block)
2070{
2071        struct dasd_eckd_private *private = block->base->private;
2072
2073        if (private->init_cqr_status < 0)
2074                return dasd_eckd_start_analysis(block);
2075        else
2076                return dasd_eckd_end_analysis(block);
2077}
2078
2079static int dasd_eckd_basic_to_ready(struct dasd_device *device)
2080{
2081        return dasd_alias_add_device(device);
2082};
2083
2084static int dasd_eckd_online_to_ready(struct dasd_device *device)
2085{
2086        cancel_work_sync(&device->reload_device);
2087        cancel_work_sync(&device->kick_validate);
2088        return 0;
2089};
2090
2091static int dasd_eckd_basic_to_known(struct dasd_device *device)
2092{
2093        return dasd_alias_remove_device(device);
2094};
2095
2096static int
2097dasd_eckd_fill_geometry(struct dasd_block *block, struct hd_geometry *geo)
2098{
2099        struct dasd_eckd_private *private = block->base->private;
2100
2101        if (dasd_check_blocksize(block->bp_block) == 0) {
2102                geo->sectors = recs_per_track(&private->rdc_data,
2103                                              0, block->bp_block);
2104        }
2105        geo->cylinders = private->rdc_data.no_cyl;
2106        geo->heads = private->rdc_data.trk_per_cyl;
2107        return 0;
2108}
2109
2110/*
2111 * Build the TCW request for the format check
2112 */
2113static struct dasd_ccw_req *
2114dasd_eckd_build_check_tcw(struct dasd_device *base, struct format_data_t *fdata,
2115                          int enable_pav, struct eckd_count *fmt_buffer,
2116                          int rpt)
2117{
2118        struct dasd_eckd_private *start_priv;
2119        struct dasd_device *startdev = NULL;
2120        struct tidaw *last_tidaw = NULL;
2121        struct dasd_ccw_req *cqr;
2122        struct itcw *itcw;
2123        int itcw_size;
2124        int count;
2125        int rc;
2126        int i;
2127
2128        if (enable_pav)
2129                startdev = dasd_alias_get_start_dev(base);
2130
2131        if (!startdev)
2132                startdev = base;
2133
2134        start_priv = startdev->private;
2135
2136        count = rpt * (fdata->stop_unit - fdata->start_unit + 1);
2137
2138        /*
2139         * we're adding 'count' amount of tidaw to the itcw.
2140         * calculate the corresponding itcw_size
2141         */
2142        itcw_size = itcw_calc_size(0, count, 0);
2143
2144        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
2145        if (IS_ERR(cqr))
2146                return cqr;
2147
2148        start_priv->count++;
2149
2150        itcw = itcw_init(cqr->data, itcw_size, ITCW_OP_READ, 0, count, 0);
2151        if (IS_ERR(itcw)) {
2152                rc = -EINVAL;
2153                goto out_err;
2154        }
2155
2156        cqr->cpaddr = itcw_get_tcw(itcw);
2157        rc = prepare_itcw(itcw, fdata->start_unit, fdata->stop_unit,
2158                          DASD_ECKD_CCW_READ_COUNT_MT, base, startdev, 0, count,
2159                          sizeof(struct eckd_count),
2160                          count * sizeof(struct eckd_count), 0, rpt);
2161        if (rc)
2162                goto out_err;
2163
2164        for (i = 0; i < count; i++) {
2165                last_tidaw = itcw_add_tidaw(itcw, 0, fmt_buffer++,
2166                                            sizeof(struct eckd_count));
2167                if (IS_ERR(last_tidaw)) {
2168                        rc = -EINVAL;
2169                        goto out_err;
2170                }
2171        }
2172
2173        last_tidaw->flags |= TIDAW_FLAGS_LAST;
2174        itcw_finalize(itcw);
2175
2176        cqr->cpmode = 1;
2177        cqr->startdev = startdev;
2178        cqr->memdev = startdev;
2179        cqr->basedev = base;
2180        cqr->retries = startdev->default_retries;
2181        cqr->expires = startdev->default_expires * HZ;
2182        cqr->buildclk = get_tod_clock();
2183        cqr->status = DASD_CQR_FILLED;
2184        /* Set flags to suppress output for expected errors */
2185        set_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
2186        set_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags);
2187
2188        return cqr;
2189
2190out_err:
2191        dasd_sfree_request(cqr, startdev);
2192
2193        return ERR_PTR(rc);
2194}
2195
2196/*
2197 * Build the CCW request for the format check
2198 */
2199static struct dasd_ccw_req *
2200dasd_eckd_build_check(struct dasd_device *base, struct format_data_t *fdata,
2201                      int enable_pav, struct eckd_count *fmt_buffer, int rpt)
2202{
2203        struct dasd_eckd_private *start_priv;
2204        struct dasd_eckd_private *base_priv;
2205        struct dasd_device *startdev = NULL;
2206        struct dasd_ccw_req *cqr;
2207        struct ccw1 *ccw;
2208        void *data;
2209        int cplength, datasize;
2210        int use_prefix;
2211        int count;
2212        int i;
2213
2214        if (enable_pav)
2215                startdev = dasd_alias_get_start_dev(base);
2216
2217        if (!startdev)
2218                startdev = base;
2219
2220        start_priv = startdev->private;
2221        base_priv = base->private;
2222
2223        count = rpt * (fdata->stop_unit - fdata->start_unit + 1);
2224
2225        use_prefix = base_priv->features.feature[8] & 0x01;
2226
2227        if (use_prefix) {
2228                cplength = 1;
2229                datasize = sizeof(struct PFX_eckd_data);
2230        } else {
2231                cplength = 2;
2232                datasize = sizeof(struct DE_eckd_data) +
2233                        sizeof(struct LO_eckd_data);
2234        }
2235        cplength += count;
2236
2237        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
2238                                  startdev);
2239        if (IS_ERR(cqr))
2240                return cqr;
2241
2242        start_priv->count++;
2243        data = cqr->data;
2244        ccw = cqr->cpaddr;
2245
2246        if (use_prefix) {
2247                prefix_LRE(ccw++, data, fdata->start_unit, fdata->stop_unit,
2248                           DASD_ECKD_CCW_READ_COUNT, base, startdev, 1, 0,
2249                           count, 0, 0);
2250        } else {
2251                define_extent(ccw++, data, fdata->start_unit, fdata->stop_unit,
2252                              DASD_ECKD_CCW_READ_COUNT, startdev);
2253
2254                data += sizeof(struct DE_eckd_data);
2255                ccw[-1].flags |= CCW_FLAG_CC;
2256
2257                locate_record(ccw++, data, fdata->start_unit, 0, count,
2258                              DASD_ECKD_CCW_READ_COUNT, base, 0);
2259        }
2260
2261        for (i = 0; i < count; i++) {
2262                ccw[-1].flags |= CCW_FLAG_CC;
2263                ccw->cmd_code = DASD_ECKD_CCW_READ_COUNT;
2264                ccw->flags = CCW_FLAG_SLI;
2265                ccw->count = 8;
2266                ccw->cda = (__u32)(addr_t) fmt_buffer;
2267                ccw++;
2268                fmt_buffer++;
2269        }
2270
2271        cqr->startdev = startdev;
2272        cqr->memdev = startdev;
2273        cqr->basedev = base;
2274        cqr->retries = DASD_RETRIES;
2275        cqr->expires = startdev->default_expires * HZ;
2276        cqr->buildclk = get_tod_clock();
2277        cqr->status = DASD_CQR_FILLED;
2278        /* Set flags to suppress output for expected errors */
2279        set_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
2280
2281        return cqr;
2282}
2283
2284static struct dasd_ccw_req *
2285dasd_eckd_build_format(struct dasd_device *base,
2286                       struct format_data_t *fdata,
2287                       int enable_pav)
2288{
2289        struct dasd_eckd_private *base_priv;
2290        struct dasd_eckd_private *start_priv;
2291        struct dasd_device *startdev = NULL;
2292        struct dasd_ccw_req *fcp;
2293        struct eckd_count *ect;
2294        struct ch_t address;
2295        struct ccw1 *ccw;
2296        void *data;
2297        int rpt;
2298        int cplength, datasize;
2299        int i, j;
2300        int intensity = 0;
2301        int r0_perm;
2302        int nr_tracks;
2303        int use_prefix;
2304
2305        if (enable_pav)
2306                startdev = dasd_alias_get_start_dev(base);
2307
2308        if (!startdev)
2309                startdev = base;
2310
2311        start_priv = startdev->private;
2312        base_priv = base->private;
2313
2314        rpt = recs_per_track(&base_priv->rdc_data, 0, fdata->blksize);
2315
2316        nr_tracks = fdata->stop_unit - fdata->start_unit + 1;
2317
2318        /*
2319         * fdata->intensity is a bit string that tells us what to do:
2320         *   Bit 0: write record zero
2321         *   Bit 1: write home address, currently not supported
2322         *   Bit 2: invalidate tracks
2323         *   Bit 3: use OS/390 compatible disk layout (cdl)
2324         *   Bit 4: do not allow storage subsystem to modify record zero
2325         * Only some bit combinations do make sense.
2326         */
2327        if (fdata->intensity & 0x10) {
2328                r0_perm = 0;
2329                intensity = fdata->intensity & ~0x10;
2330        } else {
2331                r0_perm = 1;
2332                intensity = fdata->intensity;
2333        }
2334
2335        use_prefix = base_priv->features.feature[8] & 0x01;
2336
2337        switch (intensity) {
2338        case 0x00:      /* Normal format */
2339        case 0x08:      /* Normal format, use cdl. */
2340                cplength = 2 + (rpt*nr_tracks);
2341                if (use_prefix)
2342                        datasize = sizeof(struct PFX_eckd_data) +
2343                                sizeof(struct LO_eckd_data) +
2344                                rpt * nr_tracks * sizeof(struct eckd_count);
2345                else
2346                        datasize = sizeof(struct DE_eckd_data) +
2347                                sizeof(struct LO_eckd_data) +
2348                                rpt * nr_tracks * sizeof(struct eckd_count);
2349                break;
2350        case 0x01:      /* Write record zero and format track. */
2351        case 0x09:      /* Write record zero and format track, use cdl. */
2352                cplength = 2 + rpt * nr_tracks;
2353                if (use_prefix)
2354                        datasize = sizeof(struct PFX_eckd_data) +
2355                                sizeof(struct LO_eckd_data) +
2356                                sizeof(struct eckd_count) +
2357                                rpt * nr_tracks * sizeof(struct eckd_count);
2358                else
2359                        datasize = sizeof(struct DE_eckd_data) +
2360                                sizeof(struct LO_eckd_data) +
2361                                sizeof(struct eckd_count) +
2362                                rpt * nr_tracks * sizeof(struct eckd_count);
2363                break;
2364        case 0x04:      /* Invalidate track. */
2365        case 0x0c:      /* Invalidate track, use cdl. */
2366                cplength = 3;
2367                if (use_prefix)
2368                        datasize = sizeof(struct PFX_eckd_data) +
2369                                sizeof(struct LO_eckd_data) +
2370                                sizeof(struct eckd_count);
2371                else
2372                        datasize = sizeof(struct DE_eckd_data) +
2373                                sizeof(struct LO_eckd_data) +
2374                                sizeof(struct eckd_count);
2375                break;
2376        default:
2377                dev_warn(&startdev->cdev->dev,
2378                         "An I/O control call used incorrect flags 0x%x\n",
2379                         fdata->intensity);
2380                return ERR_PTR(-EINVAL);
2381        }
2382        /* Allocate the format ccw request. */
2383        fcp = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
2384                                   datasize, startdev);
2385        if (IS_ERR(fcp))
2386                return fcp;
2387
2388        start_priv->count++;
2389        data = fcp->data;
2390        ccw = fcp->cpaddr;
2391
2392        switch (intensity & ~0x08) {
2393        case 0x00: /* Normal format. */
2394                if (use_prefix) {
2395                        prefix(ccw++, (struct PFX_eckd_data *) data,
2396                               fdata->start_unit, fdata->stop_unit,
2397                               DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2398                        /* grant subsystem permission to format R0 */
2399                        if (r0_perm)
2400                                ((struct PFX_eckd_data *)data)
2401                                        ->define_extent.ga_extended |= 0x04;
2402                        data += sizeof(struct PFX_eckd_data);
2403                } else {
2404                        define_extent(ccw++, (struct DE_eckd_data *) data,
2405                                      fdata->start_unit, fdata->stop_unit,
2406                                      DASD_ECKD_CCW_WRITE_CKD, startdev);
2407                        /* grant subsystem permission to format R0 */
2408                        if (r0_perm)
2409                                ((struct DE_eckd_data *) data)
2410                                        ->ga_extended |= 0x04;
2411                        data += sizeof(struct DE_eckd_data);
2412                }
2413                ccw[-1].flags |= CCW_FLAG_CC;
2414                locate_record(ccw++, (struct LO_eckd_data *) data,
2415                              fdata->start_unit, 0, rpt*nr_tracks,
2416                              DASD_ECKD_CCW_WRITE_CKD, base,
2417                              fdata->blksize);
2418                data += sizeof(struct LO_eckd_data);
2419                break;
2420        case 0x01: /* Write record zero + format track. */
2421                if (use_prefix) {
2422                        prefix(ccw++, (struct PFX_eckd_data *) data,
2423                               fdata->start_unit, fdata->stop_unit,
2424                               DASD_ECKD_CCW_WRITE_RECORD_ZERO,
2425                               base, startdev);
2426                        data += sizeof(struct PFX_eckd_data);
2427                } else {
2428                        define_extent(ccw++, (struct DE_eckd_data *) data,
2429                               fdata->start_unit, fdata->stop_unit,
2430                               DASD_ECKD_CCW_WRITE_RECORD_ZERO, startdev);
2431                        data += sizeof(struct DE_eckd_data);
2432                }
2433                ccw[-1].flags |= CCW_FLAG_CC;
2434                locate_record(ccw++, (struct LO_eckd_data *) data,
2435                              fdata->start_unit, 0, rpt * nr_tracks + 1,
2436                              DASD_ECKD_CCW_WRITE_RECORD_ZERO, base,
2437                              base->block->bp_block);
2438                data += sizeof(struct LO_eckd_data);
2439                break;
2440        case 0x04: /* Invalidate track. */
2441                if (use_prefix) {
2442                        prefix(ccw++, (struct PFX_eckd_data *) data,
2443                               fdata->start_unit, fdata->stop_unit,
2444                               DASD_ECKD_CCW_WRITE_CKD, base, startdev);
2445                        data += sizeof(struct PFX_eckd_data);
2446                } else {
2447                        define_extent(ccw++, (struct DE_eckd_data *) data,
2448                               fdata->start_unit, fdata->stop_unit,
2449                               DASD_ECKD_CCW_WRITE_CKD, startdev);
2450                        data += sizeof(struct DE_eckd_data);
2451                }
2452                ccw[-1].flags |= CCW_FLAG_CC;
2453                locate_record(ccw++, (struct LO_eckd_data *) data,
2454                              fdata->start_unit, 0, 1,
2455                              DASD_ECKD_CCW_WRITE_CKD, base, 8);
2456                data += sizeof(struct LO_eckd_data);
2457                break;
2458        }
2459
2460        for (j = 0; j < nr_tracks; j++) {
2461                /* calculate cylinder and head for the current track */
2462                set_ch_t(&address,
2463                         (fdata->start_unit + j) /
2464                         base_priv->rdc_data.trk_per_cyl,
2465                         (fdata->start_unit + j) %
2466                         base_priv->rdc_data.trk_per_cyl);
2467                if (intensity & 0x01) { /* write record zero */
2468                        ect = (struct eckd_count *) data;
2469                        data += sizeof(struct eckd_count);
2470                        ect->cyl = address.cyl;
2471                        ect->head = address.head;
2472                        ect->record = 0;
2473                        ect->kl = 0;
2474                        ect->dl = 8;
2475                        ccw[-1].flags |= CCW_FLAG_CC;
2476                        ccw->cmd_code = DASD_ECKD_CCW_WRITE_RECORD_ZERO;
2477                        ccw->flags = CCW_FLAG_SLI;
2478                        ccw->count = 8;
2479                        ccw->cda = (__u32)(addr_t) ect;
2480                        ccw++;
2481                }
2482                if ((intensity & ~0x08) & 0x04) {       /* erase track */
2483                        ect = (struct eckd_count *) data;
2484                        data += sizeof(struct eckd_count);
2485                        ect->cyl = address.cyl;
2486                        ect->head = address.head;
2487                        ect->record = 1;
2488                        ect->kl = 0;
2489                        ect->dl = 0;
2490                        ccw[-1].flags |= CCW_FLAG_CC;
2491                        ccw->cmd_code = DASD_ECKD_CCW_WRITE_CKD;
2492                        ccw->flags = CCW_FLAG_SLI;
2493                        ccw->count = 8;
2494                        ccw->cda = (__u32)(addr_t) ect;
2495                } else {                /* write remaining records */
2496                        for (i = 0; i < rpt; i++) {
2497                                ect = (struct eckd_count *) data;
2498                                data += sizeof(struct eckd_count);
2499                                ect->cyl = address.cyl;
2500                                ect->head = address.head;
2501                                ect->record = i + 1;
2502                                ect->kl = 0;
2503                                ect->dl = fdata->blksize;
2504                                /*
2505                                 * Check for special tracks 0-1
2506                                 * when formatting CDL
2507                                 */
2508                                if ((intensity & 0x08) &&
2509                                    address.cyl == 0 && address.head == 0) {
2510                                        if (i < 3) {
2511                                                ect->kl = 4;
2512                                                ect->dl = sizes_trk0[i] - 4;
2513                                        }
2514                                }
2515                                if ((intensity & 0x08) &&
2516                                    address.cyl == 0 && address.head == 1) {
2517                                        ect->kl = 44;
2518                                        ect->dl = LABEL_SIZE - 44;
2519                                }
2520                                ccw[-1].flags |= CCW_FLAG_CC;
2521                                if (i != 0 || j == 0)
2522                                        ccw->cmd_code =
2523                                                DASD_ECKD_CCW_WRITE_CKD;
2524                                else
2525                                        ccw->cmd_code =
2526                                                DASD_ECKD_CCW_WRITE_CKD_MT;
2527                                ccw->flags = CCW_FLAG_SLI;
2528                                ccw->count = 8;
2529                                        ccw->cda = (__u32)(addr_t) ect;
2530                                        ccw++;
2531                        }
2532                }
2533        }
2534
2535        fcp->startdev = startdev;
2536        fcp->memdev = startdev;
2537        fcp->basedev = base;
2538        fcp->retries = 256;
2539        fcp->expires = startdev->default_expires * HZ;
2540        fcp->buildclk = get_tod_clock();
2541        fcp->status = DASD_CQR_FILLED;
2542
2543        return fcp;
2544}
2545
2546/*
2547 * Wrapper function to build a CCW request depending on input data
2548 */
2549static struct dasd_ccw_req *
2550dasd_eckd_format_build_ccw_req(struct dasd_device *base,
2551                               struct format_data_t *fdata, int enable_pav,
2552                               int tpm, struct eckd_count *fmt_buffer, int rpt)
2553{
2554        struct dasd_ccw_req *ccw_req;
2555
2556        if (!fmt_buffer) {
2557                ccw_req = dasd_eckd_build_format(base, fdata, enable_pav);
2558        } else {
2559                if (tpm)
2560                        ccw_req = dasd_eckd_build_check_tcw(base, fdata,
2561                                                            enable_pav,
2562                                                            fmt_buffer, rpt);
2563                else
2564                        ccw_req = dasd_eckd_build_check(base, fdata, enable_pav,
2565                                                        fmt_buffer, rpt);
2566        }
2567
2568        return ccw_req;
2569}
2570
2571/*
2572 * Sanity checks on format_data
2573 */
2574static int dasd_eckd_format_sanity_checks(struct dasd_device *base,
2575                                          struct format_data_t *fdata)
2576{
2577        struct dasd_eckd_private *private = base->private;
2578
2579        if (fdata->start_unit >=
2580            (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2581                dev_warn(&base->cdev->dev,
2582                         "Start track number %u used in formatting is too big\n",
2583                         fdata->start_unit);
2584                return -EINVAL;
2585        }
2586        if (fdata->stop_unit >=
2587            (private->real_cyl * private->rdc_data.trk_per_cyl)) {
2588                dev_warn(&base->cdev->dev,
2589                         "Stop track number %u used in formatting is too big\n",
2590                         fdata->stop_unit);
2591                return -EINVAL;
2592        }
2593        if (fdata->start_unit > fdata->stop_unit) {
2594                dev_warn(&base->cdev->dev,
2595                         "Start track %u used in formatting exceeds end track\n",
2596                         fdata->start_unit);
2597                return -EINVAL;
2598        }
2599        if (dasd_check_blocksize(fdata->blksize) != 0) {
2600                dev_warn(&base->cdev->dev,
2601                         "The DASD cannot be formatted with block size %u\n",
2602                         fdata->blksize);
2603                return -EINVAL;
2604        }
2605        return 0;
2606}
2607
2608/*
2609 * This function will process format_data originally coming from an IOCTL
2610 */
2611static int dasd_eckd_format_process_data(struct dasd_device *base,
2612                                         struct format_data_t *fdata,
2613                                         int enable_pav, int tpm,
2614                                         struct eckd_count *fmt_buffer, int rpt,
2615                                         struct irb *irb)
2616{
2617        struct dasd_eckd_private *private = base->private;
2618        struct dasd_ccw_req *cqr, *n;
2619        struct list_head format_queue;
2620        struct dasd_device *device;
2621        char *sense = NULL;
2622        int old_start, old_stop, format_step;
2623        int step, retry;
2624        int rc;
2625
2626        rc = dasd_eckd_format_sanity_checks(base, fdata);
2627        if (rc)
2628                return rc;
2629
2630        INIT_LIST_HEAD(&format_queue);
2631
2632        old_start = fdata->start_unit;
2633        old_stop = fdata->stop_unit;
2634
2635        if (!tpm && fmt_buffer != NULL) {
2636                /* Command Mode / Format Check */
2637                format_step = 1;
2638        } else if (tpm && fmt_buffer != NULL) {
2639                /* Transport Mode / Format Check */
2640                format_step = DASD_CQR_MAX_CCW / rpt;
2641        } else {
2642                /* Normal Formatting */
2643                format_step = DASD_CQR_MAX_CCW /
2644                        recs_per_track(&private->rdc_data, 0, fdata->blksize);
2645        }
2646
2647        do {
2648                retry = 0;
2649                while (fdata->start_unit <= old_stop) {
2650                        step = fdata->stop_unit - fdata->start_unit + 1;
2651                        if (step > format_step) {
2652                                fdata->stop_unit =
2653                                        fdata->start_unit + format_step - 1;
2654                        }
2655
2656                        cqr = dasd_eckd_format_build_ccw_req(base, fdata,
2657                                                             enable_pav, tpm,
2658                                                             fmt_buffer, rpt);
2659                        if (IS_ERR(cqr)) {
2660                                rc = PTR_ERR(cqr);
2661                                if (rc == -ENOMEM) {
2662                                        if (list_empty(&format_queue))
2663                                                goto out;
2664                                        /*
2665                                         * not enough memory available, start
2666                                         * requests retry after first requests
2667                                         * were finished
2668                                         */
2669                                        retry = 1;
2670                                        break;
2671                                }
2672                                goto out_err;
2673                        }
2674                        list_add_tail(&cqr->blocklist, &format_queue);
2675
2676                        if (fmt_buffer) {
2677                                step = fdata->stop_unit - fdata->start_unit + 1;
2678                                fmt_buffer += rpt * step;
2679                        }
2680                        fdata->start_unit = fdata->stop_unit + 1;
2681                        fdata->stop_unit = old_stop;
2682                }
2683
2684                rc = dasd_sleep_on_queue(&format_queue);
2685
2686out_err:
2687                list_for_each_entry_safe(cqr, n, &format_queue, blocklist) {
2688                        device = cqr->startdev;
2689                        private = device->private;
2690
2691                        if (cqr->status == DASD_CQR_FAILED) {
2692                                /*
2693                                 * Only get sense data if called by format
2694                                 * check
2695                                 */
2696                                if (fmt_buffer && irb) {
2697                                        sense = dasd_get_sense(&cqr->irb);
2698                                        memcpy(irb, &cqr->irb, sizeof(*irb));
2699                                }
2700                                rc = -EIO;
2701                        }
2702                        list_del_init(&cqr->blocklist);
2703                        dasd_sfree_request(cqr, device);
2704                        private->count--;
2705                }
2706
2707                if (rc && rc != -EIO)
2708                        goto out;
2709                if (rc == -EIO) {
2710                        /*
2711                         * In case fewer than the expected records are on the
2712                         * track, we will most likely get a 'No Record Found'
2713                         * error (in command mode) or a 'File Protected' error
2714                         * (in transport mode). Those particular cases shouldn't
2715                         * pass the -EIO to the IOCTL, therefore reset the rc
2716                         * and continue.
2717                         */
2718                        if (sense &&
2719                            (sense[1] & SNS1_NO_REC_FOUND ||
2720                             sense[1] & SNS1_FILE_PROTECTED))
2721                                retry = 1;
2722                        else
2723                                goto out;
2724                }
2725
2726        } while (retry);
2727
2728out:
2729        fdata->start_unit = old_start;
2730        fdata->stop_unit = old_stop;
2731
2732        return rc;
2733}
2734
2735static int dasd_eckd_format_device(struct dasd_device *base,
2736                                   struct format_data_t *fdata, int enable_pav)
2737{
2738        return dasd_eckd_format_process_data(base, fdata, enable_pav, 0, NULL,
2739                                             0, NULL);
2740}
2741
2742/*
2743 * Helper function to count consecutive records of a single track.
2744 */
2745static int dasd_eckd_count_records(struct eckd_count *fmt_buffer, int start,
2746                                   int max)
2747{
2748        int head;
2749        int i;
2750
2751        head = fmt_buffer[start].head;
2752
2753        /*
2754         * There are 3 conditions where we stop counting:
2755         * - if data reoccurs (same head and record may reoccur), which may
2756         *   happen due to the way DASD_ECKD_CCW_READ_COUNT works
2757         * - when the head changes, because we're iterating over several tracks
2758         *   then (DASD_ECKD_CCW_READ_COUNT_MT)
2759         * - when we've reached the end of sensible data in the buffer (the
2760         *   record will be 0 then)
2761         */
2762        for (i = start; i < max; i++) {
2763                if (i > start) {
2764                        if ((fmt_buffer[i].head == head &&
2765                            fmt_buffer[i].record == 1) ||
2766                            fmt_buffer[i].head != head ||
2767                            fmt_buffer[i].record == 0)
2768                                break;
2769                }
2770        }
2771
2772        return i - start;
2773}
2774
2775/*
2776 * Evaluate a given range of tracks. Data like number of records, blocksize,
2777 * record ids, and key length are compared with expected data.
2778 *
2779 * If a mismatch occurs, the corresponding error bit is set, as well as
2780 * additional information, depending on the error.
2781 */
2782static void dasd_eckd_format_evaluate_tracks(struct eckd_count *fmt_buffer,
2783                                             struct format_check_t *cdata,
2784                                             int rpt_max, int rpt_exp,
2785                                             int trk_per_cyl, int tpm)
2786{
2787        struct ch_t geo;
2788        int max_entries;
2789        int count = 0;
2790        int trkcount;
2791        int blksize;
2792        int pos = 0;
2793        int i, j;
2794        int kl;
2795
2796        trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
2797        max_entries = trkcount * rpt_max;
2798
2799        for (i = cdata->expect.start_unit; i <= cdata->expect.stop_unit; i++) {
2800                /* Calculate the correct next starting position in the buffer */
2801                if (tpm) {
2802                        while (fmt_buffer[pos].record == 0 &&
2803                               fmt_buffer[pos].dl == 0) {
2804                                if (pos++ > max_entries)
2805                                        break;
2806                        }
2807                } else {
2808                        if (i != cdata->expect.start_unit)
2809                                pos += rpt_max - count;
2810                }
2811
2812                /* Calculate the expected geo values for the current track */
2813                set_ch_t(&geo, i / trk_per_cyl, i % trk_per_cyl);
2814
2815                /* Count and check number of records */
2816                count = dasd_eckd_count_records(fmt_buffer, pos, pos + rpt_max);
2817
2818                if (count < rpt_exp) {
2819                        cdata->result = DASD_FMT_ERR_TOO_FEW_RECORDS;
2820                        break;
2821                }
2822                if (count > rpt_exp) {
2823                        cdata->result = DASD_FMT_ERR_TOO_MANY_RECORDS;
2824                        break;
2825                }
2826
2827                for (j = 0; j < count; j++, pos++) {
2828                        blksize = cdata->expect.blksize;
2829                        kl = 0;
2830
2831                        /*
2832                         * Set special values when checking CDL formatted
2833                         * devices.
2834                         */
2835                        if ((cdata->expect.intensity & 0x08) &&
2836                            geo.cyl == 0 && geo.head == 0) {
2837                                if (j < 3) {
2838                                        blksize = sizes_trk0[j] - 4;
2839                                        kl = 4;
2840                                }
2841                        }
2842                        if ((cdata->expect.intensity & 0x08) &&
2843                            geo.cyl == 0 && geo.head == 1) {
2844                                blksize = LABEL_SIZE - 44;
2845                                kl = 44;
2846                        }
2847
2848                        /* Check blocksize */
2849                        if (fmt_buffer[pos].dl != blksize) {
2850                                cdata->result = DASD_FMT_ERR_BLKSIZE;
2851                                goto out;
2852                        }
2853                        /* Check if key length is 0 */
2854                        if (fmt_buffer[pos].kl != kl) {
2855                                cdata->result = DASD_FMT_ERR_KEY_LENGTH;
2856                                goto out;
2857                        }
2858                        /* Check if record_id is correct */
2859                        if (fmt_buffer[pos].cyl != geo.cyl ||
2860                            fmt_buffer[pos].head != geo.head ||
2861                            fmt_buffer[pos].record != (j + 1)) {
2862                                cdata->result = DASD_FMT_ERR_RECORD_ID;
2863                                goto out;
2864                        }
2865                }
2866        }
2867
2868out:
2869        /*
2870         * In case of no errors, we need to decrease by one
2871         * to get the correct positions.
2872         */
2873        if (!cdata->result) {
2874                i--;
2875                pos--;
2876        }
2877
2878        cdata->unit = i;
2879        cdata->num_records = count;
2880        cdata->rec = fmt_buffer[pos].record;
2881        cdata->blksize = fmt_buffer[pos].dl;
2882        cdata->key_length = fmt_buffer[pos].kl;
2883}
2884
2885/*
2886 * Check the format of a range of tracks of a DASD.
2887 */
2888static int dasd_eckd_check_device_format(struct dasd_device *base,
2889                                         struct format_check_t *cdata,
2890                                         int enable_pav)
2891{
2892        struct dasd_eckd_private *private = base->private;
2893        struct eckd_count *fmt_buffer;
2894        struct irb irb;
2895        int rpt_max, rpt_exp;
2896        int fmt_buffer_size;
2897        int trk_per_cyl;
2898        int trkcount;
2899        int tpm = 0;
2900        int rc;
2901
2902        trk_per_cyl = private->rdc_data.trk_per_cyl;
2903
2904        /* Get maximum and expected amount of records per track */
2905        rpt_max = recs_per_track(&private->rdc_data, 0, 512) + 1;
2906        rpt_exp = recs_per_track(&private->rdc_data, 0, cdata->expect.blksize);
2907
2908        trkcount = cdata->expect.stop_unit - cdata->expect.start_unit + 1;
2909        fmt_buffer_size = trkcount * rpt_max * sizeof(struct eckd_count);
2910
2911        fmt_buffer = kzalloc(fmt_buffer_size, GFP_KERNEL | GFP_DMA);
2912        if (!fmt_buffer)
2913                return -ENOMEM;
2914
2915        /*
2916         * A certain FICON feature subset is needed to operate in transport
2917         * mode. Additionally, the support for transport mode is implicitly
2918         * checked by comparing the buffer size with fcx_max_data. As long as
2919         * the buffer size is smaller we can operate in transport mode and
2920         * process multiple tracks. If not, only one track at once is being
2921         * processed using command mode.
2922         */
2923        if ((private->features.feature[40] & 0x04) &&
2924            fmt_buffer_size <= private->fcx_max_data)
2925                tpm = 1;
2926
2927        rc = dasd_eckd_format_process_data(base, &cdata->expect, enable_pav,
2928                                           tpm, fmt_buffer, rpt_max, &irb);
2929        if (rc && rc != -EIO)
2930                goto out;
2931        if (rc == -EIO) {
2932                /*
2933                 * If our first attempt with transport mode enabled comes back
2934                 * with an incorrect length error, we're going to retry the
2935                 * check with command mode.
2936                 */
2937                if (tpm && scsw_cstat(&irb.scsw) == 0x40) {
2938                        tpm = 0;
2939                        rc = dasd_eckd_format_process_data(base, &cdata->expect,
2940                                                           enable_pav, tpm,
2941                                                           fmt_buffer, rpt_max,
2942                                                           &irb);
2943                        if (rc)
2944                                goto out;
2945                } else {
2946                        goto out;
2947                }
2948        }
2949
2950        dasd_eckd_format_evaluate_tracks(fmt_buffer, cdata, rpt_max, rpt_exp,
2951                                         trk_per_cyl, tpm);
2952
2953out:
2954        kfree(fmt_buffer);
2955
2956        return rc;
2957}
2958
2959static void dasd_eckd_handle_terminated_request(struct dasd_ccw_req *cqr)
2960{
2961        if (cqr->retries < 0) {
2962                cqr->status = DASD_CQR_FAILED;
2963                return;
2964        }
2965        cqr->status = DASD_CQR_FILLED;
2966        if (cqr->block && (cqr->startdev != cqr->block->base)) {
2967                dasd_eckd_reset_ccw_to_base_io(cqr);
2968                cqr->startdev = cqr->block->base;
2969                cqr->lpm = cqr->block->base->path_data.opm;
2970        }
2971};
2972
2973static dasd_erp_fn_t
2974dasd_eckd_erp_action(struct dasd_ccw_req * cqr)
2975{
2976        struct dasd_device *device = (struct dasd_device *) cqr->startdev;
2977        struct ccw_device *cdev = device->cdev;
2978
2979        switch (cdev->id.cu_type) {
2980        case 0x3990:
2981        case 0x2105:
2982        case 0x2107:
2983        case 0x1750:
2984                return dasd_3990_erp_action;
2985        case 0x9343:
2986        case 0x3880:
2987        default:
2988                return dasd_default_erp_action;
2989        }
2990}
2991
2992static dasd_erp_fn_t
2993dasd_eckd_erp_postaction(struct dasd_ccw_req * cqr)
2994{
2995        return dasd_default_erp_postaction;
2996}
2997
2998static void dasd_eckd_check_for_device_change(struct dasd_device *device,
2999                                              struct dasd_ccw_req *cqr,
3000                                              struct irb *irb)
3001{
3002        char mask;
3003        char *sense = NULL;
3004        struct dasd_eckd_private *private = device->private;
3005
3006        /* first of all check for state change pending interrupt */
3007        mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
3008        if ((scsw_dstat(&irb->scsw) & mask) == mask) {
3009                /*
3010                 * for alias only, not in offline processing
3011                 * and only if not suspended
3012                 */
3013                if (!device->block && private->lcu &&
3014                    device->state == DASD_STATE_ONLINE &&
3015                    !test_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3016                    !test_bit(DASD_FLAG_SUSPENDED, &device->flags)) {
3017                        /* schedule worker to reload device */
3018                        dasd_reload_device(device);
3019                }
3020                dasd_generic_handle_state_change(device);
3021                return;
3022        }
3023
3024        sense = dasd_get_sense(irb);
3025        if (!sense)
3026                return;
3027
3028        /* summary unit check */
3029        if ((sense[27] & DASD_SENSE_BIT_0) && (sense[7] == 0x0D) &&
3030            (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK)) {
3031                if (test_and_set_bit(DASD_FLAG_SUC, &device->flags)) {
3032                        DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3033                                      "eckd suc: device already notified");
3034                        return;
3035                }
3036                sense = dasd_get_sense(irb);
3037                if (!sense) {
3038                        DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3039                                      "eckd suc: no reason code available");
3040                        clear_bit(DASD_FLAG_SUC, &device->flags);
3041                        return;
3042
3043                }
3044                private->suc_reason = sense[8];
3045                DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
3046                              "eckd handle summary unit check: reason",
3047                              private->suc_reason);
3048                dasd_get_device(device);
3049                if (!schedule_work(&device->suc_work))
3050                        dasd_put_device(device);
3051
3052                return;
3053        }
3054
3055        /* service information message SIM */
3056        if (!cqr && !(sense[27] & DASD_SENSE_BIT_0) &&
3057            ((sense[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
3058                dasd_3990_erp_handle_sim(device, sense);
3059                return;
3060        }
3061
3062        /* loss of device reservation is handled via base devices only
3063         * as alias devices may be used with several bases
3064         */
3065        if (device->block && (sense[27] & DASD_SENSE_BIT_0) &&
3066            (sense[7] == 0x3F) &&
3067            (scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
3068            test_bit(DASD_FLAG_IS_RESERVED, &device->flags)) {
3069                if (device->features & DASD_FEATURE_FAILONSLCK)
3070                        set_bit(DASD_FLAG_LOCK_STOLEN, &device->flags);
3071                clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
3072                dev_err(&device->cdev->dev,
3073                        "The device reservation was lost\n");
3074        }
3075}
3076
3077static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_single(
3078                                               struct dasd_device *startdev,
3079                                               struct dasd_block *block,
3080                                               struct request *req,
3081                                               sector_t first_rec,
3082                                               sector_t last_rec,
3083                                               sector_t first_trk,
3084                                               sector_t last_trk,
3085                                               unsigned int first_offs,
3086                                               unsigned int last_offs,
3087                                               unsigned int blk_per_trk,
3088                                               unsigned int blksize)
3089{
3090        struct dasd_eckd_private *private;
3091        unsigned long *idaws;
3092        struct LO_eckd_data *LO_data;
3093        struct dasd_ccw_req *cqr;
3094        struct ccw1 *ccw;
3095        struct req_iterator iter;
3096        struct bio_vec bv;
3097        char *dst;
3098        unsigned int off;
3099        int count, cidaw, cplength, datasize;
3100        sector_t recid;
3101        unsigned char cmd, rcmd;
3102        int use_prefix;
3103        struct dasd_device *basedev;
3104
3105        basedev = block->base;
3106        private = basedev->private;
3107        if (rq_data_dir(req) == READ)
3108                cmd = DASD_ECKD_CCW_READ_MT;
3109        else if (rq_data_dir(req) == WRITE)
3110                cmd = DASD_ECKD_CCW_WRITE_MT;
3111        else
3112                return ERR_PTR(-EINVAL);
3113
3114        /* Check struct bio and count the number of blocks for the request. */
3115        count = 0;
3116        cidaw = 0;
3117        rq_for_each_segment(bv, req, iter) {
3118                if (bv.bv_len & (blksize - 1))
3119                        /* Eckd can only do full blocks. */
3120                        return ERR_PTR(-EINVAL);
3121                count += bv.bv_len >> (block->s2b_shift + 9);
3122                if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
3123                        cidaw += bv.bv_len >> (block->s2b_shift + 9);
3124        }
3125        /* Paranoia. */
3126        if (count != last_rec - first_rec + 1)
3127                return ERR_PTR(-EINVAL);
3128
3129        /* use the prefix command if available */
3130        use_prefix = private->features.feature[8] & 0x01;
3131        if (use_prefix) {
3132                /* 1x prefix + number of blocks */
3133                cplength = 2 + count;
3134                /* 1x prefix + cidaws*sizeof(long) */
3135                datasize = sizeof(struct PFX_eckd_data) +
3136                        sizeof(struct LO_eckd_data) +
3137                        cidaw * sizeof(unsigned long);
3138        } else {
3139                /* 1x define extent + 1x locate record + number of blocks */
3140                cplength = 2 + count;
3141                /* 1x define extent + 1x locate record + cidaws*sizeof(long) */
3142                datasize = sizeof(struct DE_eckd_data) +
3143                        sizeof(struct LO_eckd_data) +
3144                        cidaw * sizeof(unsigned long);
3145        }
3146        /* Find out the number of additional locate record ccws for cdl. */
3147        if (private->uses_cdl && first_rec < 2*blk_per_trk) {
3148                if (last_rec >= 2*blk_per_trk)
3149                        count = 2*blk_per_trk - first_rec;
3150                cplength += count;
3151                datasize += count*sizeof(struct LO_eckd_data);
3152        }
3153        /* Allocate the ccw request. */
3154        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
3155                                   startdev);
3156        if (IS_ERR(cqr))
3157                return cqr;
3158        ccw = cqr->cpaddr;
3159        /* First ccw is define extent or prefix. */
3160        if (use_prefix) {
3161                if (prefix(ccw++, cqr->data, first_trk,
3162                           last_trk, cmd, basedev, startdev) == -EAGAIN) {
3163                        /* Clock not in sync and XRC is enabled.
3164                         * Try again later.
3165                         */
3166                        dasd_sfree_request(cqr, startdev);
3167                        return ERR_PTR(-EAGAIN);
3168                }
3169                idaws = (unsigned long *) (cqr->data +
3170                                           sizeof(struct PFX_eckd_data));
3171        } else {
3172                if (define_extent(ccw++, cqr->data, first_trk,
3173                                  last_trk, cmd, basedev) == -EAGAIN) {
3174                        /* Clock not in sync and XRC is enabled.
3175                         * Try again later.
3176                         */
3177                        dasd_sfree_request(cqr, startdev);
3178                        return ERR_PTR(-EAGAIN);
3179                }
3180                idaws = (unsigned long *) (cqr->data +
3181                                           sizeof(struct DE_eckd_data));
3182        }
3183        /* Build locate_record+read/write/ccws. */
3184        LO_data = (struct LO_eckd_data *) (idaws + cidaw);
3185        recid = first_rec;
3186        if (private->uses_cdl == 0 || recid > 2*blk_per_trk) {
3187                /* Only standard blocks so there is just one locate record. */
3188                ccw[-1].flags |= CCW_FLAG_CC;
3189                locate_record(ccw++, LO_data++, first_trk, first_offs + 1,
3190                              last_rec - recid + 1, cmd, basedev, blksize);
3191        }
3192        rq_for_each_segment(bv, req, iter) {
3193                dst = page_address(bv.bv_page) + bv.bv_offset;
3194                if (dasd_page_cache) {
3195                        char *copy = kmem_cache_alloc(dasd_page_cache,
3196                                                      GFP_DMA | __GFP_NOWARN);
3197                        if (copy && rq_data_dir(req) == WRITE)
3198                                memcpy(copy + bv.bv_offset, dst, bv.bv_len);
3199                        if (copy)
3200                                dst = copy + bv.bv_offset;
3201                }
3202                for (off = 0; off < bv.bv_len; off += blksize) {
3203                        sector_t trkid = recid;
3204                        unsigned int recoffs = sector_div(trkid, blk_per_trk);
3205                        rcmd = cmd;
3206                        count = blksize;
3207                        /* Locate record for cdl special block ? */
3208                        if (private->uses_cdl && recid < 2*blk_per_trk) {
3209                                if (dasd_eckd_cdl_special(blk_per_trk, recid)){
3210                                        rcmd |= 0x8;
3211                                        count = dasd_eckd_cdl_reclen(recid);
3212                                        if (count < blksize &&
3213                                            rq_data_dir(req) == READ)
3214                                                memset(dst + count, 0xe5,
3215                                                       blksize - count);
3216                                }
3217                                ccw[-1].flags |= CCW_FLAG_CC;
3218                                locate_record(ccw++, LO_data++,
3219                                              trkid, recoffs + 1,
3220                                              1, rcmd, basedev, count);
3221                        }
3222                        /* Locate record for standard blocks ? */
3223                        if (private->uses_cdl && recid == 2*blk_per_trk) {
3224                                ccw[-1].flags |= CCW_FLAG_CC;
3225                                locate_record(ccw++, LO_data++,
3226                                              trkid, recoffs + 1,
3227                                              last_rec - recid + 1,
3228                                              cmd, basedev, count);
3229                        }
3230                        /* Read/write ccw. */
3231                        ccw[-1].flags |= CCW_FLAG_CC;
3232                        ccw->cmd_code = rcmd;
3233                        ccw->count = count;
3234                        if (idal_is_needed(dst, blksize)) {
3235                                ccw->cda = (__u32)(addr_t) idaws;
3236                                ccw->flags = CCW_FLAG_IDA;
3237                                idaws = idal_create_words(idaws, dst, blksize);
3238                        } else {
3239                                ccw->cda = (__u32)(addr_t) dst;
3240                                ccw->flags = 0;
3241                        }
3242                        ccw++;
3243                        dst += blksize;
3244                        recid++;
3245                }
3246        }
3247        if (blk_noretry_request(req) ||
3248            block->base->features & DASD_FEATURE_FAILFAST)
3249                set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3250        cqr->startdev = startdev;
3251        cqr->memdev = startdev;
3252        cqr->block = block;
3253        cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
3254        cqr->lpm = startdev->path_data.ppm;
3255        cqr->retries = startdev->default_retries;
3256        cqr->buildclk = get_tod_clock();
3257        cqr->status = DASD_CQR_FILLED;
3258        return cqr;
3259}
3260
3261static struct dasd_ccw_req *dasd_eckd_build_cp_cmd_track(
3262                                               struct dasd_device *startdev,
3263                                               struct dasd_block *block,
3264                                               struct request *req,
3265                                               sector_t first_rec,
3266                                               sector_t last_rec,
3267                                               sector_t first_trk,
3268                                               sector_t last_trk,
3269                                               unsigned int first_offs,
3270                                               unsigned int last_offs,
3271                                               unsigned int blk_per_trk,
3272                                               unsigned int blksize)
3273{
3274        unsigned long *idaws;
3275        struct dasd_ccw_req *cqr;
3276        struct ccw1 *ccw;
3277        struct req_iterator iter;
3278        struct bio_vec bv;
3279        char *dst, *idaw_dst;
3280        unsigned int cidaw, cplength, datasize;
3281        unsigned int tlf;
3282        sector_t recid;
3283        unsigned char cmd;
3284        struct dasd_device *basedev;
3285        unsigned int trkcount, count, count_to_trk_end;
3286        unsigned int idaw_len, seg_len, part_len, len_to_track_end;
3287        unsigned char new_track, end_idaw;
3288        sector_t trkid;
3289        unsigned int recoffs;
3290
3291        basedev = block->base;
3292        if (rq_data_dir(req) == READ)
3293                cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
3294        else if (rq_data_dir(req) == WRITE)
3295                cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
3296        else
3297                return ERR_PTR(-EINVAL);
3298
3299        /* Track based I/O needs IDAWs for each page, and not just for
3300         * 64 bit addresses. We need additional idals for pages
3301         * that get filled from two tracks, so we use the number
3302         * of records as upper limit.
3303         */
3304        cidaw = last_rec - first_rec + 1;
3305        trkcount = last_trk - first_trk + 1;
3306
3307        /* 1x prefix + one read/write ccw per track */
3308        cplength = 1 + trkcount;
3309
3310        /* on 31-bit we need space for two 32 bit addresses per page
3311         * on 64-bit one 64 bit address
3312         */
3313        datasize = sizeof(struct PFX_eckd_data) +
3314                cidaw * sizeof(unsigned long long);
3315
3316        /* Allocate the ccw request. */
3317        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength, datasize,
3318                                   startdev);
3319        if (IS_ERR(cqr))
3320                return cqr;
3321        ccw = cqr->cpaddr;
3322        /* transfer length factor: how many bytes to read from the last track */
3323        if (first_trk == last_trk)
3324                tlf = last_offs - first_offs + 1;
3325        else
3326                tlf = last_offs + 1;
3327        tlf *= blksize;
3328
3329        if (prefix_LRE(ccw++, cqr->data, first_trk,
3330                       last_trk, cmd, basedev, startdev,
3331                       1 /* format */, first_offs + 1,
3332                       trkcount, blksize,
3333                       tlf) == -EAGAIN) {
3334                /* Clock not in sync and XRC is enabled.
3335                 * Try again later.
3336                 */
3337                dasd_sfree_request(cqr, startdev);
3338                return ERR_PTR(-EAGAIN);
3339        }
3340
3341        /*
3342         * The translation of request into ccw programs must meet the
3343         * following conditions:
3344         * - all idaws but the first and the last must address full pages
3345         *   (or 2K blocks on 31-bit)
3346         * - the scope of a ccw and it's idal ends with the track boundaries
3347         */
3348        idaws = (unsigned long *) (cqr->data + sizeof(struct PFX_eckd_data));
3349        recid = first_rec;
3350        new_track = 1;
3351        end_idaw = 0;
3352        len_to_track_end = 0;
3353        idaw_dst = NULL;
3354        idaw_len = 0;
3355        rq_for_each_segment(bv, req, iter) {
3356                dst = page_address(bv.bv_page) + bv.bv_offset;
3357                seg_len = bv.bv_len;
3358                while (seg_len) {
3359                        if (new_track) {
3360                                trkid = recid;
3361                                recoffs = sector_div(trkid, blk_per_trk);
3362                                count_to_trk_end = blk_per_trk - recoffs;
3363                                count = min((last_rec - recid + 1),
3364                                            (sector_t)count_to_trk_end);
3365                                len_to_track_end = count * blksize;
3366                                ccw[-1].flags |= CCW_FLAG_CC;
3367                                ccw->cmd_code = cmd;
3368                                ccw->count = len_to_track_end;
3369                                ccw->cda = (__u32)(addr_t)idaws;
3370                                ccw->flags = CCW_FLAG_IDA;
3371                                ccw++;
3372                                recid += count;
3373                                new_track = 0;
3374                                /* first idaw for a ccw may start anywhere */
3375                                if (!idaw_dst)
3376                                        idaw_dst = dst;
3377                        }
3378                        /* If we start a new idaw, we must make sure that it
3379                         * starts on an IDA_BLOCK_SIZE boundary.
3380                         * If we continue an idaw, we must make sure that the
3381                         * current segment begins where the so far accumulated
3382                         * idaw ends
3383                         */
3384                        if (!idaw_dst) {
3385                                if (__pa(dst) & (IDA_BLOCK_SIZE-1)) {
3386                                        dasd_sfree_request(cqr, startdev);
3387                                        return ERR_PTR(-ERANGE);
3388                                } else
3389                                        idaw_dst = dst;
3390                        }
3391                        if ((idaw_dst + idaw_len) != dst) {
3392                                dasd_sfree_request(cqr, startdev);
3393                                return ERR_PTR(-ERANGE);
3394                        }
3395                        part_len = min(seg_len, len_to_track_end);
3396                        seg_len -= part_len;
3397                        dst += part_len;
3398                        idaw_len += part_len;
3399                        len_to_track_end -= part_len;
3400                        /* collected memory area ends on an IDA_BLOCK border,
3401                         * -> create an idaw
3402                         * idal_create_words will handle cases where idaw_len
3403                         * is larger then IDA_BLOCK_SIZE
3404                         */
3405                        if (!(__pa(idaw_dst + idaw_len) & (IDA_BLOCK_SIZE-1)))
3406                                end_idaw = 1;
3407                        /* We also need to end the idaw at track end */
3408                        if (!len_to_track_end) {
3409                                new_track = 1;
3410                                end_idaw = 1;
3411                        }
3412                        if (end_idaw) {
3413                                idaws = idal_create_words(idaws, idaw_dst,
3414                                                          idaw_len);
3415                                idaw_dst = NULL;
3416                                idaw_len = 0;
3417                                end_idaw = 0;
3418                        }
3419                }
3420        }
3421
3422        if (blk_noretry_request(req) ||
3423            block->base->features & DASD_FEATURE_FAILFAST)
3424                set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3425        cqr->startdev = startdev;
3426        cqr->memdev = startdev;
3427        cqr->block = block;
3428        cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
3429        cqr->lpm = startdev->path_data.ppm;
3430        cqr->retries = startdev->default_retries;
3431        cqr->buildclk = get_tod_clock();
3432        cqr->status = DASD_CQR_FILLED;
3433        return cqr;
3434}
3435
3436static int prepare_itcw(struct itcw *itcw,
3437                        unsigned int trk, unsigned int totrk, int cmd,
3438                        struct dasd_device *basedev,
3439                        struct dasd_device *startdev,
3440                        unsigned int rec_on_trk, int count,
3441                        unsigned int blksize,
3442                        unsigned int total_data_size,
3443                        unsigned int tlf,
3444                        unsigned int blk_per_trk)
3445{
3446        struct PFX_eckd_data pfxdata;
3447        struct dasd_eckd_private *basepriv, *startpriv;
3448        struct DE_eckd_data *dedata;
3449        struct LRE_eckd_data *lredata;
3450        struct dcw *dcw;
3451
3452        u32 begcyl, endcyl;
3453        u16 heads, beghead, endhead;
3454        u8 pfx_cmd;
3455
3456        int rc = 0;
3457        int sector = 0;
3458        int dn, d;
3459
3460
3461        /* setup prefix data */
3462        basepriv = basedev->private;
3463        startpriv = startdev->private;
3464        dedata = &pfxdata.define_extent;
3465        lredata = &pfxdata.locate_record;
3466
3467        memset(&pfxdata, 0, sizeof(pfxdata));
3468        pfxdata.format = 1; /* PFX with LRE */
3469        pfxdata.base_address = basepriv->ned->unit_addr;
3470        pfxdata.base_lss = basepriv->ned->ID;
3471        pfxdata.validity.define_extent = 1;
3472
3473        /* private uid is kept up to date, conf_data may be outdated */
3474        if (startpriv->uid.type != UA_BASE_DEVICE) {
3475                pfxdata.validity.verify_base = 1;
3476                if (startpriv->uid.type == UA_HYPER_PAV_ALIAS)
3477                        pfxdata.validity.hyper_pav = 1;
3478        }
3479
3480        switch (cmd) {
3481        case DASD_ECKD_CCW_READ_TRACK_DATA:
3482                dedata->mask.perm = 0x1;
3483                dedata->attributes.operation = basepriv->attrib.operation;
3484                dedata->blk_size = blksize;
3485                dedata->ga_extended |= 0x42;
3486                lredata->operation.orientation = 0x0;
3487                lredata->operation.operation = 0x0C;
3488                lredata->auxiliary.check_bytes = 0x01;
3489                pfx_cmd = DASD_ECKD_CCW_PFX_READ;
3490                break;
3491        case DASD_ECKD_CCW_WRITE_TRACK_DATA:
3492                dedata->mask.perm = 0x02;
3493                dedata->attributes.operation = basepriv->attrib.operation;
3494                dedata->blk_size = blksize;
3495                rc = check_XRC_on_prefix(&pfxdata, basedev);
3496                dedata->ga_extended |= 0x42;
3497                lredata->operation.orientation = 0x0;
3498                lredata->operation.operation = 0x3F;
3499                lredata->extended_operation = 0x23;
3500                lredata->auxiliary.check_bytes = 0x2;
3501                pfx_cmd = DASD_ECKD_CCW_PFX;
3502                break;
3503        case DASD_ECKD_CCW_READ_COUNT_MT:
3504                dedata->mask.perm = 0x1;
3505                dedata->attributes.operation = DASD_BYPASS_CACHE;
3506                dedata->ga_extended |= 0x42;
3507                dedata->blk_size = blksize;
3508                lredata->operation.orientation = 0x2;
3509                lredata->operation.operation = 0x16;
3510                lredata->auxiliary.check_bytes = 0x01;
3511                pfx_cmd = DASD_ECKD_CCW_PFX_READ;
3512                break;
3513        default:
3514                DBF_DEV_EVENT(DBF_ERR, basedev,
3515                              "prepare itcw, unknown opcode 0x%x", cmd);
3516                BUG();
3517                break;
3518        }
3519        if (rc)
3520                return rc;
3521
3522        dedata->attributes.mode = 0x3;  /* ECKD */
3523
3524        heads = basepriv->rdc_data.trk_per_cyl;
3525        begcyl = trk / heads;
3526        beghead = trk % heads;
3527        endcyl = totrk / heads;
3528        endhead = totrk % heads;
3529
3530        /* check for sequential prestage - enhance cylinder range */
3531        if (dedata->attributes.operation == DASD_SEQ_PRESTAGE ||
3532            dedata->attributes.operation == DASD_SEQ_ACCESS) {
3533
3534                if (endcyl + basepriv->attrib.nr_cyl < basepriv->real_cyl)
3535                        endcyl += basepriv->attrib.nr_cyl;
3536                else
3537                        endcyl = (basepriv->real_cyl - 1);
3538        }
3539
3540        set_ch_t(&dedata->beg_ext, begcyl, beghead);
3541        set_ch_t(&dedata->end_ext, endcyl, endhead);
3542
3543        dedata->ep_format = 0x20; /* records per track is valid */
3544        dedata->ep_rec_per_track = blk_per_trk;
3545
3546        if (rec_on_trk) {
3547                switch (basepriv->rdc_data.dev_type) {
3548                case 0x3390:
3549                        dn = ceil_quot(blksize + 6, 232);
3550                        d = 9 + ceil_quot(blksize + 6 * (dn + 1), 34);
3551                        sector = (49 + (rec_on_trk - 1) * (10 + d)) / 8;
3552                        break;
3553                case 0x3380:
3554                        d = 7 + ceil_quot(blksize + 12, 32);
3555                        sector = (39 + (rec_on_trk - 1) * (8 + d)) / 7;
3556                        break;
3557                }
3558        }
3559
3560        if (cmd == DASD_ECKD_CCW_READ_COUNT_MT) {
3561                lredata->auxiliary.length_valid = 0;
3562                lredata->auxiliary.length_scope = 0;
3563                lredata->sector = 0xff;
3564        } else {
3565                lredata->auxiliary.length_valid = 1;
3566                lredata->auxiliary.length_scope = 1;
3567                lredata->sector = sector;
3568        }
3569        lredata->auxiliary.imbedded_ccw_valid = 1;
3570        lredata->length = tlf;
3571        lredata->imbedded_ccw = cmd;
3572        lredata->count = count;
3573        set_ch_t(&lredata->seek_addr, begcyl, beghead);
3574        lredata->search_arg.cyl = lredata->seek_addr.cyl;
3575        lredata->search_arg.head = lredata->seek_addr.head;
3576        lredata->search_arg.record = rec_on_trk;
3577
3578        dcw = itcw_add_dcw(itcw, pfx_cmd, 0,
3579                     &pfxdata, sizeof(pfxdata), total_data_size);
3580        return PTR_RET(dcw);
3581}
3582
3583static struct dasd_ccw_req *dasd_eckd_build_cp_tpm_track(
3584                                               struct dasd_device *startdev,
3585                                               struct dasd_block *block,
3586                                               struct request *req,
3587                                               sector_t first_rec,
3588                                               sector_t last_rec,
3589                                               sector_t first_trk,
3590                                               sector_t last_trk,
3591                                               unsigned int first_offs,
3592                                               unsigned int last_offs,
3593                                               unsigned int blk_per_trk,
3594                                               unsigned int blksize)
3595{
3596        struct dasd_ccw_req *cqr;
3597        struct req_iterator iter;
3598        struct bio_vec bv;
3599        char *dst;
3600        unsigned int trkcount, ctidaw;
3601        unsigned char cmd;
3602        struct dasd_device *basedev;
3603        unsigned int tlf;
3604        struct itcw *itcw;
3605        struct tidaw *last_tidaw = NULL;
3606        int itcw_op;
3607        size_t itcw_size;
3608        u8 tidaw_flags;
3609        unsigned int seg_len, part_len, len_to_track_end;
3610        unsigned char new_track;
3611        sector_t recid, trkid;
3612        unsigned int offs;
3613        unsigned int count, count_to_trk_end;
3614        int ret;
3615
3616        basedev = block->base;
3617        if (rq_data_dir(req) == READ) {
3618                cmd = DASD_ECKD_CCW_READ_TRACK_DATA;
3619                itcw_op = ITCW_OP_READ;
3620        } else if (rq_data_dir(req) == WRITE) {
3621                cmd = DASD_ECKD_CCW_WRITE_TRACK_DATA;
3622                itcw_op = ITCW_OP_WRITE;
3623        } else
3624                return ERR_PTR(-EINVAL);
3625
3626        /* trackbased I/O needs address all memory via TIDAWs,
3627         * not just for 64 bit addresses. This allows us to map
3628         * each segment directly to one tidaw.
3629         * In the case of write requests, additional tidaws may
3630         * be needed when a segment crosses a track boundary.
3631         */
3632        trkcount = last_trk - first_trk + 1;
3633        ctidaw = 0;
3634        rq_for_each_segment(bv, req, iter) {
3635                ++ctidaw;
3636        }
3637        if (rq_data_dir(req) == WRITE)
3638                ctidaw += (last_trk - first_trk);
3639
3640        /* Allocate the ccw request. */
3641        itcw_size = itcw_calc_size(0, ctidaw, 0);
3642        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 0, itcw_size, startdev);
3643        if (IS_ERR(cqr))
3644                return cqr;
3645
3646        /* transfer length factor: how many bytes to read from the last track */
3647        if (first_trk == last_trk)
3648                tlf = last_offs - first_offs + 1;
3649        else
3650                tlf = last_offs + 1;
3651        tlf *= blksize;
3652
3653        itcw = itcw_init(cqr->data, itcw_size, itcw_op, 0, ctidaw, 0);
3654        if (IS_ERR(itcw)) {
3655                ret = -EINVAL;
3656                goto out_error;
3657        }
3658        cqr->cpaddr = itcw_get_tcw(itcw);
3659        if (prepare_itcw(itcw, first_trk, last_trk,
3660                         cmd, basedev, startdev,
3661                         first_offs + 1,
3662                         trkcount, blksize,
3663                         (last_rec - first_rec + 1) * blksize,
3664                         tlf, blk_per_trk) == -EAGAIN) {
3665                /* Clock not in sync and XRC is enabled.
3666                 * Try again later.
3667                 */
3668                ret = -EAGAIN;
3669                goto out_error;
3670        }
3671        len_to_track_end = 0;
3672        /*
3673         * A tidaw can address 4k of memory, but must not cross page boundaries
3674         * We can let the block layer handle this by setting
3675         * blk_queue_segment_boundary to page boundaries and
3676         * blk_max_segment_size to page size when setting up the request queue.
3677         * For write requests, a TIDAW must not cross track boundaries, because
3678         * we have to set the CBC flag on the last tidaw for each track.
3679         */
3680        if (rq_data_dir(req) == WRITE) {
3681                new_track = 1;
3682                recid = first_rec;
3683                rq_for_each_segment(bv, req, iter) {
3684                        dst = page_address(bv.bv_page) + bv.bv_offset;
3685                        seg_len = bv.bv_len;
3686                        while (seg_len) {
3687                                if (new_track) {
3688                                        trkid = recid;
3689                                        offs = sector_div(trkid, blk_per_trk);
3690                                        count_to_trk_end = blk_per_trk - offs;
3691                                        count = min((last_rec - recid + 1),
3692                                                    (sector_t)count_to_trk_end);
3693                                        len_to_track_end = count * blksize;
3694                                        recid += count;
3695                                        new_track = 0;
3696                                }
3697                                part_len = min(seg_len, len_to_track_end);
3698                                seg_len -= part_len;
3699                                len_to_track_end -= part_len;
3700                                /* We need to end the tidaw at track end */
3701                                if (!len_to_track_end) {
3702                                        new_track = 1;
3703                                        tidaw_flags = TIDAW_FLAGS_INSERT_CBC;
3704                                } else
3705                                        tidaw_flags = 0;
3706                                last_tidaw = itcw_add_tidaw(itcw, tidaw_flags,
3707                                                            dst, part_len);
3708                                if (IS_ERR(last_tidaw)) {
3709                                        ret = -EINVAL;
3710                                        goto out_error;
3711                                }
3712                                dst += part_len;
3713                        }
3714                }
3715        } else {
3716                rq_for_each_segment(bv, req, iter) {
3717                        dst = page_address(bv.bv_page) + bv.bv_offset;
3718                        last_tidaw = itcw_add_tidaw(itcw, 0x00,
3719                                                    dst, bv.bv_len);
3720                        if (IS_ERR(last_tidaw)) {
3721                                ret = -EINVAL;
3722                                goto out_error;
3723                        }
3724                }
3725        }
3726        last_tidaw->flags |= TIDAW_FLAGS_LAST;
3727        last_tidaw->flags &= ~TIDAW_FLAGS_INSERT_CBC;
3728        itcw_finalize(itcw);
3729
3730        if (blk_noretry_request(req) ||
3731            block->base->features & DASD_FEATURE_FAILFAST)
3732                set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3733        cqr->cpmode = 1;
3734        cqr->startdev = startdev;
3735        cqr->memdev = startdev;
3736        cqr->block = block;
3737        cqr->expires = startdev->default_expires * HZ;  /* default 5 minutes */
3738        cqr->lpm = startdev->path_data.ppm;
3739        cqr->retries = startdev->default_retries;
3740        cqr->buildclk = get_tod_clock();
3741        cqr->status = DASD_CQR_FILLED;
3742        return cqr;
3743out_error:
3744        dasd_sfree_request(cqr, startdev);
3745        return ERR_PTR(ret);
3746}
3747
3748static struct dasd_ccw_req *dasd_eckd_build_cp(struct dasd_device *startdev,
3749                                               struct dasd_block *block,
3750                                               struct request *req)
3751{
3752        int cmdrtd, cmdwtd;
3753        int use_prefix;
3754        int fcx_multitrack;
3755        struct dasd_eckd_private *private;
3756        struct dasd_device *basedev;
3757        sector_t first_rec, last_rec;
3758        sector_t first_trk, last_trk;
3759        unsigned int first_offs, last_offs;
3760        unsigned int blk_per_trk, blksize;
3761        int cdlspecial;
3762        unsigned int data_size;
3763        struct dasd_ccw_req *cqr;
3764
3765        basedev = block->base;
3766        private = basedev->private;
3767
3768        /* Calculate number of blocks/records per track. */
3769        blksize = block->bp_block;
3770        blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3771        if (blk_per_trk == 0)
3772                return ERR_PTR(-EINVAL);
3773        /* Calculate record id of first and last block. */
3774        first_rec = first_trk = blk_rq_pos(req) >> block->s2b_shift;
3775        first_offs = sector_div(first_trk, blk_per_trk);
3776        last_rec = last_trk =
3777                (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
3778        last_offs = sector_div(last_trk, blk_per_trk);
3779        cdlspecial = (private->uses_cdl && first_rec < 2*blk_per_trk);
3780
3781        fcx_multitrack = private->features.feature[40] & 0x20;
3782        data_size = blk_rq_bytes(req);
3783        if (data_size % blksize)
3784                return ERR_PTR(-EINVAL);
3785        /* tpm write request add CBC data on each track boundary */
3786        if (rq_data_dir(req) == WRITE)
3787                data_size += (last_trk - first_trk) * 4;
3788
3789        /* is read track data and write track data in command mode supported? */
3790        cmdrtd = private->features.feature[9] & 0x20;
3791        cmdwtd = private->features.feature[12] & 0x40;
3792        use_prefix = private->features.feature[8] & 0x01;
3793
3794        cqr = NULL;
3795        if (cdlspecial || dasd_page_cache) {
3796                /* do nothing, just fall through to the cmd mode single case */
3797        } else if ((data_size <= private->fcx_max_data)
3798                   && (fcx_multitrack || (first_trk == last_trk))) {
3799                cqr = dasd_eckd_build_cp_tpm_track(startdev, block, req,
3800                                                    first_rec, last_rec,
3801                                                    first_trk, last_trk,
3802                                                    first_offs, last_offs,
3803                                                    blk_per_trk, blksize);
3804                if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3805                    (PTR_ERR(cqr) != -ENOMEM))
3806                        cqr = NULL;
3807        } else if (use_prefix &&
3808                   (((rq_data_dir(req) == READ) && cmdrtd) ||
3809                    ((rq_data_dir(req) == WRITE) && cmdwtd))) {
3810                cqr = dasd_eckd_build_cp_cmd_track(startdev, block, req,
3811                                                   first_rec, last_rec,
3812                                                   first_trk, last_trk,
3813                                                   first_offs, last_offs,
3814                                                   blk_per_trk, blksize);
3815                if (IS_ERR(cqr) && (PTR_ERR(cqr) != -EAGAIN) &&
3816                    (PTR_ERR(cqr) != -ENOMEM))
3817                        cqr = NULL;
3818        }
3819        if (!cqr)
3820                cqr = dasd_eckd_build_cp_cmd_single(startdev, block, req,
3821                                                    first_rec, last_rec,
3822                                                    first_trk, last_trk,
3823                                                    first_offs, last_offs,
3824                                                    blk_per_trk, blksize);
3825        return cqr;
3826}
3827
3828static struct dasd_ccw_req *dasd_raw_build_cp(struct dasd_device *startdev,
3829                                               struct dasd_block *block,
3830                                               struct request *req)
3831{
3832        unsigned long *idaws;
3833        struct dasd_device *basedev;
3834        struct dasd_ccw_req *cqr;
3835        struct ccw1 *ccw;
3836        struct req_iterator iter;
3837        struct bio_vec bv;
3838        char *dst;
3839        unsigned char cmd;
3840        unsigned int trkcount;
3841        unsigned int seg_len, len_to_track_end;
3842        unsigned int first_offs;
3843        unsigned int cidaw, cplength, datasize;
3844        sector_t first_trk, last_trk, sectors;
3845        sector_t start_padding_sectors, end_sector_offset, end_padding_sectors;
3846        unsigned int pfx_datasize;
3847
3848        /*
3849         * raw track access needs to be mutiple of 64k and on 64k boundary
3850         * For read requests we can fix an incorrect alignment by padding
3851         * the request with dummy pages.
3852         */
3853        start_padding_sectors = blk_rq_pos(req) % DASD_RAW_SECTORS_PER_TRACK;
3854        end_sector_offset = (blk_rq_pos(req) + blk_rq_sectors(req)) %
3855                DASD_RAW_SECTORS_PER_TRACK;
3856        end_padding_sectors = (DASD_RAW_SECTORS_PER_TRACK - end_sector_offset) %
3857                DASD_RAW_SECTORS_PER_TRACK;
3858        basedev = block->base;
3859        if ((start_padding_sectors || end_padding_sectors) &&
3860            (rq_data_dir(req) == WRITE)) {
3861                DBF_DEV_EVENT(DBF_ERR, basedev,
3862                              "raw write not track aligned (%lu,%lu) req %p",
3863                              start_padding_sectors, end_padding_sectors, req);
3864                cqr = ERR_PTR(-EINVAL);
3865                goto out;
3866        }
3867
3868        first_trk = blk_rq_pos(req) / DASD_RAW_SECTORS_PER_TRACK;
3869        last_trk = (blk_rq_pos(req) + blk_rq_sectors(req) - 1) /
3870                DASD_RAW_SECTORS_PER_TRACK;
3871        trkcount = last_trk - first_trk + 1;
3872        first_offs = 0;
3873
3874        if (rq_data_dir(req) == READ)
3875                cmd = DASD_ECKD_CCW_READ_TRACK;
3876        else if (rq_data_dir(req) == WRITE)
3877                cmd = DASD_ECKD_CCW_WRITE_FULL_TRACK;
3878        else {
3879                cqr = ERR_PTR(-EINVAL);
3880                goto out;
3881        }
3882
3883        /*
3884         * Raw track based I/O needs IDAWs for each page,
3885         * and not just for 64 bit addresses.
3886         */
3887        cidaw = trkcount * DASD_RAW_BLOCK_PER_TRACK;
3888
3889        /* 1x prefix + one read/write ccw per track */
3890        cplength = 1 + trkcount;
3891
3892        /*
3893         * struct PFX_eckd_data has up to 2 byte as extended parameter
3894         * this is needed for write full track and has to be mentioned
3895         * separately
3896         * add 8 instead of 2 to keep 8 byte boundary
3897         */
3898        pfx_datasize = sizeof(struct PFX_eckd_data) + 8;
3899
3900        datasize = pfx_datasize + cidaw * sizeof(unsigned long long);
3901
3902        /* Allocate the ccw request. */
3903        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, cplength,
3904                                   datasize, startdev);
3905        if (IS_ERR(cqr))
3906                goto out;
3907        ccw = cqr->cpaddr;
3908
3909        if (prefix_LRE(ccw++, cqr->data, first_trk, last_trk, cmd,
3910                       basedev, startdev, 1 /* format */, first_offs + 1,
3911                       trkcount, 0, 0) == -EAGAIN) {
3912                /* Clock not in sync and XRC is enabled.
3913                 * Try again later.
3914                 */
3915                dasd_sfree_request(cqr, startdev);
3916                cqr = ERR_PTR(-EAGAIN);
3917                goto out;
3918        }
3919
3920        idaws = (unsigned long *)(cqr->data + pfx_datasize);
3921        len_to_track_end = 0;
3922        if (start_padding_sectors) {
3923                ccw[-1].flags |= CCW_FLAG_CC;
3924                ccw->cmd_code = cmd;
3925                /* maximum 3390 track size */
3926                ccw->count = 57326;
3927                /* 64k map to one track */
3928                len_to_track_end = 65536 - start_padding_sectors * 512;
3929                ccw->cda = (__u32)(addr_t)idaws;
3930                ccw->flags |= CCW_FLAG_IDA;
3931                ccw->flags |= CCW_FLAG_SLI;
3932                ccw++;
3933                for (sectors = 0; sectors < start_padding_sectors; sectors += 8)
3934                        idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3935        }
3936        rq_for_each_segment(bv, req, iter) {
3937                dst = page_address(bv.bv_page) + bv.bv_offset;
3938                seg_len = bv.bv_len;
3939                if (cmd == DASD_ECKD_CCW_READ_TRACK)
3940                        memset(dst, 0, seg_len);
3941                if (!len_to_track_end) {
3942                        ccw[-1].flags |= CCW_FLAG_CC;
3943                        ccw->cmd_code = cmd;
3944                        /* maximum 3390 track size */
3945                        ccw->count = 57326;
3946                        /* 64k map to one track */
3947                        len_to_track_end = 65536;
3948                        ccw->cda = (__u32)(addr_t)idaws;
3949                        ccw->flags |= CCW_FLAG_IDA;
3950                        ccw->flags |= CCW_FLAG_SLI;
3951                        ccw++;
3952                }
3953                len_to_track_end -= seg_len;
3954                idaws = idal_create_words(idaws, dst, seg_len);
3955        }
3956        for (sectors = 0; sectors < end_padding_sectors; sectors += 8)
3957                idaws = idal_create_words(idaws, rawpadpage, PAGE_SIZE);
3958        if (blk_noretry_request(req) ||
3959            block->base->features & DASD_FEATURE_FAILFAST)
3960                set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
3961        cqr->startdev = startdev;
3962        cqr->memdev = startdev;
3963        cqr->block = block;
3964        cqr->expires = startdev->default_expires * HZ;
3965        cqr->lpm = startdev->path_data.ppm;
3966        cqr->retries = startdev->default_retries;
3967        cqr->buildclk = get_tod_clock();
3968        cqr->status = DASD_CQR_FILLED;
3969
3970        if (IS_ERR(cqr) && PTR_ERR(cqr) != -EAGAIN)
3971                cqr = NULL;
3972out:
3973        return cqr;
3974}
3975
3976
3977static int
3978dasd_eckd_free_cp(struct dasd_ccw_req *cqr, struct request *req)
3979{
3980        struct dasd_eckd_private *private;
3981        struct ccw1 *ccw;
3982        struct req_iterator iter;
3983        struct bio_vec bv;
3984        char *dst, *cda;
3985        unsigned int blksize, blk_per_trk, off;
3986        sector_t recid;
3987        int status;
3988
3989        if (!dasd_page_cache)
3990                goto out;
3991        private = cqr->block->base->private;
3992        blksize = cqr->block->bp_block;
3993        blk_per_trk = recs_per_track(&private->rdc_data, 0, blksize);
3994        recid = blk_rq_pos(req) >> cqr->block->s2b_shift;
3995        ccw = cqr->cpaddr;
3996        /* Skip over define extent & locate record. */
3997        ccw++;
3998        if (private->uses_cdl == 0 || recid > 2*blk_per_trk)
3999                ccw++;
4000        rq_for_each_segment(bv, req, iter) {
4001                dst = page_address(bv.bv_page) + bv.bv_offset;
4002                for (off = 0; off < bv.bv_len; off += blksize) {
4003                        /* Skip locate record. */
4004                        if (private->uses_cdl && recid <= 2*blk_per_trk)
4005                                ccw++;
4006                        if (dst) {
4007                                if (ccw->flags & CCW_FLAG_IDA)
4008                                        cda = *((char **)((addr_t) ccw->cda));
4009                                else
4010                                        cda = (char *)((addr_t) ccw->cda);
4011                                if (dst != cda) {
4012                                        if (rq_data_dir(req) == READ)
4013                                                memcpy(dst, cda, bv.bv_len);
4014                                        kmem_cache_free(dasd_page_cache,
4015                                            (void *)((addr_t)cda & PAGE_MASK));
4016                                }
4017                                dst = NULL;
4018                        }
4019                        ccw++;
4020                        recid++;
4021                }
4022        }
4023out:
4024        status = cqr->status == DASD_CQR_DONE;
4025        dasd_sfree_request(cqr, cqr->memdev);
4026        return status;
4027}
4028
4029/*
4030 * Modify ccw/tcw in cqr so it can be started on a base device.
4031 *
4032 * Note that this is not enough to restart the cqr!
4033 * Either reset cqr->startdev as well (summary unit check handling)
4034 * or restart via separate cqr (as in ERP handling).
4035 */
4036void dasd_eckd_reset_ccw_to_base_io(struct dasd_ccw_req *cqr)
4037{
4038        struct ccw1 *ccw;
4039        struct PFX_eckd_data *pfxdata;
4040        struct tcw *tcw;
4041        struct tccb *tccb;
4042        struct dcw *dcw;
4043
4044        if (cqr->cpmode == 1) {
4045                tcw = cqr->cpaddr;
4046                tccb = tcw_get_tccb(tcw);
4047                dcw = (struct dcw *)&tccb->tca[0];
4048                pfxdata = (struct PFX_eckd_data *)&dcw->cd[0];
4049                pfxdata->validity.verify_base = 0;
4050                pfxdata->validity.hyper_pav = 0;
4051        } else {
4052                ccw = cqr->cpaddr;
4053                pfxdata = cqr->data;
4054                if (ccw->cmd_code == DASD_ECKD_CCW_PFX) {
4055                        pfxdata->validity.verify_base = 0;
4056                        pfxdata->validity.hyper_pav = 0;
4057                }
4058        }
4059}
4060
4061#define DASD_ECKD_CHANQ_MAX_SIZE 4
4062
4063static struct dasd_ccw_req *dasd_eckd_build_alias_cp(struct dasd_device *base,
4064                                                     struct dasd_block *block,
4065                                                     struct request *req)
4066{
4067        struct dasd_eckd_private *private;
4068        struct dasd_device *startdev;
4069        unsigned long flags;
4070        struct dasd_ccw_req *cqr;
4071
4072        startdev = dasd_alias_get_start_dev(base);
4073        if (!startdev)
4074                startdev = base;
4075        private = startdev->private;
4076        if (private->count >= DASD_ECKD_CHANQ_MAX_SIZE)
4077                return ERR_PTR(-EBUSY);
4078
4079        spin_lock_irqsave(get_ccwdev_lock(startdev->cdev), flags);
4080        private->count++;
4081        if ((base->features & DASD_FEATURE_USERAW))
4082                cqr = dasd_raw_build_cp(startdev, block, req);
4083        else
4084                cqr = dasd_eckd_build_cp(startdev, block, req);
4085        if (IS_ERR(cqr))
4086                private->count--;
4087        spin_unlock_irqrestore(get_ccwdev_lock(startdev->cdev), flags);
4088        return cqr;
4089}
4090
4091static int dasd_eckd_free_alias_cp(struct dasd_ccw_req *cqr,
4092                                   struct request *req)
4093{
4094        struct dasd_eckd_private *private;
4095        unsigned long flags;
4096
4097        spin_lock_irqsave(get_ccwdev_lock(cqr->memdev->cdev), flags);
4098        private = cqr->memdev->private;
4099        private->count--;
4100        spin_unlock_irqrestore(get_ccwdev_lock(cqr->memdev->cdev), flags);
4101        return dasd_eckd_free_cp(cqr, req);
4102}
4103
4104static int
4105dasd_eckd_fill_info(struct dasd_device * device,
4106                    struct dasd_information2_t * info)
4107{
4108        struct dasd_eckd_private *private = device->private;
4109
4110        info->label_block = 2;
4111        info->FBA_layout = private->uses_cdl ? 0 : 1;
4112        info->format = private->uses_cdl ? DASD_FORMAT_CDL : DASD_FORMAT_LDL;
4113        info->characteristics_size = sizeof(private->rdc_data);
4114        memcpy(info->characteristics, &private->rdc_data,
4115               sizeof(private->rdc_data));
4116        info->confdata_size = min((unsigned long)private->conf_len,
4117                                  sizeof(info->configuration_data));
4118        memcpy(info->configuration_data, private->conf_data,
4119               info->confdata_size);
4120        return 0;
4121}
4122
4123/*
4124 * SECTION: ioctl functions for eckd devices.
4125 */
4126
4127/*
4128 * Release device ioctl.
4129 * Buils a channel programm to releases a prior reserved
4130 * (see dasd_eckd_reserve) device.
4131 */
4132static int
4133dasd_eckd_release(struct dasd_device *device)
4134{
4135        struct dasd_ccw_req *cqr;
4136        int rc;
4137        struct ccw1 *ccw;
4138        int useglobal;
4139
4140        if (!capable(CAP_SYS_ADMIN))
4141                return -EACCES;
4142
4143        useglobal = 0;
4144        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
4145        if (IS_ERR(cqr)) {
4146                mutex_lock(&dasd_reserve_mutex);
4147                useglobal = 1;
4148                cqr = &dasd_reserve_req->cqr;
4149                memset(cqr, 0, sizeof(*cqr));
4150                memset(&dasd_reserve_req->ccw, 0,
4151                       sizeof(dasd_reserve_req->ccw));
4152                cqr->cpaddr = &dasd_reserve_req->ccw;
4153                cqr->data = &dasd_reserve_req->data;
4154                cqr->magic = DASD_ECKD_MAGIC;
4155        }
4156        ccw = cqr->cpaddr;
4157        ccw->cmd_code = DASD_ECKD_CCW_RELEASE;
4158        ccw->flags |= CCW_FLAG_SLI;
4159        ccw->count = 32;
4160        ccw->cda = (__u32)(addr_t) cqr->data;
4161        cqr->startdev = device;
4162        cqr->memdev = device;
4163        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4164        set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4165        cqr->retries = 2;       /* set retry counter to enable basic ERP */
4166        cqr->expires = 2 * HZ;
4167        cqr->buildclk = get_tod_clock();
4168        cqr->status = DASD_CQR_FILLED;
4169
4170        rc = dasd_sleep_on_immediatly(cqr);
4171        if (!rc)
4172                clear_bit(DASD_FLAG_IS_RESERVED, &device->flags);
4173
4174        if (useglobal)
4175                mutex_unlock(&dasd_reserve_mutex);
4176        else
4177                dasd_sfree_request(cqr, cqr->memdev);
4178        return rc;
4179}
4180
4181/*
4182 * Reserve device ioctl.
4183 * Options are set to 'synchronous wait for interrupt' and
4184 * 'timeout the request'. This leads to a terminate IO if
4185 * the interrupt is outstanding for a certain time.
4186 */
4187static int
4188dasd_eckd_reserve(struct dasd_device *device)
4189{
4190        struct dasd_ccw_req *cqr;
4191        int rc;
4192        struct ccw1 *ccw;
4193        int useglobal;
4194
4195        if (!capable(CAP_SYS_ADMIN))
4196                return -EACCES;
4197
4198        useglobal = 0;
4199        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
4200        if (IS_ERR(cqr)) {
4201                mutex_lock(&dasd_reserve_mutex);
4202                useglobal = 1;
4203                cqr = &dasd_reserve_req->cqr;
4204                memset(cqr, 0, sizeof(*cqr));
4205                memset(&dasd_reserve_req->ccw, 0,
4206                       sizeof(dasd_reserve_req->ccw));
4207                cqr->cpaddr = &dasd_reserve_req->ccw;
4208                cqr->data = &dasd_reserve_req->data;
4209                cqr->magic = DASD_ECKD_MAGIC;
4210        }
4211        ccw = cqr->cpaddr;
4212        ccw->cmd_code = DASD_ECKD_CCW_RESERVE;
4213        ccw->flags |= CCW_FLAG_SLI;
4214        ccw->count = 32;
4215        ccw->cda = (__u32)(addr_t) cqr->data;
4216        cqr->startdev = device;
4217        cqr->memdev = device;
4218        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4219        set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4220        cqr->retries = 2;       /* set retry counter to enable basic ERP */
4221        cqr->expires = 2 * HZ;
4222        cqr->buildclk = get_tod_clock();
4223        cqr->status = DASD_CQR_FILLED;
4224
4225        rc = dasd_sleep_on_immediatly(cqr);
4226        if (!rc)
4227                set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
4228
4229        if (useglobal)
4230                mutex_unlock(&dasd_reserve_mutex);
4231        else
4232                dasd_sfree_request(cqr, cqr->memdev);
4233        return rc;
4234}
4235
4236/*
4237 * Steal lock ioctl - unconditional reserve device.
4238 * Buils a channel programm to break a device's reservation.
4239 * (unconditional reserve)
4240 */
4241static int
4242dasd_eckd_steal_lock(struct dasd_device *device)
4243{
4244        struct dasd_ccw_req *cqr;
4245        int rc;
4246        struct ccw1 *ccw;
4247        int useglobal;
4248
4249        if (!capable(CAP_SYS_ADMIN))
4250                return -EACCES;
4251
4252        useglobal = 0;
4253        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1, 32, device);
4254        if (IS_ERR(cqr)) {
4255                mutex_lock(&dasd_reserve_mutex);
4256                useglobal = 1;
4257                cqr = &dasd_reserve_req->cqr;
4258                memset(cqr, 0, sizeof(*cqr));
4259                memset(&dasd_reserve_req->ccw, 0,
4260                       sizeof(dasd_reserve_req->ccw));
4261                cqr->cpaddr = &dasd_reserve_req->ccw;
4262                cqr->data = &dasd_reserve_req->data;
4263                cqr->magic = DASD_ECKD_MAGIC;
4264        }
4265        ccw = cqr->cpaddr;
4266        ccw->cmd_code = DASD_ECKD_CCW_SLCK;
4267        ccw->flags |= CCW_FLAG_SLI;
4268        ccw->count = 32;
4269        ccw->cda = (__u32)(addr_t) cqr->data;
4270        cqr->startdev = device;
4271        cqr->memdev = device;
4272        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4273        set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4274        cqr->retries = 2;       /* set retry counter to enable basic ERP */
4275        cqr->expires = 2 * HZ;
4276        cqr->buildclk = get_tod_clock();
4277        cqr->status = DASD_CQR_FILLED;
4278
4279        rc = dasd_sleep_on_immediatly(cqr);
4280        if (!rc)
4281                set_bit(DASD_FLAG_IS_RESERVED, &device->flags);
4282
4283        if (useglobal)
4284                mutex_unlock(&dasd_reserve_mutex);
4285        else
4286                dasd_sfree_request(cqr, cqr->memdev);
4287        return rc;
4288}
4289
4290/*
4291 * SNID - Sense Path Group ID
4292 * This ioctl may be used in situations where I/O is stalled due to
4293 * a reserve, so if the normal dasd_smalloc_request fails, we use the
4294 * preallocated dasd_reserve_req.
4295 */
4296static int dasd_eckd_snid(struct dasd_device *device,
4297                          void __user *argp)
4298{
4299        struct dasd_ccw_req *cqr;
4300        int rc;
4301        struct ccw1 *ccw;
4302        int useglobal;
4303        struct dasd_snid_ioctl_data usrparm;
4304
4305        if (!capable(CAP_SYS_ADMIN))
4306                return -EACCES;
4307
4308        if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
4309                return -EFAULT;
4310
4311        useglobal = 0;
4312        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1,
4313                                   sizeof(struct dasd_snid_data), device);
4314        if (IS_ERR(cqr)) {
4315                mutex_lock(&dasd_reserve_mutex);
4316                useglobal = 1;
4317                cqr = &dasd_reserve_req->cqr;
4318                memset(cqr, 0, sizeof(*cqr));
4319                memset(&dasd_reserve_req->ccw, 0,
4320                       sizeof(dasd_reserve_req->ccw));
4321                cqr->cpaddr = &dasd_reserve_req->ccw;
4322                cqr->data = &dasd_reserve_req->data;
4323                cqr->magic = DASD_ECKD_MAGIC;
4324        }
4325        ccw = cqr->cpaddr;
4326        ccw->cmd_code = DASD_ECKD_CCW_SNID;
4327        ccw->flags |= CCW_FLAG_SLI;
4328        ccw->count = 12;
4329        ccw->cda = (__u32)(addr_t) cqr->data;
4330        cqr->startdev = device;
4331        cqr->memdev = device;
4332        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4333        set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
4334        set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags);
4335        cqr->retries = 5;
4336        cqr->expires = 10 * HZ;
4337        cqr->buildclk = get_tod_clock();
4338        cqr->status = DASD_CQR_FILLED;
4339        cqr->lpm = usrparm.path_mask;
4340
4341        rc = dasd_sleep_on_immediatly(cqr);
4342        /* verify that I/O processing didn't modify the path mask */
4343        if (!rc && usrparm.path_mask && (cqr->lpm != usrparm.path_mask))
4344                rc = -EIO;
4345        if (!rc) {
4346                usrparm.data = *((struct dasd_snid_data *)cqr->data);
4347                if (copy_to_user(argp, &usrparm, sizeof(usrparm)))
4348                        rc = -EFAULT;
4349        }
4350
4351        if (useglobal)
4352                mutex_unlock(&dasd_reserve_mutex);
4353        else
4354                dasd_sfree_request(cqr, cqr->memdev);
4355        return rc;
4356}
4357
4358/*
4359 * Read performance statistics
4360 */
4361static int
4362dasd_eckd_performance(struct dasd_device *device, void __user *argp)
4363{
4364        struct dasd_psf_prssd_data *prssdp;
4365        struct dasd_rssd_perf_stats_t *stats;
4366        struct dasd_ccw_req *cqr;
4367        struct ccw1 *ccw;
4368        int rc;
4369
4370        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */  + 1 /* RSSD */,
4371                                   (sizeof(struct dasd_psf_prssd_data) +
4372                                    sizeof(struct dasd_rssd_perf_stats_t)),
4373                                   device);
4374        if (IS_ERR(cqr)) {
4375                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4376                            "Could not allocate initialization request");
4377                return PTR_ERR(cqr);
4378        }
4379        cqr->startdev = device;
4380        cqr->memdev = device;
4381        cqr->retries = 0;
4382        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
4383        cqr->expires = 10 * HZ;
4384
4385        /* Prepare for Read Subsystem Data */
4386        prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4387        memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
4388        prssdp->order = PSF_ORDER_PRSSD;
4389        prssdp->suborder = 0x01;        /* Performance Statistics */
4390        prssdp->varies[1] = 0x01;       /* Perf Statistics for the Subsystem */
4391
4392        ccw = cqr->cpaddr;
4393        ccw->cmd_code = DASD_ECKD_CCW_PSF;
4394        ccw->count = sizeof(struct dasd_psf_prssd_data);
4395        ccw->flags |= CCW_FLAG_CC;
4396        ccw->cda = (__u32)(addr_t) prssdp;
4397
4398        /* Read Subsystem Data - Performance Statistics */
4399        stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
4400        memset(stats, 0, sizeof(struct dasd_rssd_perf_stats_t));
4401
4402        ccw++;
4403        ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4404        ccw->count = sizeof(struct dasd_rssd_perf_stats_t);
4405        ccw->cda = (__u32)(addr_t) stats;
4406
4407        cqr->buildclk = get_tod_clock();
4408        cqr->status = DASD_CQR_FILLED;
4409        rc = dasd_sleep_on(cqr);
4410        if (rc == 0) {
4411                prssdp = (struct dasd_psf_prssd_data *) cqr->data;
4412                stats = (struct dasd_rssd_perf_stats_t *) (prssdp + 1);
4413                if (copy_to_user(argp, stats,
4414                                 sizeof(struct dasd_rssd_perf_stats_t)))
4415                        rc = -EFAULT;
4416        }
4417        dasd_sfree_request(cqr, cqr->memdev);
4418        return rc;
4419}
4420
4421/*
4422 * Get attributes (cache operations)
4423 * Returnes the cache attributes used in Define Extend (DE).
4424 */
4425static int
4426dasd_eckd_get_attrib(struct dasd_device *device, void __user *argp)
4427{
4428        struct dasd_eckd_private *private = device->private;
4429        struct attrib_data_t attrib = private->attrib;
4430        int rc;
4431
4432        if (!capable(CAP_SYS_ADMIN))
4433                return -EACCES;
4434        if (!argp)
4435                return -EINVAL;
4436
4437        rc = 0;
4438        if (copy_to_user(argp, (long *) &attrib,
4439                         sizeof(struct attrib_data_t)))
4440                rc = -EFAULT;
4441
4442        return rc;
4443}
4444
4445/*
4446 * Set attributes (cache operations)
4447 * Stores the attributes for cache operation to be used in Define Extend (DE).
4448 */
4449static int
4450dasd_eckd_set_attrib(struct dasd_device *device, void __user *argp)
4451{
4452        struct dasd_eckd_private *private = device->private;
4453        struct attrib_data_t attrib;
4454
4455        if (!capable(CAP_SYS_ADMIN))
4456                return -EACCES;
4457        if (!argp)
4458                return -EINVAL;
4459
4460        if (copy_from_user(&attrib, argp, sizeof(struct attrib_data_t)))
4461                return -EFAULT;
4462        private->attrib = attrib;
4463
4464        dev_info(&device->cdev->dev,
4465                 "The DASD cache mode was set to %x (%i cylinder prestage)\n",
4466                 private->attrib.operation, private->attrib.nr_cyl);
4467        return 0;
4468}
4469
4470/*
4471 * Issue syscall I/O to EMC Symmetrix array.
4472 * CCWs are PSF and RSSD
4473 */
4474static int dasd_symm_io(struct dasd_device *device, void __user *argp)
4475{
4476        struct dasd_symmio_parms usrparm;
4477        char *psf_data, *rssd_result;
4478        struct dasd_ccw_req *cqr;
4479        struct ccw1 *ccw;
4480        char psf0, psf1;
4481        int rc;
4482
4483        if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
4484                return -EACCES;
4485        psf0 = psf1 = 0;
4486
4487        /* Copy parms from caller */
4488        rc = -EFAULT;
4489        if (copy_from_user(&usrparm, argp, sizeof(usrparm)))
4490                goto out;
4491        if (is_compat_task()) {
4492                /* Make sure pointers are sane even on 31 bit. */
4493                rc = -EINVAL;
4494                if ((usrparm.psf_data >> 32) != 0)
4495                        goto out;
4496                if ((usrparm.rssd_result >> 32) != 0)
4497                        goto out;
4498                usrparm.psf_data &= 0x7fffffffULL;
4499                usrparm.rssd_result &= 0x7fffffffULL;
4500        }
4501        /* alloc I/O data area */
4502        psf_data = kzalloc(usrparm.psf_data_len, GFP_KERNEL | GFP_DMA);
4503        rssd_result = kzalloc(usrparm.rssd_result_len, GFP_KERNEL | GFP_DMA);
4504        if (!psf_data || !rssd_result) {
4505                rc = -ENOMEM;
4506                goto out_free;
4507        }
4508
4509        /* get syscall header from user space */
4510        rc = -EFAULT;
4511        if (copy_from_user(psf_data,
4512                           (void __user *)(unsigned long) usrparm.psf_data,
4513                           usrparm.psf_data_len))
4514                goto out_free;
4515        psf0 = psf_data[0];
4516        psf1 = psf_data[1];
4517
4518        /* setup CCWs for PSF + RSSD */
4519        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 2 , 0, device);
4520        if (IS_ERR(cqr)) {
4521                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4522                        "Could not allocate initialization request");
4523                rc = PTR_ERR(cqr);
4524                goto out_free;
4525        }
4526
4527        cqr->startdev = device;
4528        cqr->memdev = device;
4529        cqr->retries = 3;
4530        cqr->expires = 10 * HZ;
4531        cqr->buildclk = get_tod_clock();
4532        cqr->status = DASD_CQR_FILLED;
4533
4534        /* Build the ccws */
4535        ccw = cqr->cpaddr;
4536
4537        /* PSF ccw */
4538        ccw->cmd_code = DASD_ECKD_CCW_PSF;
4539        ccw->count = usrparm.psf_data_len;
4540        ccw->flags |= CCW_FLAG_CC;
4541        ccw->cda = (__u32)(addr_t) psf_data;
4542
4543        ccw++;
4544
4545        /* RSSD ccw  */
4546        ccw->cmd_code = DASD_ECKD_CCW_RSSD;
4547        ccw->count = usrparm.rssd_result_len;
4548        ccw->flags = CCW_FLAG_SLI ;
4549        ccw->cda = (__u32)(addr_t) rssd_result;
4550
4551        rc = dasd_sleep_on(cqr);
4552        if (rc)
4553                goto out_sfree;
4554
4555        rc = -EFAULT;
4556        if (copy_to_user((void __user *)(unsigned long) usrparm.rssd_result,
4557                           rssd_result, usrparm.rssd_result_len))
4558                goto out_sfree;
4559        rc = 0;
4560
4561out_sfree:
4562        dasd_sfree_request(cqr, cqr->memdev);
4563out_free:
4564        kfree(rssd_result);
4565        kfree(psf_data);
4566out:
4567        DBF_DEV_EVENT(DBF_WARNING, device,
4568                      "Symmetrix ioctl (0x%02x 0x%02x): rc=%d",
4569                      (int) psf0, (int) psf1, rc);
4570        return rc;
4571}
4572
4573static int
4574dasd_eckd_ioctl(struct dasd_block *block, unsigned int cmd, void __user *argp)
4575{
4576        struct dasd_device *device = block->base;
4577
4578        switch (cmd) {
4579        case BIODASDGATTR:
4580                return dasd_eckd_get_attrib(device, argp);
4581        case BIODASDSATTR:
4582                return dasd_eckd_set_attrib(device, argp);
4583        case BIODASDPSRD:
4584                return dasd_eckd_performance(device, argp);
4585        case BIODASDRLSE:
4586                return dasd_eckd_release(device);
4587        case BIODASDRSRV:
4588                return dasd_eckd_reserve(device);
4589        case BIODASDSLCK:
4590                return dasd_eckd_steal_lock(device);
4591        case BIODASDSNID:
4592                return dasd_eckd_snid(device, argp);
4593        case BIODASDSYMMIO:
4594                return dasd_symm_io(device, argp);
4595        default:
4596                return -ENOTTY;
4597        }
4598}
4599
4600/*
4601 * Dump the range of CCWs into 'page' buffer
4602 * and return number of printed chars.
4603 */
4604static int
4605dasd_eckd_dump_ccw_range(struct ccw1 *from, struct ccw1 *to, char *page)
4606{
4607        int len, count;
4608        char *datap;
4609
4610        len = 0;
4611        while (from <= to) {
4612                len += sprintf(page + len, PRINTK_HEADER
4613                               " CCW %p: %08X %08X DAT:",
4614                               from, ((int *) from)[0], ((int *) from)[1]);
4615
4616                /* get pointer to data (consider IDALs) */
4617                if (from->flags & CCW_FLAG_IDA)
4618                        datap = (char *) *((addr_t *) (addr_t) from->cda);
4619                else
4620                        datap = (char *) ((addr_t) from->cda);
4621
4622                /* dump data (max 32 bytes) */
4623                for (count = 0; count < from->count && count < 32; count++) {
4624                        if (count % 8 == 0) len += sprintf(page + len, " ");
4625                        if (count % 4 == 0) len += sprintf(page + len, " ");
4626                        len += sprintf(page + len, "%02x", datap[count]);
4627                }
4628                len += sprintf(page + len, "\n");
4629                from++;
4630        }
4631        return len;
4632}
4633
4634static void
4635dasd_eckd_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
4636                         char *reason)
4637{
4638        u64 *sense;
4639        u64 *stat;
4640
4641        sense = (u64 *) dasd_get_sense(irb);
4642        stat = (u64 *) &irb->scsw;
4643        if (sense) {
4644                DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : "
4645                              "%016llx %016llx %016llx %016llx",
4646                              reason, *stat, *((u32 *) (stat + 1)),
4647                              sense[0], sense[1], sense[2], sense[3]);
4648        } else {
4649                DBF_DEV_EVENT(DBF_EMERG, device, "%s: %016llx %08x : %s",
4650                              reason, *stat, *((u32 *) (stat + 1)),
4651                              "NO VALID SENSE");
4652        }
4653}
4654
4655/*
4656 * Print sense data and related channel program.
4657 * Parts are printed because printk buffer is only 1024 bytes.
4658 */
4659static void dasd_eckd_dump_sense_ccw(struct dasd_device *device,
4660                                 struct dasd_ccw_req *req, struct irb *irb)
4661{
4662        char *page;
4663        struct ccw1 *first, *last, *fail, *from, *to;
4664        int len, sl, sct;
4665
4666        page = (char *) get_zeroed_page(GFP_ATOMIC);
4667        if (page == NULL) {
4668                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
4669                              "No memory to dump sense data\n");
4670                return;
4671        }
4672        /* dump the sense data */
4673        len = sprintf(page, PRINTK_HEADER
4674                      " I/O status report for device %s:\n",
4675                      dev_name(&device->cdev->dev));
4676        len += sprintf(page + len, PRINTK_HEADER
4677                       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4678                       "CS:%02X RC:%d\n",
4679                       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4680                       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4681                       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4682                       req ? req->intrc : 0);
4683        len += sprintf(page + len, PRINTK_HEADER
4684                       " device %s: Failing CCW: %p\n",
4685                       dev_name(&device->cdev->dev),
4686                       (void *) (addr_t) irb->scsw.cmd.cpa);
4687        if (irb->esw.esw0.erw.cons) {
4688                for (sl = 0; sl < 4; sl++) {
4689                        len += sprintf(page + len, PRINTK_HEADER
4690                                       " Sense(hex) %2d-%2d:",
4691                                       (8 * sl), ((8 * sl) + 7));
4692
4693                        for (sct = 0; sct < 8; sct++) {
4694                                len += sprintf(page + len, " %02x",
4695                                               irb->ecw[8 * sl + sct]);
4696                        }
4697                        len += sprintf(page + len, "\n");
4698                }
4699
4700                if (irb->ecw[27] & DASD_SENSE_BIT_0) {
4701                        /* 24 Byte Sense Data */
4702                        sprintf(page + len, PRINTK_HEADER
4703                                " 24 Byte: %x MSG %x, "
4704                                "%s MSGb to SYSOP\n",
4705                                irb->ecw[7] >> 4, irb->ecw[7] & 0x0f,
4706                                irb->ecw[1] & 0x10 ? "" : "no");
4707                } else {
4708                        /* 32 Byte Sense Data */
4709                        sprintf(page + len, PRINTK_HEADER
4710                                " 32 Byte: Format: %x "
4711                                "Exception class %x\n",
4712                                irb->ecw[6] & 0x0f, irb->ecw[22] >> 4);
4713                }
4714        } else {
4715                sprintf(page + len, PRINTK_HEADER
4716                        " SORRY - NO VALID SENSE AVAILABLE\n");
4717        }
4718        printk(KERN_ERR "%s", page);
4719
4720        if (req) {
4721                /* req == NULL for unsolicited interrupts */
4722                /* dump the Channel Program (max 140 Bytes per line) */
4723                /* Count CCW and print first CCWs (maximum 1024 % 140 = 7) */
4724                first = req->cpaddr;
4725                for (last = first; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
4726                to = min(first + 6, last);
4727                len = sprintf(page, PRINTK_HEADER
4728                              " Related CP in req: %p\n", req);
4729                dasd_eckd_dump_ccw_range(first, to, page + len);
4730                printk(KERN_ERR "%s", page);
4731
4732                /* print failing CCW area (maximum 4) */
4733                /* scsw->cda is either valid or zero  */
4734                len = 0;
4735                from = ++to;
4736                fail = (struct ccw1 *)(addr_t)
4737                                irb->scsw.cmd.cpa; /* failing CCW */
4738                if (from <  fail - 2) {
4739                        from = fail - 2;     /* there is a gap - print header */
4740                        len += sprintf(page, PRINTK_HEADER "......\n");
4741                }
4742                to = min(fail + 1, last);
4743                len += dasd_eckd_dump_ccw_range(from, to, page + len);
4744
4745                /* print last CCWs (maximum 2) */
4746                from = max(from, ++to);
4747                if (from < last - 1) {
4748                        from = last - 1;     /* there is a gap - print header */
4749                        len += sprintf(page + len, PRINTK_HEADER "......\n");
4750                }
4751                len += dasd_eckd_dump_ccw_range(from, last, page + len);
4752                if (len > 0)
4753                        printk(KERN_ERR "%s", page);
4754        }
4755        free_page((unsigned long) page);
4756}
4757
4758
4759/*
4760 * Print sense data from a tcw.
4761 */
4762static void dasd_eckd_dump_sense_tcw(struct dasd_device *device,
4763                                 struct dasd_ccw_req *req, struct irb *irb)
4764{
4765        char *page;
4766        int len, sl, sct, residual;
4767        struct tsb *tsb;
4768        u8 *sense, *rcq;
4769
4770        page = (char *) get_zeroed_page(GFP_ATOMIC);
4771        if (page == NULL) {
4772                DBF_DEV_EVENT(DBF_WARNING, device, " %s",
4773                            "No memory to dump sense data");
4774                return;
4775        }
4776        /* dump the sense data */
4777        len = sprintf(page, PRINTK_HEADER
4778                      " I/O status report for device %s:\n",
4779                      dev_name(&device->cdev->dev));
4780        len += sprintf(page + len, PRINTK_HEADER
4781                       " in req: %p CC:%02X FC:%02X AC:%02X SC:%02X DS:%02X "
4782                       "CS:%02X fcxs:%02X schxs:%02X RC:%d\n",
4783                       req, scsw_cc(&irb->scsw), scsw_fctl(&irb->scsw),
4784                       scsw_actl(&irb->scsw), scsw_stctl(&irb->scsw),
4785                       scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw),
4786                       irb->scsw.tm.fcxs, irb->scsw.tm.schxs,
4787                       req ? req->intrc : 0);
4788        len += sprintf(page + len, PRINTK_HEADER
4789                       " device %s: Failing TCW: %p\n",
4790                       dev_name(&device->cdev->dev),
4791                       (void *) (addr_t) irb->scsw.tm.tcw);
4792
4793        tsb = NULL;
4794        sense = NULL;
4795        if (irb->scsw.tm.tcw && (irb->scsw.tm.fcxs & 0x01))
4796                tsb = tcw_get_tsb(
4797                        (struct tcw *)(unsigned long)irb->scsw.tm.tcw);
4798
4799        if (tsb) {
4800                len += sprintf(page + len, PRINTK_HEADER
4801                               " tsb->length %d\n", tsb->length);
4802                len += sprintf(page + len, PRINTK_HEADER
4803                               " tsb->flags %x\n", tsb->flags);
4804                len += sprintf(page + len, PRINTK_HEADER
4805                               " tsb->dcw_offset %d\n", tsb->dcw_offset);
4806                len += sprintf(page + len, PRINTK_HEADER
4807                               " tsb->count %d\n", tsb->count);
4808                residual = tsb->count - 28;
4809                len += sprintf(page + len, PRINTK_HEADER
4810                               " residual %d\n", residual);
4811
4812                switch (tsb->flags & 0x07) {
4813                case 1: /* tsa_iostat */
4814                        len += sprintf(page + len, PRINTK_HEADER
4815                               " tsb->tsa.iostat.dev_time %d\n",
4816                                       tsb->tsa.iostat.dev_time);
4817                        len += sprintf(page + len, PRINTK_HEADER
4818                               " tsb->tsa.iostat.def_time %d\n",
4819                                       tsb->tsa.iostat.def_time);
4820                        len += sprintf(page + len, PRINTK_HEADER
4821                               " tsb->tsa.iostat.queue_time %d\n",
4822                                       tsb->tsa.iostat.queue_time);
4823                        len += sprintf(page + len, PRINTK_HEADER
4824                               " tsb->tsa.iostat.dev_busy_time %d\n",
4825                                       tsb->tsa.iostat.dev_busy_time);
4826                        len += sprintf(page + len, PRINTK_HEADER
4827                               " tsb->tsa.iostat.dev_act_time %d\n",
4828                                       tsb->tsa.iostat.dev_act_time);
4829                        sense = tsb->tsa.iostat.sense;
4830                        break;
4831                case 2: /* ts_ddpc */
4832                        len += sprintf(page + len, PRINTK_HEADER
4833                               " tsb->tsa.ddpc.rc %d\n", tsb->tsa.ddpc.rc);
4834                        for (sl = 0; sl < 2; sl++) {
4835                                len += sprintf(page + len, PRINTK_HEADER
4836                                               " tsb->tsa.ddpc.rcq %2d-%2d: ",
4837                                               (8 * sl), ((8 * sl) + 7));
4838                                rcq = tsb->tsa.ddpc.rcq;
4839                                for (sct = 0; sct < 8; sct++) {
4840                                        len += sprintf(page + len, " %02x",
4841                                                       rcq[8 * sl + sct]);
4842                                }
4843                                len += sprintf(page + len, "\n");
4844                        }
4845                        sense = tsb->tsa.ddpc.sense;
4846                        break;
4847                case 3: /* tsa_intrg */
4848                        len += sprintf(page + len, PRINTK_HEADER
4849                                      " tsb->tsa.intrg.: not supportet yet\n");
4850                        break;
4851                }
4852
4853                if (sense) {
4854                        for (sl = 0; sl < 4; sl++) {
4855                                len += sprintf(page + len, PRINTK_HEADER
4856                                               " Sense(hex) %2d-%2d:",
4857                                               (8 * sl), ((8 * sl) + 7));
4858                                for (sct = 0; sct < 8; sct++) {
4859                                        len += sprintf(page + len, " %02x",
4860                                                       sense[8 * sl + sct]);
4861                                }
4862                                len += sprintf(page + len, "\n");
4863                        }
4864
4865                        if (sense[27] & DASD_SENSE_BIT_0) {
4866                                /* 24 Byte Sense Data */
4867                                sprintf(page + len, PRINTK_HEADER
4868                                        " 24 Byte: %x MSG %x, "
4869                                        "%s MSGb to SYSOP\n",
4870                                        sense[7] >> 4, sense[7] & 0x0f,
4871                                        sense[1] & 0x10 ? "" : "no");
4872                        } else {
4873                                /* 32 Byte Sense Data */
4874                                sprintf(page + len, PRINTK_HEADER
4875                                        " 32 Byte: Format: %x "
4876                                        "Exception class %x\n",
4877                                        sense[6] & 0x0f, sense[22] >> 4);
4878                        }
4879                } else {
4880                        sprintf(page + len, PRINTK_HEADER
4881                                " SORRY - NO VALID SENSE AVAILABLE\n");
4882                }
4883        } else {
4884                sprintf(page + len, PRINTK_HEADER
4885                        " SORRY - NO TSB DATA AVAILABLE\n");
4886        }
4887        printk(KERN_ERR "%s", page);
4888        free_page((unsigned long) page);
4889}
4890
4891static void dasd_eckd_dump_sense(struct dasd_device *device,
4892                                 struct dasd_ccw_req *req, struct irb *irb)
4893{
4894        u8 *sense = dasd_get_sense(irb);
4895
4896        if (scsw_is_tm(&irb->scsw)) {
4897                /*
4898                 * In some cases the 'File Protected' or 'Incorrect Length'
4899                 * error might be expected and log messages shouldn't be written
4900                 * then. Check if the according suppress bit is set.
4901                 */
4902                if (sense && (sense[1] & SNS1_FILE_PROTECTED) &&
4903                    test_bit(DASD_CQR_SUPPRESS_FP, &req->flags))
4904                        return;
4905                if (scsw_cstat(&irb->scsw) == 0x40 &&
4906                    test_bit(DASD_CQR_SUPPRESS_IL, &req->flags))
4907                        return;
4908
4909                dasd_eckd_dump_sense_tcw(device, req, irb);
4910        } else {
4911                /*
4912                 * In some cases the 'No Record Found' error might be expected
4913                 * and log messages shouldn't be written then. Check if the
4914                 * according suppress bit is set.
4915                 */
4916                if (sense && sense[1] & SNS1_NO_REC_FOUND &&
4917                    test_bit(DASD_CQR_SUPPRESS_NRF, &req->flags))
4918                        return;
4919
4920                dasd_eckd_dump_sense_ccw(device, req, irb);
4921        }
4922}
4923
4924static int dasd_eckd_pm_freeze(struct dasd_device *device)
4925{
4926        /*
4927         * the device should be disconnected from our LCU structure
4928         * on restore we will reconnect it and reread LCU specific
4929         * information like PAV support that might have changed
4930         */
4931        dasd_alias_remove_device(device);
4932        dasd_alias_disconnect_device_from_lcu(device);
4933
4934        return 0;
4935}
4936
4937static int dasd_eckd_restore_device(struct dasd_device *device)
4938{
4939        struct dasd_eckd_private *private = device->private;
4940        struct dasd_eckd_characteristics temp_rdc_data;
4941        int rc;
4942        struct dasd_uid temp_uid;
4943        unsigned long flags;
4944        unsigned long cqr_flags = 0;
4945
4946        /* Read Configuration Data */
4947        rc = dasd_eckd_read_conf(device);
4948        if (rc) {
4949                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4950                                "Read configuration data failed, rc=%d", rc);
4951                goto out_err;
4952        }
4953
4954        dasd_eckd_get_uid(device, &temp_uid);
4955        /* Generate device unique id */
4956        rc = dasd_eckd_generate_uid(device);
4957        spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4958        if (memcmp(&private->uid, &temp_uid, sizeof(struct dasd_uid)) != 0)
4959                dev_err(&device->cdev->dev, "The UID of the DASD has "
4960                        "changed\n");
4961        spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4962        if (rc)
4963                goto out_err;
4964
4965        /* register lcu with alias handling, enable PAV if this is a new lcu */
4966        rc = dasd_alias_make_device_known_to_lcu(device);
4967        if (rc)
4968                goto out_err;
4969
4970        set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr_flags);
4971        dasd_eckd_validate_server(device, cqr_flags);
4972
4973        /* RE-Read Configuration Data */
4974        rc = dasd_eckd_read_conf(device);
4975        if (rc) {
4976                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4977                        "Read configuration data failed, rc=%d", rc);
4978                goto out_err2;
4979        }
4980
4981        /* Read Feature Codes */
4982        dasd_eckd_read_features(device);
4983
4984        /* Read Device Characteristics */
4985        rc = dasd_generic_read_dev_chars(device, DASD_ECKD_MAGIC,
4986                                         &temp_rdc_data, 64);
4987        if (rc) {
4988                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
4989                                "Read device characteristic failed, rc=%d", rc);
4990                goto out_err2;
4991        }
4992        spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
4993        memcpy(&private->rdc_data, &temp_rdc_data, sizeof(temp_rdc_data));
4994        spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
4995
4996        /* add device to alias management */
4997        dasd_alias_add_device(device);
4998
4999        return 0;
5000
5001out_err2:
5002        dasd_alias_disconnect_device_from_lcu(device);
5003out_err:
5004        return -1;
5005}
5006
5007static int dasd_eckd_reload_device(struct dasd_device *device)
5008{
5009        struct dasd_eckd_private *private = device->private;
5010        int rc, old_base;
5011        char print_uid[60];
5012        struct dasd_uid uid;
5013        unsigned long flags;
5014
5015        /*
5016         * remove device from alias handling to prevent new requests
5017         * from being scheduled on the wrong alias device
5018         */
5019        dasd_alias_remove_device(device);
5020
5021        spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
5022        old_base = private->uid.base_unit_addr;
5023        spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
5024
5025        /* Read Configuration Data */
5026        rc = dasd_eckd_read_conf(device);
5027        if (rc)
5028                goto out_err;
5029
5030        rc = dasd_eckd_generate_uid(device);
5031        if (rc)
5032                goto out_err;
5033        /*
5034         * update unit address configuration and
5035         * add device to alias management
5036         */
5037        dasd_alias_update_add_device(device);
5038
5039        dasd_eckd_get_uid(device, &uid);
5040
5041        if (old_base != uid.base_unit_addr) {
5042                if (strlen(uid.vduit) > 0)
5043                        snprintf(print_uid, sizeof(print_uid),
5044                                 "%s.%s.%04x.%02x.%s", uid.vendor, uid.serial,
5045                                 uid.ssid, uid.base_unit_addr, uid.vduit);
5046                else
5047                        snprintf(print_uid, sizeof(print_uid),
5048                                 "%s.%s.%04x.%02x", uid.vendor, uid.serial,
5049                                 uid.ssid, uid.base_unit_addr);
5050
5051                dev_info(&device->cdev->dev,
5052                         "An Alias device was reassigned to a new base device "
5053                         "with UID: %s\n", print_uid);
5054        }
5055        return 0;
5056
5057out_err:
5058        return -1;
5059}
5060
5061static int dasd_eckd_read_message_buffer(struct dasd_device *device,
5062                                         struct dasd_rssd_messages *messages,
5063                                         __u8 lpum)
5064{
5065        struct dasd_rssd_messages *message_buf;
5066        struct dasd_psf_prssd_data *prssdp;
5067        struct dasd_ccw_req *cqr;
5068        struct ccw1 *ccw;
5069        int rc;
5070
5071        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
5072                                   (sizeof(struct dasd_psf_prssd_data) +
5073                                    sizeof(struct dasd_rssd_messages)),
5074                                   device);
5075        if (IS_ERR(cqr)) {
5076                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5077                                "Could not allocate read message buffer request");
5078                return PTR_ERR(cqr);
5079        }
5080
5081        cqr->lpm = lpum;
5082retry:
5083        cqr->startdev = device;
5084        cqr->memdev = device;
5085        cqr->block = NULL;
5086        cqr->expires = 10 * HZ;
5087        set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5088        /* dasd_sleep_on_immediatly does not do complex error
5089         * recovery so clear erp flag and set retry counter to
5090         * do basic erp */
5091        clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
5092        cqr->retries = 256;
5093
5094        /* Prepare for Read Subsystem Data */
5095        prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5096        memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
5097        prssdp->order = PSF_ORDER_PRSSD;
5098        prssdp->suborder = 0x03;        /* Message Buffer */
5099        /* all other bytes of prssdp must be zero */
5100
5101        ccw = cqr->cpaddr;
5102        ccw->cmd_code = DASD_ECKD_CCW_PSF;
5103        ccw->count = sizeof(struct dasd_psf_prssd_data);
5104        ccw->flags |= CCW_FLAG_CC;
5105        ccw->flags |= CCW_FLAG_SLI;
5106        ccw->cda = (__u32)(addr_t) prssdp;
5107
5108        /* Read Subsystem Data - message buffer */
5109        message_buf = (struct dasd_rssd_messages *) (prssdp + 1);
5110        memset(message_buf, 0, sizeof(struct dasd_rssd_messages));
5111
5112        ccw++;
5113        ccw->cmd_code = DASD_ECKD_CCW_RSSD;
5114        ccw->count = sizeof(struct dasd_rssd_messages);
5115        ccw->flags |= CCW_FLAG_SLI;
5116        ccw->cda = (__u32)(addr_t) message_buf;
5117
5118        cqr->buildclk = get_tod_clock();
5119        cqr->status = DASD_CQR_FILLED;
5120        rc = dasd_sleep_on_immediatly(cqr);
5121        if (rc == 0) {
5122                prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5123                message_buf = (struct dasd_rssd_messages *)
5124                        (prssdp + 1);
5125                memcpy(messages, message_buf,
5126                       sizeof(struct dasd_rssd_messages));
5127        } else if (cqr->lpm) {
5128                /*
5129                 * on z/VM we might not be able to do I/O on the requested path
5130                 * but instead we get the required information on any path
5131                 * so retry with open path mask
5132                 */
5133                cqr->lpm = 0;
5134                goto retry;
5135        } else
5136                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5137                                "Reading messages failed with rc=%d\n"
5138                                , rc);
5139        dasd_sfree_request(cqr, cqr->memdev);
5140        return rc;
5141}
5142
5143static int dasd_eckd_query_host_access(struct dasd_device *device,
5144                                       struct dasd_psf_query_host_access *data)
5145{
5146        struct dasd_eckd_private *private = device->private;
5147        struct dasd_psf_query_host_access *host_access;
5148        struct dasd_psf_prssd_data *prssdp;
5149        struct dasd_ccw_req *cqr;
5150        struct ccw1 *ccw;
5151        int rc;
5152
5153        /* not available for HYPER PAV alias devices */
5154        if (!device->block && private->lcu->pav == HYPER_PAV)
5155                return -EOPNOTSUPP;
5156
5157        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
5158                                   sizeof(struct dasd_psf_prssd_data) + 1,
5159                                   device);
5160        if (IS_ERR(cqr)) {
5161                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5162                                "Could not allocate read message buffer request");
5163                return PTR_ERR(cqr);
5164        }
5165        host_access = kzalloc(sizeof(*host_access), GFP_KERNEL | GFP_DMA);
5166        if (!host_access) {
5167                dasd_sfree_request(cqr, device);
5168                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5169                                "Could not allocate host_access buffer");
5170                return -ENOMEM;
5171        }
5172        cqr->startdev = device;
5173        cqr->memdev = device;
5174        cqr->block = NULL;
5175        cqr->retries = 256;
5176        cqr->expires = 10 * HZ;
5177
5178        /* Prepare for Read Subsystem Data */
5179        prssdp = (struct dasd_psf_prssd_data *) cqr->data;
5180        memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
5181        prssdp->order = PSF_ORDER_PRSSD;
5182        prssdp->suborder = PSF_SUBORDER_QHA;    /* query host access */
5183        /* LSS and Volume that will be queried */
5184        prssdp->lss = private->ned->ID;
5185        prssdp->volume = private->ned->unit_addr;
5186        /* all other bytes of prssdp must be zero */
5187
5188        ccw = cqr->cpaddr;
5189        ccw->cmd_code = DASD_ECKD_CCW_PSF;
5190        ccw->count = sizeof(struct dasd_psf_prssd_data);
5191        ccw->flags |= CCW_FLAG_CC;
5192        ccw->flags |= CCW_FLAG_SLI;
5193        ccw->cda = (__u32)(addr_t) prssdp;
5194
5195        /* Read Subsystem Data - query host access */
5196        ccw++;
5197        ccw->cmd_code = DASD_ECKD_CCW_RSSD;
5198        ccw->count = sizeof(struct dasd_psf_query_host_access);
5199        ccw->flags |= CCW_FLAG_SLI;
5200        ccw->cda = (__u32)(addr_t) host_access;
5201
5202        cqr->buildclk = get_tod_clock();
5203        cqr->status = DASD_CQR_FILLED;
5204        rc = dasd_sleep_on(cqr);
5205        if (rc == 0) {
5206                *data = *host_access;
5207        } else {
5208                DBF_EVENT_DEVID(DBF_WARNING, device->cdev,
5209                                "Reading host access data failed with rc=%d\n",
5210                                rc);
5211                rc = -EOPNOTSUPP;
5212        }
5213
5214        dasd_sfree_request(cqr, cqr->memdev);
5215        kfree(host_access);
5216        return rc;
5217}
5218/*
5219 * return number of grouped devices
5220 */
5221static int dasd_eckd_host_access_count(struct dasd_device *device)
5222{
5223        struct dasd_psf_query_host_access *access;
5224        struct dasd_ckd_path_group_entry *entry;
5225        struct dasd_ckd_host_information *info;
5226        int count = 0;
5227        int rc, i;
5228
5229        access = kzalloc(sizeof(*access), GFP_NOIO);
5230        if (!access) {
5231                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5232                                "Could not allocate access buffer");
5233                return -ENOMEM;
5234        }
5235        rc = dasd_eckd_query_host_access(device, access);
5236        if (rc) {
5237                kfree(access);
5238                return rc;
5239        }
5240
5241        info = (struct dasd_ckd_host_information *)
5242                access->host_access_information;
5243        for (i = 0; i < info->entry_count; i++) {
5244                entry = (struct dasd_ckd_path_group_entry *)
5245                        (info->entry + i * info->entry_size);
5246                if (entry->status_flags & DASD_ECKD_PG_GROUPED)
5247                        count++;
5248        }
5249
5250        kfree(access);
5251        return count;
5252}
5253
5254/*
5255 * write host access information to a sequential file
5256 */
5257static int dasd_hosts_print(struct dasd_device *device, struct seq_file *m)
5258{
5259        struct dasd_psf_query_host_access *access;
5260        struct dasd_ckd_path_group_entry *entry;
5261        struct dasd_ckd_host_information *info;
5262        char sysplex[9] = "";
5263        int rc, i, j;
5264
5265        access = kzalloc(sizeof(*access), GFP_NOIO);
5266        if (!access) {
5267                DBF_EVENT_DEVID(DBF_WARNING, device->cdev, "%s",
5268                                "Could not allocate access buffer");
5269                return -ENOMEM;
5270        }
5271        rc = dasd_eckd_query_host_access(device, access);
5272        if (rc) {
5273                kfree(access);
5274                return rc;
5275        }
5276
5277        info = (struct dasd_ckd_host_information *)
5278                access->host_access_information;
5279        for (i = 0; i < info->entry_count; i++) {
5280                entry = (struct dasd_ckd_path_group_entry *)
5281                        (info->entry + i * info->entry_size);
5282                /* PGID */
5283                seq_puts(m, "pgid ");
5284                for (j = 0; j < 11; j++)
5285                        seq_printf(m, "%02x", entry->pgid[j]);
5286                seq_putc(m, '\n');
5287                /* FLAGS */
5288                seq_printf(m, "status_flags %02x\n", entry->status_flags);
5289                /* SYSPLEX NAME */
5290                memcpy(&sysplex, &entry->sysplex_name, sizeof(sysplex) - 1);
5291                EBCASC(sysplex, sizeof(sysplex));
5292                seq_printf(m, "sysplex_name %8s\n", sysplex);
5293                /* SUPPORTED CYLINDER */
5294                seq_printf(m, "supported_cylinder %d\n", entry->cylinder);
5295                /* TIMESTAMP */
5296                seq_printf(m, "timestamp %lu\n", (unsigned long)
5297                           entry->timestamp);
5298        }
5299        kfree(access);
5300
5301        return 0;
5302}
5303
5304/*
5305 * Perform Subsystem Function - CUIR response
5306 */
5307static int
5308dasd_eckd_psf_cuir_response(struct dasd_device *device, int response,
5309                            __u32 message_id,
5310                            struct channel_path_desc *desc,
5311                            struct subchannel_id sch_id)
5312{
5313        struct dasd_psf_cuir_response *psf_cuir;
5314        struct dasd_ccw_req *cqr;
5315        struct ccw1 *ccw;
5316        int rc;
5317
5318        cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ ,
5319                                  sizeof(struct dasd_psf_cuir_response),
5320                                  device);
5321
5322        if (IS_ERR(cqr)) {
5323                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5324                           "Could not allocate PSF-CUIR request");
5325                return PTR_ERR(cqr);
5326        }
5327
5328        psf_cuir = (struct dasd_psf_cuir_response *)cqr->data;
5329        psf_cuir->order = PSF_ORDER_CUIR_RESPONSE;
5330        psf_cuir->cc = response;
5331        if (desc)
5332                psf_cuir->chpid = desc->chpid;
5333        psf_cuir->message_id = message_id;
5334        psf_cuir->cssid = sch_id.cssid;
5335        psf_cuir->ssid = sch_id.ssid;
5336        ccw = cqr->cpaddr;
5337        ccw->cmd_code = DASD_ECKD_CCW_PSF;
5338        ccw->cda = (__u32)(addr_t)psf_cuir;
5339        ccw->flags = CCW_FLAG_SLI;
5340        ccw->count = sizeof(struct dasd_psf_cuir_response);
5341
5342        cqr->startdev = device;
5343        cqr->memdev = device;
5344        cqr->block = NULL;
5345        cqr->retries = 256;
5346        cqr->expires = 10*HZ;
5347        cqr->buildclk = get_tod_clock();
5348        cqr->status = DASD_CQR_FILLED;
5349        set_bit(DASD_CQR_VERIFY_PATH, &cqr->flags);
5350
5351        rc = dasd_sleep_on(cqr);
5352
5353        dasd_sfree_request(cqr, cqr->memdev);
5354        return rc;
5355}
5356
5357/*
5358 * return configuration data that is referenced by record selector
5359 * if a record selector is specified or per default return the
5360 * conf_data pointer for the path specified by lpum
5361 */
5362static struct dasd_conf_data *dasd_eckd_get_ref_conf(struct dasd_device *device,
5363                                                     __u8 lpum,
5364                                                     struct dasd_cuir_message *cuir)
5365{
5366        struct dasd_eckd_private *private = device->private;
5367        struct dasd_conf_data *conf_data;
5368        int path, pos;
5369
5370        if (cuir->record_selector == 0)
5371                goto out;
5372        for (path = 0x80, pos = 0; path; path >>= 1, pos++) {
5373                conf_data = private->path_conf_data[pos];
5374                if (conf_data->gneq.record_selector ==
5375                    cuir->record_selector)
5376                        return conf_data;
5377        }
5378out:
5379        return private->path_conf_data[pathmask_to_pos(lpum)];
5380}
5381
5382/*
5383 * This function determines the scope of a reconfiguration request by
5384 * analysing the path and device selection data provided in the CUIR request.
5385 * Returns a path mask containing CUIR affected paths for the give device.
5386 *
5387 * If the CUIR request does not contain the required information return the
5388 * path mask of the path the attention message for the CUIR request was reveived
5389 * on.
5390 */
5391static int dasd_eckd_cuir_scope(struct dasd_device *device, __u8 lpum,
5392                                struct dasd_cuir_message *cuir)
5393{
5394        struct dasd_eckd_private *private = device->private;
5395        struct dasd_conf_data *ref_conf_data;
5396        unsigned long bitmask = 0, mask = 0;
5397        struct dasd_conf_data *conf_data;
5398        unsigned int pos, path;
5399        char *ref_gneq, *gneq;
5400        char *ref_ned, *ned;
5401        int tbcpm = 0;
5402
5403        /* if CUIR request does not specify the scope use the path
5404           the attention message was presented on */
5405        if (!cuir->ned_map ||
5406            !(cuir->neq_map[0] | cuir->neq_map[1] | cuir->neq_map[2]))
5407                return lpum;
5408
5409        /* get reference conf data */
5410        ref_conf_data = dasd_eckd_get_ref_conf(device, lpum, cuir);
5411        /* reference ned is determined by ned_map field */
5412        pos = 8 - ffs(cuir->ned_map);
5413        ref_ned = (char *)&ref_conf_data->neds[pos];
5414        ref_gneq = (char *)&ref_conf_data->gneq;
5415        /* transfer 24 bit neq_map to mask */
5416        mask = cuir->neq_map[2];
5417        mask |= cuir->neq_map[1] << 8;
5418        mask |= cuir->neq_map[0] << 16;
5419
5420        for (path = 0x80; path; path >>= 1) {
5421                /* initialise data per path */
5422                bitmask = mask;
5423                pos = pathmask_to_pos(path);
5424                conf_data = private->path_conf_data[pos];
5425                pos = 8 - ffs(cuir->ned_map);
5426                ned = (char *) &conf_data->neds[pos];
5427                /* compare reference ned and per path ned */
5428                if (memcmp(ref_ned, ned, sizeof(*ned)) != 0)
5429                        continue;
5430                gneq = (char *)&conf_data->gneq;
5431                /* compare reference gneq and per_path gneq under
5432                   24 bit mask where mask bit 0 equals byte 7 of
5433                   the gneq and mask bit 24 equals byte 31 */
5434                while (bitmask) {
5435                        pos = ffs(bitmask) - 1;
5436                        if (memcmp(&ref_gneq[31 - pos], &gneq[31 - pos], 1)
5437                            != 0)
5438                                break;
5439                        clear_bit(pos, &bitmask);
5440                }
5441                if (bitmask)
5442                        continue;
5443                /* device and path match the reference values
5444                   add path to CUIR scope */
5445                tbcpm |= path;
5446        }
5447        return tbcpm;
5448}
5449
5450static void dasd_eckd_cuir_notify_user(struct dasd_device *device,
5451                                       unsigned long paths,
5452                                       struct subchannel_id sch_id, int action)
5453{
5454        struct channel_path_desc *desc;
5455        int pos;
5456
5457        while (paths) {
5458                /* get position of bit in mask */
5459                pos = ffs(paths) - 1;
5460                /* get channel path descriptor from this position */
5461                desc = ccw_device_get_chp_desc(device->cdev, 7 - pos);
5462                if (action == CUIR_QUIESCE)
5463                        pr_warn("Service on the storage server caused path "
5464                                "%x.%02x to go offline", sch_id.cssid,
5465                                desc ? desc->chpid : 0);
5466                else if (action == CUIR_RESUME)
5467                        pr_info("Path %x.%02x is back online after service "
5468                                "on the storage server", sch_id.cssid,
5469                                desc ? desc->chpid : 0);
5470                kfree(desc);
5471                clear_bit(pos, &paths);
5472        }
5473}
5474
5475static int dasd_eckd_cuir_remove_path(struct dasd_device *device, __u8 lpum,
5476                                      struct dasd_cuir_message *cuir)
5477{
5478        unsigned long tbcpm;
5479
5480        tbcpm = dasd_eckd_cuir_scope(device, lpum, cuir);
5481        /* nothing to do if path is not in use */
5482        if (!(device->path_data.opm & tbcpm))
5483                return 0;
5484        if (!(device->path_data.opm & ~tbcpm)) {
5485                /* no path would be left if the CUIR action is taken
5486                   return error */
5487                return -EINVAL;
5488        }
5489        /* remove device from operational path mask */
5490        device->path_data.opm &= ~tbcpm;
5491        device->path_data.cuirpm |= tbcpm;
5492        return tbcpm;
5493}
5494
5495/*
5496 * walk through all devices and build a path mask to quiesce them
5497 * return an error if the last path to a device would be removed
5498 *
5499 * if only part of the devices are quiesced and an error
5500 * occurs no onlining necessary, the storage server will
5501 * notify the already set offline devices again
5502 */
5503static int dasd_eckd_cuir_quiesce(struct dasd_device *device, __u8 lpum,
5504                                  struct subchannel_id sch_id,
5505                                  struct dasd_cuir_message *cuir)
5506{
5507        struct dasd_eckd_private *private = device->private;
5508        struct alias_pav_group *pavgroup, *tempgroup;
5509        struct dasd_device *dev, *n;
5510        unsigned long paths = 0;
5511        unsigned long flags;
5512        int tbcpm;
5513
5514        /* active devices */
5515        list_for_each_entry_safe(dev, n, &private->lcu->active_devices,
5516                                 alias_list) {
5517                spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
5518                tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
5519                spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
5520                if (tbcpm < 0)
5521                        goto out_err;
5522                paths |= tbcpm;
5523        }
5524        /* inactive devices */
5525        list_for_each_entry_safe(dev, n, &private->lcu->inactive_devices,
5526                                 alias_list) {
5527                spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
5528                tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
5529                spin_unlock_irqrestore(get_ccwdev_lock(dev->cdev), flags);
5530                if (tbcpm < 0)
5531                        goto out_err;
5532                paths |= tbcpm;
5533        }
5534        /* devices in PAV groups */
5535        list_for_each_entry_safe(pavgroup, tempgroup,
5536                                 &private->lcu->grouplist, group) {
5537                list_for_each_entry_safe(dev, n, &pavgroup->baselist,
5538                                         alias_list) {
5539                        spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
5540                        tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
5541                        spin_unlock_irqrestore(
5542                                get_ccwdev_lock(dev->cdev), flags);
5543                        if (tbcpm < 0)
5544                                goto out_err;
5545                        paths |= tbcpm;
5546                }
5547                list_for_each_entry_safe(dev, n, &pavgroup->aliaslist,
5548                                         alias_list) {
5549                        spin_lock_irqsave(get_ccwdev_lock(dev->cdev), flags);
5550                        tbcpm = dasd_eckd_cuir_remove_path(dev, lpum, cuir);
5551                        spin_unlock_irqrestore(
5552                                get_ccwdev_lock(dev->cdev), flags);
5553                        if (tbcpm < 0)
5554                                goto out_err;
5555                        paths |= tbcpm;
5556                }
5557        }
5558        /* notify user about all paths affected by CUIR action */
5559        dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_QUIESCE);
5560        return 0;
5561out_err:
5562        return tbcpm;
5563}
5564
5565static int dasd_eckd_cuir_resume(struct dasd_device *device, __u8 lpum,
5566                                 struct subchannel_id sch_id,
5567                                 struct dasd_cuir_message *cuir)
5568{
5569        struct dasd_eckd_private *private = device->private;
5570        struct alias_pav_group *pavgroup, *tempgroup;
5571        struct dasd_device *dev, *n;
5572        unsigned long paths = 0;
5573        int tbcpm;
5574
5575        /*
5576         * the path may have been added through a generic path event before
5577         * only trigger path verification if the path is not already in use
5578         */
5579        list_for_each_entry_safe(dev, n,
5580                                 &private->lcu->active_devices,
5581                                 alias_list) {
5582                tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
5583                paths |= tbcpm;
5584                if (!(dev->path_data.opm & tbcpm)) {
5585                        dev->path_data.tbvpm |= tbcpm;
5586                        dasd_schedule_device_bh(dev);
5587                }
5588        }
5589        list_for_each_entry_safe(dev, n,
5590                                 &private->lcu->inactive_devices,
5591                                 alias_list) {
5592                tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
5593                paths |= tbcpm;
5594                if (!(dev->path_data.opm & tbcpm)) {
5595                        dev->path_data.tbvpm |= tbcpm;
5596                        dasd_schedule_device_bh(dev);
5597                }
5598        }
5599        /* devices in PAV groups */
5600        list_for_each_entry_safe(pavgroup, tempgroup,
5601                                 &private->lcu->grouplist,
5602                                 group) {
5603                list_for_each_entry_safe(dev, n,
5604                                         &pavgroup->baselist,
5605                                         alias_list) {
5606                        tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
5607                        paths |= tbcpm;
5608                        if (!(dev->path_data.opm & tbcpm)) {
5609                                dev->path_data.tbvpm |= tbcpm;
5610                                dasd_schedule_device_bh(dev);
5611                        }
5612                }
5613                list_for_each_entry_safe(dev, n,
5614                                         &pavgroup->aliaslist,
5615                                         alias_list) {
5616                        tbcpm = dasd_eckd_cuir_scope(dev, lpum, cuir);
5617                        paths |= tbcpm;
5618                        if (!(dev->path_data.opm & tbcpm)) {
5619                                dev->path_data.tbvpm |= tbcpm;
5620                                dasd_schedule_device_bh(dev);
5621                        }
5622                }
5623        }
5624        /* notify user about all paths affected by CUIR action */
5625        dasd_eckd_cuir_notify_user(device, paths, sch_id, CUIR_RESUME);
5626        return 0;
5627}
5628
5629static void dasd_eckd_handle_cuir(struct dasd_device *device, void *messages,
5630                                 __u8 lpum)
5631{
5632        struct dasd_cuir_message *cuir = messages;
5633        struct channel_path_desc *desc;
5634        struct subchannel_id sch_id;
5635        int pos, response;
5636
5637        DBF_DEV_EVENT(DBF_WARNING, device,
5638                      "CUIR request: %016llx %016llx %016llx %08x",
5639                      ((u64 *)cuir)[0], ((u64 *)cuir)[1], ((u64 *)cuir)[2],
5640                      ((u32 *)cuir)[3]);
5641        ccw_device_get_schid(device->cdev, &sch_id);
5642        pos = pathmask_to_pos(lpum);
5643        desc = ccw_device_get_chp_desc(device->cdev, pos);
5644
5645        if (cuir->code == CUIR_QUIESCE) {
5646                /* quiesce */
5647                if (dasd_eckd_cuir_quiesce(device, lpum, sch_id, cuir))
5648                        response = PSF_CUIR_LAST_PATH;
5649                else
5650                        response = PSF_CUIR_COMPLETED;
5651        } else if (cuir->code == CUIR_RESUME) {
5652                /* resume */
5653                dasd_eckd_cuir_resume(device, lpum, sch_id, cuir);
5654                response = PSF_CUIR_COMPLETED;
5655        } else
5656                response = PSF_CUIR_NOT_SUPPORTED;
5657
5658        dasd_eckd_psf_cuir_response(device, response,
5659                                    cuir->message_id, desc, sch_id);
5660        DBF_DEV_EVENT(DBF_WARNING, device,
5661                      "CUIR response: %d on message ID %08x", response,
5662                      cuir->message_id);
5663        /* free descriptor copy */
5664        kfree(desc);
5665        /* to make sure there is no attention left schedule work again */
5666        device->discipline->check_attention(device, lpum);
5667}
5668
5669static void dasd_eckd_check_attention_work(struct work_struct *work)
5670{
5671        struct check_attention_work_data *data;
5672        struct dasd_rssd_messages *messages;
5673        struct dasd_device *device;
5674        int rc;
5675
5676        data = container_of(work, struct check_attention_work_data, worker);
5677        device = data->device;
5678        messages = kzalloc(sizeof(*messages), GFP_KERNEL);
5679        if (!messages) {
5680                DBF_DEV_EVENT(DBF_WARNING, device, "%s",
5681                              "Could not allocate attention message buffer");
5682                goto out;
5683        }
5684        rc = dasd_eckd_read_message_buffer(device, messages, data->lpum);
5685        if (rc)
5686                goto out;
5687        if (messages->length == ATTENTION_LENGTH_CUIR &&
5688            messages->format == ATTENTION_FORMAT_CUIR)
5689                dasd_eckd_handle_cuir(device, messages, data->lpum);
5690out:
5691        dasd_put_device(device);
5692        kfree(messages);
5693        kfree(data);
5694}
5695
5696static int dasd_eckd_check_attention(struct dasd_device *device, __u8 lpum)
5697{
5698        struct check_attention_work_data *data;
5699
5700        data = kzalloc(sizeof(*data), GFP_ATOMIC);
5701        if (!data)
5702                return -ENOMEM;
5703        INIT_WORK(&data->worker, dasd_eckd_check_attention_work);
5704        dasd_get_device(device);
5705        data->device = device;
5706        data->lpum = lpum;
5707        schedule_work(&data->worker);
5708        return 0;
5709}
5710
5711static struct ccw_driver dasd_eckd_driver = {
5712        .driver = {
5713                .name   = "dasd-eckd",
5714                .owner  = THIS_MODULE,
5715        },
5716        .ids         = dasd_eckd_ids,
5717        .probe       = dasd_eckd_probe,
5718        .remove      = dasd_generic_remove,
5719        .set_offline = dasd_generic_set_offline,
5720        .set_online  = dasd_eckd_set_online,
5721        .notify      = dasd_generic_notify,
5722        .path_event  = dasd_generic_path_event,
5723        .shutdown    = dasd_generic_shutdown,
5724        .freeze      = dasd_generic_pm_freeze,
5725        .thaw        = dasd_generic_restore_device,
5726        .restore     = dasd_generic_restore_device,
5727        .uc_handler  = dasd_generic_uc_handler,
5728        .int_class   = IRQIO_DAS,
5729};
5730
5731/*
5732 * max_blocks is dependent on the amount of storage that is available
5733 * in the static io buffer for each device. Currently each device has
5734 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
5735 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
5736 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
5737 * addition we have one define extent ccw + 16 bytes of data and one
5738 * locate record ccw + 16 bytes of data. That makes:
5739 * (8192 - 24 - 136 - 8 - 16 - 8 - 16) / 16 = 499 blocks at maximum.
5740 * We want to fit two into the available memory so that we can immediately
5741 * start the next request if one finishes off. That makes 249.5 blocks
5742 * for one request. Give a little safety and the result is 240.
5743 */
5744static struct dasd_discipline dasd_eckd_discipline = {
5745        .owner = THIS_MODULE,
5746        .name = "ECKD",
5747        .ebcname = "ECKD",
5748        .max_blocks = 190,
5749        .check_device = dasd_eckd_check_characteristics,
5750        .uncheck_device = dasd_eckd_uncheck_device,
5751        .do_analysis = dasd_eckd_do_analysis,
5752        .verify_path = dasd_eckd_verify_path,
5753        .basic_to_ready = dasd_eckd_basic_to_ready,
5754        .online_to_ready = dasd_eckd_online_to_ready,
5755        .basic_to_known = dasd_eckd_basic_to_known,
5756        .fill_geometry = dasd_eckd_fill_geometry,
5757        .start_IO = dasd_start_IO,
5758        .term_IO = dasd_term_IO,
5759        .handle_terminated_request = dasd_eckd_handle_terminated_request,
5760        .format_device = dasd_eckd_format_device,
5761        .check_device_format = dasd_eckd_check_device_format,
5762        .erp_action = dasd_eckd_erp_action,
5763        .erp_postaction = dasd_eckd_erp_postaction,
5764        .check_for_device_change = dasd_eckd_check_for_device_change,
5765        .build_cp = dasd_eckd_build_alias_cp,
5766        .free_cp = dasd_eckd_free_alias_cp,
5767        .dump_sense = dasd_eckd_dump_sense,
5768        .dump_sense_dbf = dasd_eckd_dump_sense_dbf,
5769        .fill_info = dasd_eckd_fill_info,
5770        .ioctl = dasd_eckd_ioctl,
5771        .freeze = dasd_eckd_pm_freeze,
5772        .restore = dasd_eckd_restore_device,
5773        .reload = dasd_eckd_reload_device,
5774        .get_uid = dasd_eckd_get_uid,
5775        .kick_validate = dasd_eckd_kick_validate_server,
5776        .check_attention = dasd_eckd_check_attention,
5777        .host_access_count = dasd_eckd_host_access_count,
5778        .hosts_print = dasd_hosts_print,
5779};
5780
5781static int __init
5782dasd_eckd_init(void)
5783{
5784        int ret;
5785
5786        ASCEBC(dasd_eckd_discipline.ebcname, 4);
5787        dasd_reserve_req = kmalloc(sizeof(*dasd_reserve_req),
5788                                   GFP_KERNEL | GFP_DMA);
5789        if (!dasd_reserve_req)
5790                return -ENOMEM;
5791        path_verification_worker = kmalloc(sizeof(*path_verification_worker),
5792                                   GFP_KERNEL | GFP_DMA);
5793        if (!path_verification_worker) {
5794                kfree(dasd_reserve_req);
5795                return -ENOMEM;
5796        }
5797        rawpadpage = (void *)__get_free_page(GFP_KERNEL);
5798        if (!rawpadpage) {
5799                kfree(path_verification_worker);
5800                kfree(dasd_reserve_req);
5801                return -ENOMEM;
5802        }
5803        ret = ccw_driver_register(&dasd_eckd_driver);
5804        if (!ret)
5805                wait_for_device_probe();
5806        else {
5807                kfree(path_verification_worker);
5808                kfree(dasd_reserve_req);
5809                free_page((unsigned long)rawpadpage);
5810        }
5811        return ret;
5812}
5813
5814static void __exit
5815dasd_eckd_cleanup(void)
5816{
5817        ccw_driver_unregister(&dasd_eckd_driver);
5818        kfree(path_verification_worker);
5819        kfree(dasd_reserve_req);
5820        free_page((unsigned long)rawpadpage);
5821}
5822
5823module_init(dasd_eckd_init);
5824module_exit(dasd_eckd_cleanup);
5825