Back to home page

OSCL-LXR

 
 

    


0001 /* 
0002         pf.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
0003                             Under the terms of the GNU General Public License.
0004 
0005         This is the high-level driver for parallel port ATAPI disk
0006         drives based on chips supported by the paride module.
0007 
0008         By default, the driver will autoprobe for a single parallel
0009         port ATAPI disk drive, but if their individual parameters are
0010         specified, the driver can handle up to 4 drives.
0011 
0012         The behaviour of the pf driver can be altered by setting
0013         some parameters from the insmod command line.  The following
0014         parameters are adjustable:
0015 
0016             drive0      These four arguments can be arrays of       
0017             drive1      1-7 integers as follows:
0018             drive2
0019             drive3      <prt>,<pro>,<uni>,<mod>,<slv>,<lun>,<dly>
0020 
0021                         Where,
0022 
0023                 <prt>   is the base of the parallel port address for
0024                         the corresponding drive.  (required)
0025 
0026                 <pro>   is the protocol number for the adapter that
0027                         supports this drive.  These numbers are
0028                         logged by 'paride' when the protocol modules
0029                         are initialised.  (0 if not given)
0030 
0031                 <uni>   for those adapters that support chained
0032                         devices, this is the unit selector for the
0033                         chain of devices on the given port.  It should
0034                         be zero for devices that don't support chaining.
0035                         (0 if not given)
0036 
0037                 <mod>   this can be -1 to choose the best mode, or one
0038                         of the mode numbers supported by the adapter.
0039                         (-1 if not given)
0040 
0041                 <slv>   ATAPI CDroms can be jumpered to master or slave.
0042                         Set this to 0 to choose the master drive, 1 to
0043                         choose the slave, -1 (the default) to choose the
0044                         first drive found.
0045 
0046         <lun>   Some ATAPI devices support multiple LUNs.
0047                         One example is the ATAPI PD/CD drive from
0048                         Matshita/Panasonic.  This device has a 
0049                         CD drive on LUN 0 and a PD drive on LUN 1.
0050                         By default, the driver will search for the
0051                         first LUN with a supported device.  Set 
0052                         this parameter to force it to use a specific
0053                         LUN.  (default -1)
0054 
0055                 <dly>   some parallel ports require the driver to 
0056                         go more slowly.  -1 sets a default value that
0057                         should work with the chosen protocol.  Otherwise,
0058                         set this to a small integer, the larger it is
0059                         the slower the port i/o.  In some cases, setting
0060                         this to zero will speed up the device. (default -1)
0061 
0062         major   You may use this parameter to override the
0063             default major number (47) that this driver
0064             will use.  Be sure to change the device
0065             name as well.
0066 
0067         name    This parameter is a character string that
0068             contains the name the kernel will use for this
0069             device (in /proc output, for instance).
0070             (default "pf").
0071 
0072             cluster     The driver will attempt to aggregate requests
0073                         for adjacent blocks into larger multi-block
0074                         clusters.  The maximum cluster size (in 512
0075                         byte sectors) is set with this parameter.
0076                         (default 64)
0077 
0078             verbose     This parameter controls the amount of logging
0079                         that the driver will do.  Set it to 0 for
0080                         normal operation, 1 to see autoprobe progress
0081                         messages, or 2 to see additional debugging
0082                         output.  (default 0)
0083  
0084         nice        This parameter controls the driver's use of
0085             idle CPU time, at the expense of some speed.
0086 
0087         If this driver is built into the kernel, you can use the
0088         following command line parameters, with the same values
0089         as the corresponding module parameters listed above:
0090 
0091             pf.drive0
0092             pf.drive1
0093             pf.drive2
0094             pf.drive3
0095         pf.cluster
0096             pf.nice
0097 
0098         In addition, you can use the parameter pf.disable to disable
0099         the driver entirely.
0100 
0101 */
0102 
0103 /* Changes:
0104 
0105     1.01    GRG 1998.05.03  Changes for SMP.  Eliminate sti().
0106                 Fix for drives that don't clear STAT_ERR
0107                     until after next CDB delivered.
0108                 Small change in pf_completion to round
0109                 up transfer size.
0110     1.02    GRG 1998.06.16  Eliminated an Ugh
0111     1.03    GRG 1998.08.16  Use HZ in loop timings, extra debugging
0112     1.04    GRG 1998.09.24  Added jumbo support
0113 
0114 */
0115 
0116 #define PF_VERSION      "1.04"
0117 #define PF_MAJOR    47
0118 #define PF_NAME     "pf"
0119 #define PF_UNITS    4
0120 
0121 #include <linux/types.h>
0122 
0123 /* Here are things one can override from the insmod command.
0124    Most are autoprobed by paride unless set here.  Verbose is off
0125    by default.
0126 
0127 */
0128 
0129 static bool verbose = 0;
0130 static int major = PF_MAJOR;
0131 static char *name = PF_NAME;
0132 static int cluster = 64;
0133 static int nice = 0;
0134 static int disable = 0;
0135 
0136 static int drive0[7] = { 0, 0, 0, -1, -1, -1, -1 };
0137 static int drive1[7] = { 0, 0, 0, -1, -1, -1, -1 };
0138 static int drive2[7] = { 0, 0, 0, -1, -1, -1, -1 };
0139 static int drive3[7] = { 0, 0, 0, -1, -1, -1, -1 };
0140 
0141 static int (*drives[4])[7] = {&drive0, &drive1, &drive2, &drive3};
0142 static int pf_drive_count;
0143 
0144 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_LUN, D_DLY};
0145 
0146 /* end of parameters */
0147 
0148 #include <linux/module.h>
0149 #include <linux/init.h>
0150 #include <linux/fs.h>
0151 #include <linux/delay.h>
0152 #include <linux/hdreg.h>
0153 #include <linux/cdrom.h>
0154 #include <linux/spinlock.h>
0155 #include <linux/blk-mq.h>
0156 #include <linux/blkpg.h>
0157 #include <linux/mutex.h>
0158 #include <linux/uaccess.h>
0159 
0160 static DEFINE_MUTEX(pf_mutex);
0161 static DEFINE_SPINLOCK(pf_spin_lock);
0162 
0163 module_param(verbose, bool, 0644);
0164 module_param(major, int, 0);
0165 module_param(name, charp, 0);
0166 module_param(cluster, int, 0);
0167 module_param(nice, int, 0);
0168 module_param_array(drive0, int, NULL, 0);
0169 module_param_array(drive1, int, NULL, 0);
0170 module_param_array(drive2, int, NULL, 0);
0171 module_param_array(drive3, int, NULL, 0);
0172 
0173 #include "paride.h"
0174 #include "pseudo.h"
0175 
0176 /* constants for faking geometry numbers */
0177 
0178 #define PF_FD_MAX   8192    /* use FD geometry under this size */
0179 #define PF_FD_HDS   2
0180 #define PF_FD_SPT   18
0181 #define PF_HD_HDS   64
0182 #define PF_HD_SPT   32
0183 
0184 #define PF_MAX_RETRIES  5
0185 #define PF_TMO          800 /* interrupt timeout in jiffies */
0186 #define PF_SPIN_DEL     50  /* spin delay in micro-seconds  */
0187 
0188 #define PF_SPIN         (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
0189 
0190 #define STAT_ERR        0x00001
0191 #define STAT_INDEX      0x00002
0192 #define STAT_ECC        0x00004
0193 #define STAT_DRQ        0x00008
0194 #define STAT_SEEK       0x00010
0195 #define STAT_WRERR      0x00020
0196 #define STAT_READY      0x00040
0197 #define STAT_BUSY       0x00080
0198 
0199 #define ATAPI_REQ_SENSE     0x03
0200 #define ATAPI_LOCK      0x1e
0201 #define ATAPI_DOOR      0x1b
0202 #define ATAPI_MODE_SENSE    0x5a
0203 #define ATAPI_CAPACITY      0x25
0204 #define ATAPI_IDENTIFY      0x12
0205 #define ATAPI_READ_10       0x28
0206 #define ATAPI_WRITE_10      0x2a
0207 
0208 static int pf_open(struct block_device *bdev, fmode_t mode);
0209 static blk_status_t pf_queue_rq(struct blk_mq_hw_ctx *hctx,
0210                 const struct blk_mq_queue_data *bd);
0211 static int pf_ioctl(struct block_device *bdev, fmode_t mode,
0212             unsigned int cmd, unsigned long arg);
0213 static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo);
0214 
0215 static void pf_release(struct gendisk *disk, fmode_t mode);
0216 
0217 static void do_pf_read(void);
0218 static void do_pf_read_start(void);
0219 static void do_pf_write(void);
0220 static void do_pf_write_start(void);
0221 static void do_pf_read_drq(void);
0222 static void do_pf_write_done(void);
0223 
0224 #define PF_NM           0
0225 #define PF_RO           1
0226 #define PF_RW           2
0227 
0228 #define PF_NAMELEN      8
0229 
0230 struct pf_unit {
0231     struct pi_adapter pia;  /* interface to paride layer */
0232     struct pi_adapter *pi;
0233     int removable;      /* removable media device  ?  */
0234     int media_status;   /* media present ?  WP ? */
0235     int drive;      /* drive */
0236     int lun;
0237     int access;     /* count of active opens ... */
0238     int present;        /* device present ? */
0239     char name[PF_NAMELEN];  /* pf0, pf1, ... */
0240     struct gendisk *disk;
0241     struct blk_mq_tag_set tag_set;
0242     struct list_head rq_list;
0243 };
0244 
0245 static struct pf_unit units[PF_UNITS];
0246 
0247 static int pf_identify(struct pf_unit *pf);
0248 static void pf_lock(struct pf_unit *pf, int func);
0249 static void pf_eject(struct pf_unit *pf);
0250 static unsigned int pf_check_events(struct gendisk *disk,
0251                     unsigned int clearing);
0252 
0253 static char pf_scratch[512];    /* scratch block buffer */
0254 
0255 /* the variables below are used mainly in the I/O request engine, which
0256    processes only one request at a time.
0257 */
0258 
0259 static int pf_retries = 0;  /* i/o error retry count */
0260 static int pf_busy = 0;     /* request being processed ? */
0261 static struct request *pf_req;  /* current request */
0262 static int pf_block;        /* address of next requested block */
0263 static int pf_count;        /* number of blocks still to do */
0264 static int pf_run;      /* sectors in current cluster */
0265 static int pf_cmd;      /* current command READ/WRITE */
0266 static struct pf_unit *pf_current;/* unit of current request */
0267 static int pf_mask;     /* stopper for pseudo-int */
0268 static char *pf_buf;        /* buffer for request in progress */
0269 static void *par_drv;       /* reference of parport driver */
0270 
0271 /* kernel glue structures */
0272 
0273 static const struct block_device_operations pf_fops = {
0274     .owner      = THIS_MODULE,
0275     .open       = pf_open,
0276     .release    = pf_release,
0277     .ioctl      = pf_ioctl,
0278     .compat_ioctl   = pf_ioctl,
0279     .getgeo     = pf_getgeo,
0280     .check_events   = pf_check_events,
0281 };
0282 
0283 static const struct blk_mq_ops pf_mq_ops = {
0284     .queue_rq   = pf_queue_rq,
0285 };
0286 
0287 static int pf_open(struct block_device *bdev, fmode_t mode)
0288 {
0289     struct pf_unit *pf = bdev->bd_disk->private_data;
0290     int ret;
0291 
0292     mutex_lock(&pf_mutex);
0293     pf_identify(pf);
0294 
0295     ret = -ENODEV;
0296     if (pf->media_status == PF_NM)
0297         goto out;
0298 
0299     ret = -EROFS;
0300     if ((pf->media_status == PF_RO) && (mode & FMODE_WRITE))
0301         goto out;
0302 
0303     ret = 0;
0304     pf->access++;
0305     if (pf->removable)
0306         pf_lock(pf, 1);
0307 out:
0308     mutex_unlock(&pf_mutex);
0309     return ret;
0310 }
0311 
0312 static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo)
0313 {
0314     struct pf_unit *pf = bdev->bd_disk->private_data;
0315     sector_t capacity = get_capacity(pf->disk);
0316 
0317     if (capacity < PF_FD_MAX) {
0318         geo->cylinders = sector_div(capacity, PF_FD_HDS * PF_FD_SPT);
0319         geo->heads = PF_FD_HDS;
0320         geo->sectors = PF_FD_SPT;
0321     } else {
0322         geo->cylinders = sector_div(capacity, PF_HD_HDS * PF_HD_SPT);
0323         geo->heads = PF_HD_HDS;
0324         geo->sectors = PF_HD_SPT;
0325     }
0326 
0327     return 0;
0328 }
0329 
0330 static int pf_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
0331 {
0332     struct pf_unit *pf = bdev->bd_disk->private_data;
0333 
0334     if (cmd != CDROMEJECT)
0335         return -EINVAL;
0336 
0337     if (pf->access != 1)
0338         return -EBUSY;
0339     mutex_lock(&pf_mutex);
0340     pf_eject(pf);
0341     mutex_unlock(&pf_mutex);
0342 
0343     return 0;
0344 }
0345 
0346 static void pf_release(struct gendisk *disk, fmode_t mode)
0347 {
0348     struct pf_unit *pf = disk->private_data;
0349 
0350     mutex_lock(&pf_mutex);
0351     if (pf->access <= 0) {
0352         mutex_unlock(&pf_mutex);
0353         WARN_ON(1);
0354         return;
0355     }
0356 
0357     pf->access--;
0358 
0359     if (!pf->access && pf->removable)
0360         pf_lock(pf, 0);
0361 
0362     mutex_unlock(&pf_mutex);
0363 }
0364 
0365 static unsigned int pf_check_events(struct gendisk *disk, unsigned int clearing)
0366 {
0367     return DISK_EVENT_MEDIA_CHANGE;
0368 }
0369 
0370 static inline int status_reg(struct pf_unit *pf)
0371 {
0372     return pi_read_regr(pf->pi, 1, 6);
0373 }
0374 
0375 static inline int read_reg(struct pf_unit *pf, int reg)
0376 {
0377     return pi_read_regr(pf->pi, 0, reg);
0378 }
0379 
0380 static inline void write_reg(struct pf_unit *pf, int reg, int val)
0381 {
0382     pi_write_regr(pf->pi, 0, reg, val);
0383 }
0384 
0385 static int pf_wait(struct pf_unit *pf, int go, int stop, char *fun, char *msg)
0386 {
0387     int j, r, e, s, p;
0388 
0389     j = 0;
0390     while ((((r = status_reg(pf)) & go) || (stop && (!(r & stop))))
0391            && (j++ < PF_SPIN))
0392         udelay(PF_SPIN_DEL);
0393 
0394     if ((r & (STAT_ERR & stop)) || (j > PF_SPIN)) {
0395         s = read_reg(pf, 7);
0396         e = read_reg(pf, 1);
0397         p = read_reg(pf, 2);
0398         if (j > PF_SPIN)
0399             e |= 0x100;
0400         if (fun)
0401             printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
0402                    " loop=%d phase=%d\n",
0403                    pf->name, fun, msg, r, s, e, j, p);
0404         return (e << 8) + s;
0405     }
0406     return 0;
0407 }
0408 
0409 static int pf_command(struct pf_unit *pf, char *cmd, int dlen, char *fun)
0410 {
0411     pi_connect(pf->pi);
0412 
0413     write_reg(pf, 6, 0xa0+0x10*pf->drive);
0414 
0415     if (pf_wait(pf, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
0416         pi_disconnect(pf->pi);
0417         return -1;
0418     }
0419 
0420     write_reg(pf, 4, dlen % 256);
0421     write_reg(pf, 5, dlen / 256);
0422     write_reg(pf, 7, 0xa0); /* ATAPI packet command */
0423 
0424     if (pf_wait(pf, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
0425         pi_disconnect(pf->pi);
0426         return -1;
0427     }
0428 
0429     if (read_reg(pf, 2) != 1) {
0430         printk("%s: %s: command phase error\n", pf->name, fun);
0431         pi_disconnect(pf->pi);
0432         return -1;
0433     }
0434 
0435     pi_write_block(pf->pi, cmd, 12);
0436 
0437     return 0;
0438 }
0439 
0440 static int pf_completion(struct pf_unit *pf, char *buf, char *fun)
0441 {
0442     int r, s, n;
0443 
0444     r = pf_wait(pf, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
0445             fun, "completion");
0446 
0447     if ((read_reg(pf, 2) & 2) && (read_reg(pf, 7) & STAT_DRQ)) {
0448         n = (((read_reg(pf, 4) + 256 * read_reg(pf, 5)) +
0449               3) & 0xfffc);
0450         pi_read_block(pf->pi, buf, n);
0451     }
0452 
0453     s = pf_wait(pf, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
0454 
0455     pi_disconnect(pf->pi);
0456 
0457     return (r ? r : s);
0458 }
0459 
0460 static void pf_req_sense(struct pf_unit *pf, int quiet)
0461 {
0462     char rs_cmd[12] =
0463         { ATAPI_REQ_SENSE, pf->lun << 5, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
0464     char buf[16];
0465     int r;
0466 
0467     r = pf_command(pf, rs_cmd, 16, "Request sense");
0468     mdelay(1);
0469     if (!r)
0470         pf_completion(pf, buf, "Request sense");
0471 
0472     if ((!r) && (!quiet))
0473         printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
0474                pf->name, buf[2] & 0xf, buf[12], buf[13]);
0475 }
0476 
0477 static int pf_atapi(struct pf_unit *pf, char *cmd, int dlen, char *buf, char *fun)
0478 {
0479     int r;
0480 
0481     r = pf_command(pf, cmd, dlen, fun);
0482     mdelay(1);
0483     if (!r)
0484         r = pf_completion(pf, buf, fun);
0485     if (r)
0486         pf_req_sense(pf, !fun);
0487 
0488     return r;
0489 }
0490 
0491 static void pf_lock(struct pf_unit *pf, int func)
0492 {
0493     char lo_cmd[12] = { ATAPI_LOCK, pf->lun << 5, 0, 0, func, 0, 0, 0, 0, 0, 0, 0 };
0494 
0495     pf_atapi(pf, lo_cmd, 0, pf_scratch, func ? "lock" : "unlock");
0496 }
0497 
0498 static void pf_eject(struct pf_unit *pf)
0499 {
0500     char ej_cmd[12] = { ATAPI_DOOR, pf->lun << 5, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
0501 
0502     pf_lock(pf, 0);
0503     pf_atapi(pf, ej_cmd, 0, pf_scratch, "eject");
0504 }
0505 
0506 #define PF_RESET_TMO   30   /* in tenths of a second */
0507 
0508 static void pf_sleep(int cs)
0509 {
0510     schedule_timeout_interruptible(cs);
0511 }
0512 
0513 /* the ATAPI standard actually specifies the contents of all 7 registers
0514    after a reset, but the specification is ambiguous concerning the last
0515    two bytes, and different drives interpret the standard differently.
0516  */
0517 
0518 static int pf_reset(struct pf_unit *pf)
0519 {
0520     int i, k, flg;
0521     int expect[5] = { 1, 1, 1, 0x14, 0xeb };
0522 
0523     pi_connect(pf->pi);
0524     write_reg(pf, 6, 0xa0+0x10*pf->drive);
0525     write_reg(pf, 7, 8);
0526 
0527     pf_sleep(20 * HZ / 1000);
0528 
0529     k = 0;
0530     while ((k++ < PF_RESET_TMO) && (status_reg(pf) & STAT_BUSY))
0531         pf_sleep(HZ / 10);
0532 
0533     flg = 1;
0534     for (i = 0; i < 5; i++)
0535         flg &= (read_reg(pf, i + 1) == expect[i]);
0536 
0537     if (verbose) {
0538         printk("%s: Reset (%d) signature = ", pf->name, k);
0539         for (i = 0; i < 5; i++)
0540             printk("%3x", read_reg(pf, i + 1));
0541         if (!flg)
0542             printk(" (incorrect)");
0543         printk("\n");
0544     }
0545 
0546     pi_disconnect(pf->pi);
0547     return flg - 1;
0548 }
0549 
0550 static void pf_mode_sense(struct pf_unit *pf)
0551 {
0552     char ms_cmd[12] =
0553         { ATAPI_MODE_SENSE, pf->lun << 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0 };
0554     char buf[8];
0555 
0556     pf_atapi(pf, ms_cmd, 8, buf, "mode sense");
0557     pf->media_status = PF_RW;
0558     if (buf[3] & 0x80)
0559         pf->media_status = PF_RO;
0560 }
0561 
0562 static void xs(char *buf, char *targ, int offs, int len)
0563 {
0564     int j, k, l;
0565 
0566     j = 0;
0567     l = 0;
0568     for (k = 0; k < len; k++)
0569         if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
0570             l = targ[j++] = buf[k + offs];
0571     if (l == 0x20)
0572         j--;
0573     targ[j] = 0;
0574 }
0575 
0576 static int xl(char *buf, int offs)
0577 {
0578     int v, k;
0579 
0580     v = 0;
0581     for (k = 0; k < 4; k++)
0582         v = v * 256 + (buf[k + offs] & 0xff);
0583     return v;
0584 }
0585 
0586 static void pf_get_capacity(struct pf_unit *pf)
0587 {
0588     char rc_cmd[12] = { ATAPI_CAPACITY, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0589     char buf[8];
0590     int bs;
0591 
0592     if (pf_atapi(pf, rc_cmd, 8, buf, "get capacity")) {
0593         pf->media_status = PF_NM;
0594         return;
0595     }
0596     set_capacity(pf->disk, xl(buf, 0) + 1);
0597     bs = xl(buf, 4);
0598     if (bs != 512) {
0599         set_capacity(pf->disk, 0);
0600         if (verbose)
0601             printk("%s: Drive %d, LUN %d,"
0602                    " unsupported block size %d\n",
0603                    pf->name, pf->drive, pf->lun, bs);
0604     }
0605 }
0606 
0607 static int pf_identify(struct pf_unit *pf)
0608 {
0609     int dt, s;
0610     char *ms[2] = { "master", "slave" };
0611     char mf[10], id[18];
0612     char id_cmd[12] =
0613         { ATAPI_IDENTIFY, pf->lun << 5, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
0614     char buf[36];
0615 
0616     s = pf_atapi(pf, id_cmd, 36, buf, "identify");
0617     if (s)
0618         return -1;
0619 
0620     dt = buf[0] & 0x1f;
0621     if ((dt != 0) && (dt != 7)) {
0622         if (verbose)
0623             printk("%s: Drive %d, LUN %d, unsupported type %d\n",
0624                    pf->name, pf->drive, pf->lun, dt);
0625         return -1;
0626     }
0627 
0628     xs(buf, mf, 8, 8);
0629     xs(buf, id, 16, 16);
0630 
0631     pf->removable = (buf[1] & 0x80);
0632 
0633     pf_mode_sense(pf);
0634     pf_mode_sense(pf);
0635     pf_mode_sense(pf);
0636 
0637     pf_get_capacity(pf);
0638 
0639     printk("%s: %s %s, %s LUN %d, type %d",
0640            pf->name, mf, id, ms[pf->drive], pf->lun, dt);
0641     if (pf->removable)
0642         printk(", removable");
0643     if (pf->media_status == PF_NM)
0644         printk(", no media\n");
0645     else {
0646         if (pf->media_status == PF_RO)
0647             printk(", RO");
0648         printk(", %llu blocks\n",
0649             (unsigned long long)get_capacity(pf->disk));
0650     }
0651     return 0;
0652 }
0653 
0654 /*
0655  * returns 0, with id set if drive is detected, otherwise an error code.
0656  */
0657 static int pf_probe(struct pf_unit *pf)
0658 {
0659     if (pf->drive == -1) {
0660         for (pf->drive = 0; pf->drive <= 1; pf->drive++)
0661             if (!pf_reset(pf)) {
0662                 if (pf->lun != -1)
0663                     return pf_identify(pf);
0664                 else
0665                     for (pf->lun = 0; pf->lun < 8; pf->lun++)
0666                         if (!pf_identify(pf))
0667                             return 0;
0668             }
0669     } else {
0670         if (pf_reset(pf))
0671             return -1;
0672         if (pf->lun != -1)
0673             return pf_identify(pf);
0674         for (pf->lun = 0; pf->lun < 8; pf->lun++)
0675             if (!pf_identify(pf))
0676                 return 0;
0677     }
0678     return -ENODEV;
0679 }
0680 
0681 /* The i/o request engine */
0682 
0683 static int pf_start(struct pf_unit *pf, int cmd, int b, int c)
0684 {
0685     int i;
0686     char io_cmd[12] = { cmd, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0687 
0688     for (i = 0; i < 4; i++) {
0689         io_cmd[5 - i] = b & 0xff;
0690         b = b >> 8;
0691     }
0692 
0693     io_cmd[8] = c & 0xff;
0694     io_cmd[7] = (c >> 8) & 0xff;
0695 
0696     i = pf_command(pf, io_cmd, c * 512, "start i/o");
0697 
0698     mdelay(1);
0699 
0700     return i;
0701 }
0702 
0703 static int pf_ready(void)
0704 {
0705     return (((status_reg(pf_current) & (STAT_BUSY | pf_mask)) == pf_mask));
0706 }
0707 
0708 static int pf_queue;
0709 
0710 static int set_next_request(void)
0711 {
0712     struct pf_unit *pf;
0713     int old_pos = pf_queue;
0714 
0715     do {
0716         pf = &units[pf_queue];
0717         if (++pf_queue == PF_UNITS)
0718             pf_queue = 0;
0719         if (pf->present && !list_empty(&pf->rq_list)) {
0720             pf_req = list_first_entry(&pf->rq_list, struct request,
0721                             queuelist);
0722             list_del_init(&pf_req->queuelist);
0723             blk_mq_start_request(pf_req);
0724             break;
0725         }
0726     } while (pf_queue != old_pos);
0727 
0728     return pf_req != NULL;
0729 }
0730 
0731 static void pf_end_request(blk_status_t err)
0732 {
0733     if (!pf_req)
0734         return;
0735     if (!blk_update_request(pf_req, err, blk_rq_cur_bytes(pf_req))) {
0736         __blk_mq_end_request(pf_req, err);
0737         pf_req = NULL;
0738     }
0739 }
0740 
0741 static void pf_request(void)
0742 {
0743     if (pf_busy)
0744         return;
0745 repeat:
0746     if (!pf_req && !set_next_request())
0747         return;
0748 
0749     pf_current = pf_req->q->disk->private_data;
0750     pf_block = blk_rq_pos(pf_req);
0751     pf_run = blk_rq_sectors(pf_req);
0752     pf_count = blk_rq_cur_sectors(pf_req);
0753 
0754     if (pf_block + pf_count > get_capacity(pf_req->q->disk)) {
0755         pf_end_request(BLK_STS_IOERR);
0756         goto repeat;
0757     }
0758 
0759     pf_cmd = rq_data_dir(pf_req);
0760     pf_buf = bio_data(pf_req->bio);
0761     pf_retries = 0;
0762 
0763     pf_busy = 1;
0764     if (pf_cmd == READ)
0765         pi_do_claimed(pf_current->pi, do_pf_read);
0766     else if (pf_cmd == WRITE)
0767         pi_do_claimed(pf_current->pi, do_pf_write);
0768     else {
0769         pf_busy = 0;
0770         pf_end_request(BLK_STS_IOERR);
0771         goto repeat;
0772     }
0773 }
0774 
0775 static blk_status_t pf_queue_rq(struct blk_mq_hw_ctx *hctx,
0776                 const struct blk_mq_queue_data *bd)
0777 {
0778     struct pf_unit *pf = hctx->queue->queuedata;
0779 
0780     spin_lock_irq(&pf_spin_lock);
0781     list_add_tail(&bd->rq->queuelist, &pf->rq_list);
0782     pf_request();
0783     spin_unlock_irq(&pf_spin_lock);
0784 
0785     return BLK_STS_OK;
0786 }
0787 
0788 static int pf_next_buf(void)
0789 {
0790     unsigned long saved_flags;
0791 
0792     pf_count--;
0793     pf_run--;
0794     pf_buf += 512;
0795     pf_block++;
0796     if (!pf_run)
0797         return 1;
0798     if (!pf_count) {
0799         spin_lock_irqsave(&pf_spin_lock, saved_flags);
0800         pf_end_request(0);
0801         spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
0802         if (!pf_req)
0803             return 1;
0804         pf_count = blk_rq_cur_sectors(pf_req);
0805         pf_buf = bio_data(pf_req->bio);
0806     }
0807     return 0;
0808 }
0809 
0810 static inline void next_request(blk_status_t err)
0811 {
0812     unsigned long saved_flags;
0813 
0814     spin_lock_irqsave(&pf_spin_lock, saved_flags);
0815     pf_end_request(err);
0816     pf_busy = 0;
0817     pf_request();
0818     spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
0819 }
0820 
0821 /* detach from the calling context - in case the spinlock is held */
0822 static void do_pf_read(void)
0823 {
0824     ps_set_intr(do_pf_read_start, NULL, 0, nice);
0825 }
0826 
0827 static void do_pf_read_start(void)
0828 {
0829     pf_busy = 1;
0830 
0831     if (pf_start(pf_current, ATAPI_READ_10, pf_block, pf_run)) {
0832         pi_disconnect(pf_current->pi);
0833         if (pf_retries < PF_MAX_RETRIES) {
0834             pf_retries++;
0835             pi_do_claimed(pf_current->pi, do_pf_read_start);
0836             return;
0837         }
0838         next_request(BLK_STS_IOERR);
0839         return;
0840     }
0841     pf_mask = STAT_DRQ;
0842     ps_set_intr(do_pf_read_drq, pf_ready, PF_TMO, nice);
0843 }
0844 
0845 static void do_pf_read_drq(void)
0846 {
0847     while (1) {
0848         if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
0849                 "read block", "completion") & STAT_ERR) {
0850             pi_disconnect(pf_current->pi);
0851             if (pf_retries < PF_MAX_RETRIES) {
0852                 pf_req_sense(pf_current, 0);
0853                 pf_retries++;
0854                 pi_do_claimed(pf_current->pi, do_pf_read_start);
0855                 return;
0856             }
0857             next_request(BLK_STS_IOERR);
0858             return;
0859         }
0860         pi_read_block(pf_current->pi, pf_buf, 512);
0861         if (pf_next_buf())
0862             break;
0863     }
0864     pi_disconnect(pf_current->pi);
0865     next_request(0);
0866 }
0867 
0868 static void do_pf_write(void)
0869 {
0870     ps_set_intr(do_pf_write_start, NULL, 0, nice);
0871 }
0872 
0873 static void do_pf_write_start(void)
0874 {
0875     pf_busy = 1;
0876 
0877     if (pf_start(pf_current, ATAPI_WRITE_10, pf_block, pf_run)) {
0878         pi_disconnect(pf_current->pi);
0879         if (pf_retries < PF_MAX_RETRIES) {
0880             pf_retries++;
0881             pi_do_claimed(pf_current->pi, do_pf_write_start);
0882             return;
0883         }
0884         next_request(BLK_STS_IOERR);
0885         return;
0886     }
0887 
0888     while (1) {
0889         if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
0890                 "write block", "data wait") & STAT_ERR) {
0891             pi_disconnect(pf_current->pi);
0892             if (pf_retries < PF_MAX_RETRIES) {
0893                 pf_retries++;
0894                 pi_do_claimed(pf_current->pi, do_pf_write_start);
0895                 return;
0896             }
0897             next_request(BLK_STS_IOERR);
0898             return;
0899         }
0900         pi_write_block(pf_current->pi, pf_buf, 512);
0901         if (pf_next_buf())
0902             break;
0903     }
0904     pf_mask = 0;
0905     ps_set_intr(do_pf_write_done, pf_ready, PF_TMO, nice);
0906 }
0907 
0908 static void do_pf_write_done(void)
0909 {
0910     if (pf_wait(pf_current, STAT_BUSY, 0, "write block", "done") & STAT_ERR) {
0911         pi_disconnect(pf_current->pi);
0912         if (pf_retries < PF_MAX_RETRIES) {
0913             pf_retries++;
0914             pi_do_claimed(pf_current->pi, do_pf_write_start);
0915             return;
0916         }
0917         next_request(BLK_STS_IOERR);
0918         return;
0919     }
0920     pi_disconnect(pf_current->pi);
0921     next_request(0);
0922 }
0923 
0924 static int __init pf_init_unit(struct pf_unit *pf, bool autoprobe, int port,
0925         int mode, int unit, int protocol, int delay, int ms)
0926 {
0927     struct gendisk *disk;
0928     int ret;
0929 
0930     ret = blk_mq_alloc_sq_tag_set(&pf->tag_set, &pf_mq_ops, 1,
0931                       BLK_MQ_F_SHOULD_MERGE);
0932     if (ret)
0933         return ret;
0934 
0935     disk = blk_mq_alloc_disk(&pf->tag_set, pf);
0936     if (IS_ERR(disk)) {
0937         ret = PTR_ERR(disk);
0938         goto out_free_tag_set;
0939     }
0940     disk->major = major;
0941     disk->first_minor = pf - units;
0942     disk->minors = 1;
0943     strcpy(disk->disk_name, pf->name);
0944     disk->fops = &pf_fops;
0945     disk->flags |= GENHD_FL_NO_PART;
0946     disk->events = DISK_EVENT_MEDIA_CHANGE;
0947     disk->private_data = pf;
0948 
0949     blk_queue_max_segments(disk->queue, cluster);
0950     blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
0951 
0952     INIT_LIST_HEAD(&pf->rq_list);
0953     pf->disk = disk;
0954     pf->pi = &pf->pia;
0955     pf->media_status = PF_NM;
0956     pf->drive = (*drives[disk->first_minor])[D_SLV];
0957     pf->lun = (*drives[disk->first_minor])[D_LUN];
0958     snprintf(pf->name, PF_NAMELEN, "%s%d", name, disk->first_minor);
0959 
0960     if (!pi_init(pf->pi, autoprobe, port, mode, unit, protocol, delay,
0961             pf_scratch, PI_PF, verbose, pf->name)) {
0962         ret = -ENODEV;
0963         goto out_free_disk;
0964     }
0965     ret = pf_probe(pf);
0966     if (ret)
0967         goto out_pi_release;
0968 
0969     ret = add_disk(disk);
0970     if (ret)
0971         goto out_pi_release;
0972     pf->present = 1;
0973     return 0;
0974 
0975 out_pi_release:
0976     pi_release(pf->pi);
0977 out_free_disk:
0978     put_disk(pf->disk);
0979 out_free_tag_set:
0980     blk_mq_free_tag_set(&pf->tag_set);
0981     return ret;
0982 }
0983 
0984 static int __init pf_init(void)
0985 {               /* preliminary initialisation */
0986     struct pf_unit *pf;
0987     int found = 0, unit;
0988 
0989     if (disable)
0990         return -EINVAL;
0991 
0992     if (register_blkdev(major, name))
0993         return -EBUSY;
0994 
0995     printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
0996            name, name, PF_VERSION, major, cluster, nice);
0997 
0998     par_drv = pi_register_driver(name);
0999     if (!par_drv) {
1000         pr_err("failed to register %s driver\n", name);
1001         goto out_unregister_blkdev;
1002     }
1003 
1004     for (unit = 0; unit < PF_UNITS; unit++) {
1005         if (!(*drives[unit])[D_PRT])
1006             pf_drive_count++;
1007     }
1008 
1009     pf = units;
1010     if (pf_drive_count == 0) {
1011         if (pf_init_unit(pf, 1, -1, -1, -1, -1, -1, verbose))
1012             found++;
1013     } else {
1014         for (unit = 0; unit < PF_UNITS; unit++, pf++) {
1015             int *conf = *drives[unit];
1016             if (!conf[D_PRT])
1017                 continue;
1018             if (pf_init_unit(pf, 0, conf[D_PRT], conf[D_MOD],
1019                     conf[D_UNI], conf[D_PRO], conf[D_DLY],
1020                     verbose))
1021                 found++;
1022         }
1023     }
1024     if (!found) {
1025         printk("%s: No ATAPI disk detected\n", name);
1026         goto out_unregister_pi_driver;
1027     }
1028     pf_busy = 0;
1029     return 0;
1030 
1031 out_unregister_pi_driver:
1032     pi_unregister_driver(par_drv);
1033 out_unregister_blkdev:
1034     unregister_blkdev(major, name);
1035     return -ENODEV;
1036 }
1037 
1038 static void __exit pf_exit(void)
1039 {
1040     struct pf_unit *pf;
1041     int unit;
1042 
1043     for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++) {
1044         if (!pf->present)
1045             continue;
1046         del_gendisk(pf->disk);
1047         put_disk(pf->disk);
1048         blk_mq_free_tag_set(&pf->tag_set);
1049         pi_release(pf->pi);
1050     }
1051 
1052     unregister_blkdev(major, name);
1053 }
1054 
1055 MODULE_LICENSE("GPL");
1056 module_init(pf_init)
1057 module_exit(pf_exit)