cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

pf.c (26218B)


      1/* 
      2        pf.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
      3                            Under the terms of the GNU General Public License.
      4
      5        This is the high-level driver for parallel port ATAPI disk
      6        drives based on chips supported by the paride module.
      7
      8        By default, the driver will autoprobe for a single parallel
      9        port ATAPI disk drive, but if their individual parameters are
     10        specified, the driver can handle up to 4 drives.
     11
     12        The behaviour of the pf driver can be altered by setting
     13        some parameters from the insmod command line.  The following
     14        parameters are adjustable:
     15
     16            drive0      These four arguments can be arrays of       
     17            drive1      1-7 integers as follows:
     18            drive2
     19            drive3      <prt>,<pro>,<uni>,<mod>,<slv>,<lun>,<dly>
     20
     21                        Where,
     22
     23                <prt>   is the base of the parallel port address for
     24                        the corresponding drive.  (required)
     25
     26                <pro>   is the protocol number for the adapter that
     27                        supports this drive.  These numbers are
     28                        logged by 'paride' when the protocol modules
     29                        are initialised.  (0 if not given)
     30
     31                <uni>   for those adapters that support chained
     32                        devices, this is the unit selector for the
     33                        chain of devices on the given port.  It should
     34                        be zero for devices that don't support chaining.
     35                        (0 if not given)
     36
     37                <mod>   this can be -1 to choose the best mode, or one
     38                        of the mode numbers supported by the adapter.
     39                        (-1 if not given)
     40
     41                <slv>   ATAPI CDroms can be jumpered to master or slave.
     42                        Set this to 0 to choose the master drive, 1 to
     43                        choose the slave, -1 (the default) to choose the
     44                        first drive found.
     45
     46		<lun>   Some ATAPI devices support multiple LUNs.
     47                        One example is the ATAPI PD/CD drive from
     48                        Matshita/Panasonic.  This device has a 
     49                        CD drive on LUN 0 and a PD drive on LUN 1.
     50                        By default, the driver will search for the
     51                        first LUN with a supported device.  Set 
     52                        this parameter to force it to use a specific
     53                        LUN.  (default -1)
     54
     55                <dly>   some parallel ports require the driver to 
     56                        go more slowly.  -1 sets a default value that
     57                        should work with the chosen protocol.  Otherwise,
     58                        set this to a small integer, the larger it is
     59                        the slower the port i/o.  In some cases, setting
     60                        this to zero will speed up the device. (default -1)
     61
     62	    major	You may use this parameter to override the
     63			default major number (47) that this driver
     64			will use.  Be sure to change the device
     65			name as well.
     66
     67	    name	This parameter is a character string that
     68			contains the name the kernel will use for this
     69			device (in /proc output, for instance).
     70			(default "pf").
     71
     72            cluster     The driver will attempt to aggregate requests
     73                        for adjacent blocks into larger multi-block
     74                        clusters.  The maximum cluster size (in 512
     75                        byte sectors) is set with this parameter.
     76                        (default 64)
     77
     78            verbose     This parameter controls the amount of logging
     79                        that the driver will do.  Set it to 0 for
     80                        normal operation, 1 to see autoprobe progress
     81                        messages, or 2 to see additional debugging
     82                        output.  (default 0)
     83 
     84	    nice        This parameter controls the driver's use of
     85			idle CPU time, at the expense of some speed.
     86
     87        If this driver is built into the kernel, you can use the
     88        following command line parameters, with the same values
     89        as the corresponding module parameters listed above:
     90
     91            pf.drive0
     92            pf.drive1
     93            pf.drive2
     94            pf.drive3
     95	    pf.cluster
     96            pf.nice
     97
     98        In addition, you can use the parameter pf.disable to disable
     99        the driver entirely.
    100
    101*/
    102
    103/* Changes:
    104
    105	1.01	GRG 1998.05.03  Changes for SMP.  Eliminate sti().
    106				Fix for drives that don't clear STAT_ERR
    107			        until after next CDB delivered.
    108				Small change in pf_completion to round
    109				up transfer size.
    110	1.02    GRG 1998.06.16  Eliminated an Ugh
    111	1.03    GRG 1998.08.16  Use HZ in loop timings, extra debugging
    112	1.04    GRG 1998.09.24  Added jumbo support
    113
    114*/
    115
    116#define PF_VERSION      "1.04"
    117#define PF_MAJOR	47
    118#define PF_NAME		"pf"
    119#define PF_UNITS	4
    120
    121#include <linux/types.h>
    122
    123/* Here are things one can override from the insmod command.
    124   Most are autoprobed by paride unless set here.  Verbose is off
    125   by default.
    126
    127*/
    128
    129static bool verbose = 0;
    130static int major = PF_MAJOR;
    131static char *name = PF_NAME;
    132static int cluster = 64;
    133static int nice = 0;
    134static int disable = 0;
    135
    136static int drive0[7] = { 0, 0, 0, -1, -1, -1, -1 };
    137static int drive1[7] = { 0, 0, 0, -1, -1, -1, -1 };
    138static int drive2[7] = { 0, 0, 0, -1, -1, -1, -1 };
    139static int drive3[7] = { 0, 0, 0, -1, -1, -1, -1 };
    140
    141static int (*drives[4])[7] = {&drive0, &drive1, &drive2, &drive3};
    142static int pf_drive_count;
    143
    144enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_LUN, D_DLY};
    145
    146/* end of parameters */
    147
    148#include <linux/module.h>
    149#include <linux/init.h>
    150#include <linux/fs.h>
    151#include <linux/delay.h>
    152#include <linux/hdreg.h>
    153#include <linux/cdrom.h>
    154#include <linux/spinlock.h>
    155#include <linux/blk-mq.h>
    156#include <linux/blkpg.h>
    157#include <linux/mutex.h>
    158#include <linux/uaccess.h>
    159
    160static DEFINE_MUTEX(pf_mutex);
    161static DEFINE_SPINLOCK(pf_spin_lock);
    162
    163module_param(verbose, bool, 0644);
    164module_param(major, int, 0);
    165module_param(name, charp, 0);
    166module_param(cluster, int, 0);
    167module_param(nice, int, 0);
    168module_param_array(drive0, int, NULL, 0);
    169module_param_array(drive1, int, NULL, 0);
    170module_param_array(drive2, int, NULL, 0);
    171module_param_array(drive3, int, NULL, 0);
    172
    173#include "paride.h"
    174#include "pseudo.h"
    175
    176/* constants for faking geometry numbers */
    177
    178#define PF_FD_MAX	8192	/* use FD geometry under this size */
    179#define PF_FD_HDS	2
    180#define PF_FD_SPT	18
    181#define PF_HD_HDS	64
    182#define PF_HD_SPT	32
    183
    184#define PF_MAX_RETRIES  5
    185#define PF_TMO          800	/* interrupt timeout in jiffies */
    186#define PF_SPIN_DEL     50	/* spin delay in micro-seconds  */
    187
    188#define PF_SPIN         (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
    189
    190#define STAT_ERR        0x00001
    191#define STAT_INDEX      0x00002
    192#define STAT_ECC        0x00004
    193#define STAT_DRQ        0x00008
    194#define STAT_SEEK       0x00010
    195#define STAT_WRERR      0x00020
    196#define STAT_READY      0x00040
    197#define STAT_BUSY       0x00080
    198
    199#define ATAPI_REQ_SENSE		0x03
    200#define ATAPI_LOCK		0x1e
    201#define ATAPI_DOOR		0x1b
    202#define ATAPI_MODE_SENSE	0x5a
    203#define ATAPI_CAPACITY		0x25
    204#define ATAPI_IDENTIFY		0x12
    205#define ATAPI_READ_10		0x28
    206#define ATAPI_WRITE_10		0x2a
    207
    208static int pf_open(struct block_device *bdev, fmode_t mode);
    209static blk_status_t pf_queue_rq(struct blk_mq_hw_ctx *hctx,
    210				const struct blk_mq_queue_data *bd);
    211static int pf_ioctl(struct block_device *bdev, fmode_t mode,
    212		    unsigned int cmd, unsigned long arg);
    213static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo);
    214
    215static void pf_release(struct gendisk *disk, fmode_t mode);
    216
    217static void do_pf_read(void);
    218static void do_pf_read_start(void);
    219static void do_pf_write(void);
    220static void do_pf_write_start(void);
    221static void do_pf_read_drq(void);
    222static void do_pf_write_done(void);
    223
    224#define PF_NM           0
    225#define PF_RO           1
    226#define PF_RW           2
    227
    228#define PF_NAMELEN      8
    229
    230struct pf_unit {
    231	struct pi_adapter pia;	/* interface to paride layer */
    232	struct pi_adapter *pi;
    233	int removable;		/* removable media device  ?  */
    234	int media_status;	/* media present ?  WP ? */
    235	int drive;		/* drive */
    236	int lun;
    237	int access;		/* count of active opens ... */
    238	int present;		/* device present ? */
    239	char name[PF_NAMELEN];	/* pf0, pf1, ... */
    240	struct gendisk *disk;
    241	struct blk_mq_tag_set tag_set;
    242	struct list_head rq_list;
    243};
    244
    245static struct pf_unit units[PF_UNITS];
    246
    247static int pf_identify(struct pf_unit *pf);
    248static void pf_lock(struct pf_unit *pf, int func);
    249static void pf_eject(struct pf_unit *pf);
    250static unsigned int pf_check_events(struct gendisk *disk,
    251				    unsigned int clearing);
    252
    253static char pf_scratch[512];	/* scratch block buffer */
    254
    255/* the variables below are used mainly in the I/O request engine, which
    256   processes only one request at a time.
    257*/
    258
    259static int pf_retries = 0;	/* i/o error retry count */
    260static int pf_busy = 0;		/* request being processed ? */
    261static struct request *pf_req;	/* current request */
    262static int pf_block;		/* address of next requested block */
    263static int pf_count;		/* number of blocks still to do */
    264static int pf_run;		/* sectors in current cluster */
    265static int pf_cmd;		/* current command READ/WRITE */
    266static struct pf_unit *pf_current;/* unit of current request */
    267static int pf_mask;		/* stopper for pseudo-int */
    268static char *pf_buf;		/* buffer for request in progress */
    269static void *par_drv;		/* reference of parport driver */
    270
    271/* kernel glue structures */
    272
    273static const struct block_device_operations pf_fops = {
    274	.owner		= THIS_MODULE,
    275	.open		= pf_open,
    276	.release	= pf_release,
    277	.ioctl		= pf_ioctl,
    278	.compat_ioctl	= pf_ioctl,
    279	.getgeo		= pf_getgeo,
    280	.check_events	= pf_check_events,
    281};
    282
    283static const struct blk_mq_ops pf_mq_ops = {
    284	.queue_rq	= pf_queue_rq,
    285};
    286
    287static int pf_open(struct block_device *bdev, fmode_t mode)
    288{
    289	struct pf_unit *pf = bdev->bd_disk->private_data;
    290	int ret;
    291
    292	mutex_lock(&pf_mutex);
    293	pf_identify(pf);
    294
    295	ret = -ENODEV;
    296	if (pf->media_status == PF_NM)
    297		goto out;
    298
    299	ret = -EROFS;
    300	if ((pf->media_status == PF_RO) && (mode & FMODE_WRITE))
    301		goto out;
    302
    303	ret = 0;
    304	pf->access++;
    305	if (pf->removable)
    306		pf_lock(pf, 1);
    307out:
    308	mutex_unlock(&pf_mutex);
    309	return ret;
    310}
    311
    312static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo)
    313{
    314	struct pf_unit *pf = bdev->bd_disk->private_data;
    315	sector_t capacity = get_capacity(pf->disk);
    316
    317	if (capacity < PF_FD_MAX) {
    318		geo->cylinders = sector_div(capacity, PF_FD_HDS * PF_FD_SPT);
    319		geo->heads = PF_FD_HDS;
    320		geo->sectors = PF_FD_SPT;
    321	} else {
    322		geo->cylinders = sector_div(capacity, PF_HD_HDS * PF_HD_SPT);
    323		geo->heads = PF_HD_HDS;
    324		geo->sectors = PF_HD_SPT;
    325	}
    326
    327	return 0;
    328}
    329
    330static int pf_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
    331{
    332	struct pf_unit *pf = bdev->bd_disk->private_data;
    333
    334	if (cmd != CDROMEJECT)
    335		return -EINVAL;
    336
    337	if (pf->access != 1)
    338		return -EBUSY;
    339	mutex_lock(&pf_mutex);
    340	pf_eject(pf);
    341	mutex_unlock(&pf_mutex);
    342
    343	return 0;
    344}
    345
    346static void pf_release(struct gendisk *disk, fmode_t mode)
    347{
    348	struct pf_unit *pf = disk->private_data;
    349
    350	mutex_lock(&pf_mutex);
    351	if (pf->access <= 0) {
    352		mutex_unlock(&pf_mutex);
    353		WARN_ON(1);
    354		return;
    355	}
    356
    357	pf->access--;
    358
    359	if (!pf->access && pf->removable)
    360		pf_lock(pf, 0);
    361
    362	mutex_unlock(&pf_mutex);
    363}
    364
    365static unsigned int pf_check_events(struct gendisk *disk, unsigned int clearing)
    366{
    367	return DISK_EVENT_MEDIA_CHANGE;
    368}
    369
    370static inline int status_reg(struct pf_unit *pf)
    371{
    372	return pi_read_regr(pf->pi, 1, 6);
    373}
    374
    375static inline int read_reg(struct pf_unit *pf, int reg)
    376{
    377	return pi_read_regr(pf->pi, 0, reg);
    378}
    379
    380static inline void write_reg(struct pf_unit *pf, int reg, int val)
    381{
    382	pi_write_regr(pf->pi, 0, reg, val);
    383}
    384
    385static int pf_wait(struct pf_unit *pf, int go, int stop, char *fun, char *msg)
    386{
    387	int j, r, e, s, p;
    388
    389	j = 0;
    390	while ((((r = status_reg(pf)) & go) || (stop && (!(r & stop))))
    391	       && (j++ < PF_SPIN))
    392		udelay(PF_SPIN_DEL);
    393
    394	if ((r & (STAT_ERR & stop)) || (j > PF_SPIN)) {
    395		s = read_reg(pf, 7);
    396		e = read_reg(pf, 1);
    397		p = read_reg(pf, 2);
    398		if (j > PF_SPIN)
    399			e |= 0x100;
    400		if (fun)
    401			printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
    402			       " loop=%d phase=%d\n",
    403			       pf->name, fun, msg, r, s, e, j, p);
    404		return (e << 8) + s;
    405	}
    406	return 0;
    407}
    408
    409static int pf_command(struct pf_unit *pf, char *cmd, int dlen, char *fun)
    410{
    411	pi_connect(pf->pi);
    412
    413	write_reg(pf, 6, 0xa0+0x10*pf->drive);
    414
    415	if (pf_wait(pf, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
    416		pi_disconnect(pf->pi);
    417		return -1;
    418	}
    419
    420	write_reg(pf, 4, dlen % 256);
    421	write_reg(pf, 5, dlen / 256);
    422	write_reg(pf, 7, 0xa0);	/* ATAPI packet command */
    423
    424	if (pf_wait(pf, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
    425		pi_disconnect(pf->pi);
    426		return -1;
    427	}
    428
    429	if (read_reg(pf, 2) != 1) {
    430		printk("%s: %s: command phase error\n", pf->name, fun);
    431		pi_disconnect(pf->pi);
    432		return -1;
    433	}
    434
    435	pi_write_block(pf->pi, cmd, 12);
    436
    437	return 0;
    438}
    439
    440static int pf_completion(struct pf_unit *pf, char *buf, char *fun)
    441{
    442	int r, s, n;
    443
    444	r = pf_wait(pf, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
    445		    fun, "completion");
    446
    447	if ((read_reg(pf, 2) & 2) && (read_reg(pf, 7) & STAT_DRQ)) {
    448		n = (((read_reg(pf, 4) + 256 * read_reg(pf, 5)) +
    449		      3) & 0xfffc);
    450		pi_read_block(pf->pi, buf, n);
    451	}
    452
    453	s = pf_wait(pf, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
    454
    455	pi_disconnect(pf->pi);
    456
    457	return (r ? r : s);
    458}
    459
    460static void pf_req_sense(struct pf_unit *pf, int quiet)
    461{
    462	char rs_cmd[12] =
    463	    { ATAPI_REQ_SENSE, pf->lun << 5, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
    464	char buf[16];
    465	int r;
    466
    467	r = pf_command(pf, rs_cmd, 16, "Request sense");
    468	mdelay(1);
    469	if (!r)
    470		pf_completion(pf, buf, "Request sense");
    471
    472	if ((!r) && (!quiet))
    473		printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
    474		       pf->name, buf[2] & 0xf, buf[12], buf[13]);
    475}
    476
    477static int pf_atapi(struct pf_unit *pf, char *cmd, int dlen, char *buf, char *fun)
    478{
    479	int r;
    480
    481	r = pf_command(pf, cmd, dlen, fun);
    482	mdelay(1);
    483	if (!r)
    484		r = pf_completion(pf, buf, fun);
    485	if (r)
    486		pf_req_sense(pf, !fun);
    487
    488	return r;
    489}
    490
    491static void pf_lock(struct pf_unit *pf, int func)
    492{
    493	char lo_cmd[12] = { ATAPI_LOCK, pf->lun << 5, 0, 0, func, 0, 0, 0, 0, 0, 0, 0 };
    494
    495	pf_atapi(pf, lo_cmd, 0, pf_scratch, func ? "lock" : "unlock");
    496}
    497
    498static void pf_eject(struct pf_unit *pf)
    499{
    500	char ej_cmd[12] = { ATAPI_DOOR, pf->lun << 5, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
    501
    502	pf_lock(pf, 0);
    503	pf_atapi(pf, ej_cmd, 0, pf_scratch, "eject");
    504}
    505
    506#define PF_RESET_TMO   30	/* in tenths of a second */
    507
    508static void pf_sleep(int cs)
    509{
    510	schedule_timeout_interruptible(cs);
    511}
    512
    513/* the ATAPI standard actually specifies the contents of all 7 registers
    514   after a reset, but the specification is ambiguous concerning the last
    515   two bytes, and different drives interpret the standard differently.
    516 */
    517
    518static int pf_reset(struct pf_unit *pf)
    519{
    520	int i, k, flg;
    521	int expect[5] = { 1, 1, 1, 0x14, 0xeb };
    522
    523	pi_connect(pf->pi);
    524	write_reg(pf, 6, 0xa0+0x10*pf->drive);
    525	write_reg(pf, 7, 8);
    526
    527	pf_sleep(20 * HZ / 1000);
    528
    529	k = 0;
    530	while ((k++ < PF_RESET_TMO) && (status_reg(pf) & STAT_BUSY))
    531		pf_sleep(HZ / 10);
    532
    533	flg = 1;
    534	for (i = 0; i < 5; i++)
    535		flg &= (read_reg(pf, i + 1) == expect[i]);
    536
    537	if (verbose) {
    538		printk("%s: Reset (%d) signature = ", pf->name, k);
    539		for (i = 0; i < 5; i++)
    540			printk("%3x", read_reg(pf, i + 1));
    541		if (!flg)
    542			printk(" (incorrect)");
    543		printk("\n");
    544	}
    545
    546	pi_disconnect(pf->pi);
    547	return flg - 1;
    548}
    549
    550static void pf_mode_sense(struct pf_unit *pf)
    551{
    552	char ms_cmd[12] =
    553	    { ATAPI_MODE_SENSE, pf->lun << 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0 };
    554	char buf[8];
    555
    556	pf_atapi(pf, ms_cmd, 8, buf, "mode sense");
    557	pf->media_status = PF_RW;
    558	if (buf[3] & 0x80)
    559		pf->media_status = PF_RO;
    560}
    561
    562static void xs(char *buf, char *targ, int offs, int len)
    563{
    564	int j, k, l;
    565
    566	j = 0;
    567	l = 0;
    568	for (k = 0; k < len; k++)
    569		if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
    570			l = targ[j++] = buf[k + offs];
    571	if (l == 0x20)
    572		j--;
    573	targ[j] = 0;
    574}
    575
    576static int xl(char *buf, int offs)
    577{
    578	int v, k;
    579
    580	v = 0;
    581	for (k = 0; k < 4; k++)
    582		v = v * 256 + (buf[k + offs] & 0xff);
    583	return v;
    584}
    585
    586static void pf_get_capacity(struct pf_unit *pf)
    587{
    588	char rc_cmd[12] = { ATAPI_CAPACITY, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    589	char buf[8];
    590	int bs;
    591
    592	if (pf_atapi(pf, rc_cmd, 8, buf, "get capacity")) {
    593		pf->media_status = PF_NM;
    594		return;
    595	}
    596	set_capacity(pf->disk, xl(buf, 0) + 1);
    597	bs = xl(buf, 4);
    598	if (bs != 512) {
    599		set_capacity(pf->disk, 0);
    600		if (verbose)
    601			printk("%s: Drive %d, LUN %d,"
    602			       " unsupported block size %d\n",
    603			       pf->name, pf->drive, pf->lun, bs);
    604	}
    605}
    606
    607static int pf_identify(struct pf_unit *pf)
    608{
    609	int dt, s;
    610	char *ms[2] = { "master", "slave" };
    611	char mf[10], id[18];
    612	char id_cmd[12] =
    613	    { ATAPI_IDENTIFY, pf->lun << 5, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
    614	char buf[36];
    615
    616	s = pf_atapi(pf, id_cmd, 36, buf, "identify");
    617	if (s)
    618		return -1;
    619
    620	dt = buf[0] & 0x1f;
    621	if ((dt != 0) && (dt != 7)) {
    622		if (verbose)
    623			printk("%s: Drive %d, LUN %d, unsupported type %d\n",
    624			       pf->name, pf->drive, pf->lun, dt);
    625		return -1;
    626	}
    627
    628	xs(buf, mf, 8, 8);
    629	xs(buf, id, 16, 16);
    630
    631	pf->removable = (buf[1] & 0x80);
    632
    633	pf_mode_sense(pf);
    634	pf_mode_sense(pf);
    635	pf_mode_sense(pf);
    636
    637	pf_get_capacity(pf);
    638
    639	printk("%s: %s %s, %s LUN %d, type %d",
    640	       pf->name, mf, id, ms[pf->drive], pf->lun, dt);
    641	if (pf->removable)
    642		printk(", removable");
    643	if (pf->media_status == PF_NM)
    644		printk(", no media\n");
    645	else {
    646		if (pf->media_status == PF_RO)
    647			printk(", RO");
    648		printk(", %llu blocks\n",
    649			(unsigned long long)get_capacity(pf->disk));
    650	}
    651	return 0;
    652}
    653
    654/*
    655 * returns 0, with id set if drive is detected, otherwise an error code.
    656 */
    657static int pf_probe(struct pf_unit *pf)
    658{
    659	if (pf->drive == -1) {
    660		for (pf->drive = 0; pf->drive <= 1; pf->drive++)
    661			if (!pf_reset(pf)) {
    662				if (pf->lun != -1)
    663					return pf_identify(pf);
    664				else
    665					for (pf->lun = 0; pf->lun < 8; pf->lun++)
    666						if (!pf_identify(pf))
    667							return 0;
    668			}
    669	} else {
    670		if (pf_reset(pf))
    671			return -1;
    672		if (pf->lun != -1)
    673			return pf_identify(pf);
    674		for (pf->lun = 0; pf->lun < 8; pf->lun++)
    675			if (!pf_identify(pf))
    676				return 0;
    677	}
    678	return -ENODEV;
    679}
    680
    681/* The i/o request engine */
    682
    683static int pf_start(struct pf_unit *pf, int cmd, int b, int c)
    684{
    685	int i;
    686	char io_cmd[12] = { cmd, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    687
    688	for (i = 0; i < 4; i++) {
    689		io_cmd[5 - i] = b & 0xff;
    690		b = b >> 8;
    691	}
    692
    693	io_cmd[8] = c & 0xff;
    694	io_cmd[7] = (c >> 8) & 0xff;
    695
    696	i = pf_command(pf, io_cmd, c * 512, "start i/o");
    697
    698	mdelay(1);
    699
    700	return i;
    701}
    702
    703static int pf_ready(void)
    704{
    705	return (((status_reg(pf_current) & (STAT_BUSY | pf_mask)) == pf_mask));
    706}
    707
    708static int pf_queue;
    709
    710static int set_next_request(void)
    711{
    712	struct pf_unit *pf;
    713	int old_pos = pf_queue;
    714
    715	do {
    716		pf = &units[pf_queue];
    717		if (++pf_queue == PF_UNITS)
    718			pf_queue = 0;
    719		if (pf->present && !list_empty(&pf->rq_list)) {
    720			pf_req = list_first_entry(&pf->rq_list, struct request,
    721							queuelist);
    722			list_del_init(&pf_req->queuelist);
    723			blk_mq_start_request(pf_req);
    724			break;
    725		}
    726	} while (pf_queue != old_pos);
    727
    728	return pf_req != NULL;
    729}
    730
    731static void pf_end_request(blk_status_t err)
    732{
    733	if (!pf_req)
    734		return;
    735	if (!blk_update_request(pf_req, err, blk_rq_cur_bytes(pf_req))) {
    736		__blk_mq_end_request(pf_req, err);
    737		pf_req = NULL;
    738	}
    739}
    740
    741static void pf_request(void)
    742{
    743	if (pf_busy)
    744		return;
    745repeat:
    746	if (!pf_req && !set_next_request())
    747		return;
    748
    749	pf_current = pf_req->q->disk->private_data;
    750	pf_block = blk_rq_pos(pf_req);
    751	pf_run = blk_rq_sectors(pf_req);
    752	pf_count = blk_rq_cur_sectors(pf_req);
    753
    754	if (pf_block + pf_count > get_capacity(pf_req->q->disk)) {
    755		pf_end_request(BLK_STS_IOERR);
    756		goto repeat;
    757	}
    758
    759	pf_cmd = rq_data_dir(pf_req);
    760	pf_buf = bio_data(pf_req->bio);
    761	pf_retries = 0;
    762
    763	pf_busy = 1;
    764	if (pf_cmd == READ)
    765		pi_do_claimed(pf_current->pi, do_pf_read);
    766	else if (pf_cmd == WRITE)
    767		pi_do_claimed(pf_current->pi, do_pf_write);
    768	else {
    769		pf_busy = 0;
    770		pf_end_request(BLK_STS_IOERR);
    771		goto repeat;
    772	}
    773}
    774
    775static blk_status_t pf_queue_rq(struct blk_mq_hw_ctx *hctx,
    776				const struct blk_mq_queue_data *bd)
    777{
    778	struct pf_unit *pf = hctx->queue->queuedata;
    779
    780	spin_lock_irq(&pf_spin_lock);
    781	list_add_tail(&bd->rq->queuelist, &pf->rq_list);
    782	pf_request();
    783	spin_unlock_irq(&pf_spin_lock);
    784
    785	return BLK_STS_OK;
    786}
    787
    788static int pf_next_buf(void)
    789{
    790	unsigned long saved_flags;
    791
    792	pf_count--;
    793	pf_run--;
    794	pf_buf += 512;
    795	pf_block++;
    796	if (!pf_run)
    797		return 1;
    798	if (!pf_count) {
    799		spin_lock_irqsave(&pf_spin_lock, saved_flags);
    800		pf_end_request(0);
    801		spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
    802		if (!pf_req)
    803			return 1;
    804		pf_count = blk_rq_cur_sectors(pf_req);
    805		pf_buf = bio_data(pf_req->bio);
    806	}
    807	return 0;
    808}
    809
    810static inline void next_request(blk_status_t err)
    811{
    812	unsigned long saved_flags;
    813
    814	spin_lock_irqsave(&pf_spin_lock, saved_flags);
    815	pf_end_request(err);
    816	pf_busy = 0;
    817	pf_request();
    818	spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
    819}
    820
    821/* detach from the calling context - in case the spinlock is held */
    822static void do_pf_read(void)
    823{
    824	ps_set_intr(do_pf_read_start, NULL, 0, nice);
    825}
    826
    827static void do_pf_read_start(void)
    828{
    829	pf_busy = 1;
    830
    831	if (pf_start(pf_current, ATAPI_READ_10, pf_block, pf_run)) {
    832		pi_disconnect(pf_current->pi);
    833		if (pf_retries < PF_MAX_RETRIES) {
    834			pf_retries++;
    835			pi_do_claimed(pf_current->pi, do_pf_read_start);
    836			return;
    837		}
    838		next_request(BLK_STS_IOERR);
    839		return;
    840	}
    841	pf_mask = STAT_DRQ;
    842	ps_set_intr(do_pf_read_drq, pf_ready, PF_TMO, nice);
    843}
    844
    845static void do_pf_read_drq(void)
    846{
    847	while (1) {
    848		if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
    849			    "read block", "completion") & STAT_ERR) {
    850			pi_disconnect(pf_current->pi);
    851			if (pf_retries < PF_MAX_RETRIES) {
    852				pf_req_sense(pf_current, 0);
    853				pf_retries++;
    854				pi_do_claimed(pf_current->pi, do_pf_read_start);
    855				return;
    856			}
    857			next_request(BLK_STS_IOERR);
    858			return;
    859		}
    860		pi_read_block(pf_current->pi, pf_buf, 512);
    861		if (pf_next_buf())
    862			break;
    863	}
    864	pi_disconnect(pf_current->pi);
    865	next_request(0);
    866}
    867
    868static void do_pf_write(void)
    869{
    870	ps_set_intr(do_pf_write_start, NULL, 0, nice);
    871}
    872
    873static void do_pf_write_start(void)
    874{
    875	pf_busy = 1;
    876
    877	if (pf_start(pf_current, ATAPI_WRITE_10, pf_block, pf_run)) {
    878		pi_disconnect(pf_current->pi);
    879		if (pf_retries < PF_MAX_RETRIES) {
    880			pf_retries++;
    881			pi_do_claimed(pf_current->pi, do_pf_write_start);
    882			return;
    883		}
    884		next_request(BLK_STS_IOERR);
    885		return;
    886	}
    887
    888	while (1) {
    889		if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
    890			    "write block", "data wait") & STAT_ERR) {
    891			pi_disconnect(pf_current->pi);
    892			if (pf_retries < PF_MAX_RETRIES) {
    893				pf_retries++;
    894				pi_do_claimed(pf_current->pi, do_pf_write_start);
    895				return;
    896			}
    897			next_request(BLK_STS_IOERR);
    898			return;
    899		}
    900		pi_write_block(pf_current->pi, pf_buf, 512);
    901		if (pf_next_buf())
    902			break;
    903	}
    904	pf_mask = 0;
    905	ps_set_intr(do_pf_write_done, pf_ready, PF_TMO, nice);
    906}
    907
    908static void do_pf_write_done(void)
    909{
    910	if (pf_wait(pf_current, STAT_BUSY, 0, "write block", "done") & STAT_ERR) {
    911		pi_disconnect(pf_current->pi);
    912		if (pf_retries < PF_MAX_RETRIES) {
    913			pf_retries++;
    914			pi_do_claimed(pf_current->pi, do_pf_write_start);
    915			return;
    916		}
    917		next_request(BLK_STS_IOERR);
    918		return;
    919	}
    920	pi_disconnect(pf_current->pi);
    921	next_request(0);
    922}
    923
    924static int __init pf_init_unit(struct pf_unit *pf, bool autoprobe, int port,
    925		int mode, int unit, int protocol, int delay, int ms)
    926{
    927	struct gendisk *disk;
    928	int ret;
    929
    930	ret = blk_mq_alloc_sq_tag_set(&pf->tag_set, &pf_mq_ops, 1,
    931				      BLK_MQ_F_SHOULD_MERGE);
    932	if (ret)
    933		return ret;
    934
    935	disk = blk_mq_alloc_disk(&pf->tag_set, pf);
    936	if (IS_ERR(disk)) {
    937		ret = PTR_ERR(disk);
    938		goto out_free_tag_set;
    939	}
    940	disk->major = major;
    941	disk->first_minor = pf - units;
    942	disk->minors = 1;
    943	strcpy(disk->disk_name, pf->name);
    944	disk->fops = &pf_fops;
    945	disk->flags |= GENHD_FL_NO_PART;
    946	disk->events = DISK_EVENT_MEDIA_CHANGE;
    947	disk->private_data = pf;
    948
    949	blk_queue_max_segments(disk->queue, cluster);
    950	blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
    951
    952	INIT_LIST_HEAD(&pf->rq_list);
    953	pf->disk = disk;
    954	pf->pi = &pf->pia;
    955	pf->media_status = PF_NM;
    956	pf->drive = (*drives[disk->first_minor])[D_SLV];
    957	pf->lun = (*drives[disk->first_minor])[D_LUN];
    958	snprintf(pf->name, PF_NAMELEN, "%s%d", name, disk->first_minor);
    959
    960	if (!pi_init(pf->pi, autoprobe, port, mode, unit, protocol, delay,
    961			pf_scratch, PI_PF, verbose, pf->name)) {
    962		ret = -ENODEV;
    963		goto out_free_disk;
    964	}
    965	ret = pf_probe(pf);
    966	if (ret)
    967		goto out_pi_release;
    968
    969	ret = add_disk(disk);
    970	if (ret)
    971		goto out_pi_release;
    972	pf->present = 1;
    973	return 0;
    974
    975out_pi_release:
    976	pi_release(pf->pi);
    977out_free_disk:
    978	blk_cleanup_disk(pf->disk);
    979out_free_tag_set:
    980	blk_mq_free_tag_set(&pf->tag_set);
    981	return ret;
    982}
    983
    984static int __init pf_init(void)
    985{				/* preliminary initialisation */
    986	struct pf_unit *pf;
    987	int found = 0, unit;
    988
    989	if (disable)
    990		return -EINVAL;
    991
    992	if (register_blkdev(major, name))
    993		return -EBUSY;
    994
    995	printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
    996	       name, name, PF_VERSION, major, cluster, nice);
    997
    998	par_drv = pi_register_driver(name);
    999	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
   1031out_unregister_pi_driver:
   1032	pi_unregister_driver(par_drv);
   1033out_unregister_blkdev:
   1034	unregister_blkdev(major, name);
   1035	return -ENODEV;
   1036}
   1037
   1038static 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		blk_cleanup_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
   1055MODULE_LICENSE("GPL");
   1056module_init(pf_init)
   1057module_exit(pf_exit)