xref: /openbmc/linux/drivers/memstick/core/ms_block.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20ab30494SMaxim Levitsky /*
30ab30494SMaxim Levitsky  *  ms_block.c - Sony MemoryStick (legacy) storage support
40ab30494SMaxim Levitsky 
50ab30494SMaxim Levitsky  *  Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com>
60ab30494SMaxim Levitsky  *
70ab30494SMaxim Levitsky  * Minor portions of the driver were copied from mspro_block.c which is
80ab30494SMaxim Levitsky  * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
90ab30494SMaxim Levitsky  */
100ab30494SMaxim Levitsky #define DRIVER_NAME "ms_block"
110ab30494SMaxim Levitsky #define pr_fmt(fmt) DRIVER_NAME ": " fmt
120ab30494SMaxim Levitsky 
130ab30494SMaxim Levitsky #include <linux/module.h>
14db1142a8SJens Axboe #include <linux/blk-mq.h>
150ab30494SMaxim Levitsky #include <linux/memstick.h>
160ab30494SMaxim Levitsky #include <linux/idr.h>
170ab30494SMaxim Levitsky #include <linux/hdreg.h>
180ab30494SMaxim Levitsky #include <linux/delay.h>
190ab30494SMaxim Levitsky #include <linux/slab.h>
200ab30494SMaxim Levitsky #include <linux/random.h>
210ab30494SMaxim Levitsky #include <linux/bitmap.h>
220ab30494SMaxim Levitsky #include <linux/scatterlist.h>
230ab30494SMaxim Levitsky #include <linux/jiffies.h>
240ab30494SMaxim Levitsky #include <linux/workqueue.h>
250ab30494SMaxim Levitsky #include <linux/mutex.h>
260ab30494SMaxim Levitsky #include "ms_block.h"
270ab30494SMaxim Levitsky 
280ab30494SMaxim Levitsky static int debug;
290ab30494SMaxim Levitsky static int cache_flush_timeout = 1000;
300ab30494SMaxim Levitsky static bool verify_writes;
310ab30494SMaxim Levitsky 
320ab30494SMaxim Levitsky /*
330ab30494SMaxim Levitsky  * Copies section of 'sg_from' starting from offset 'offset' and with length
340ab30494SMaxim Levitsky  * 'len' To another scatterlist of to_nents enties
350ab30494SMaxim Levitsky  */
msb_sg_copy(struct scatterlist * sg_from,struct scatterlist * sg_to,int to_nents,size_t offset,size_t len)360ab30494SMaxim Levitsky static size_t msb_sg_copy(struct scatterlist *sg_from,
370ab30494SMaxim Levitsky 	struct scatterlist *sg_to, int to_nents, size_t offset, size_t len)
380ab30494SMaxim Levitsky {
390ab30494SMaxim Levitsky 	size_t copied = 0;
400ab30494SMaxim Levitsky 
410ab30494SMaxim Levitsky 	while (offset > 0) {
420ab30494SMaxim Levitsky 		if (offset >= sg_from->length) {
430ab30494SMaxim Levitsky 			if (sg_is_last(sg_from))
440ab30494SMaxim Levitsky 				return 0;
450ab30494SMaxim Levitsky 
460ab30494SMaxim Levitsky 			offset -= sg_from->length;
470ab30494SMaxim Levitsky 			sg_from = sg_next(sg_from);
480ab30494SMaxim Levitsky 			continue;
490ab30494SMaxim Levitsky 		}
500ab30494SMaxim Levitsky 
510ab30494SMaxim Levitsky 		copied = min(len, sg_from->length - offset);
520ab30494SMaxim Levitsky 		sg_set_page(sg_to, sg_page(sg_from),
530ab30494SMaxim Levitsky 			copied, sg_from->offset + offset);
540ab30494SMaxim Levitsky 
550ab30494SMaxim Levitsky 		len -= copied;
560ab30494SMaxim Levitsky 		offset = 0;
570ab30494SMaxim Levitsky 
580ab30494SMaxim Levitsky 		if (sg_is_last(sg_from) || !len)
590ab30494SMaxim Levitsky 			goto out;
600ab30494SMaxim Levitsky 
610ab30494SMaxim Levitsky 		sg_to = sg_next(sg_to);
620ab30494SMaxim Levitsky 		to_nents--;
630ab30494SMaxim Levitsky 		sg_from = sg_next(sg_from);
640ab30494SMaxim Levitsky 	}
650ab30494SMaxim Levitsky 
660ab30494SMaxim Levitsky 	while (len > sg_from->length && to_nents--) {
670ab30494SMaxim Levitsky 		len -= sg_from->length;
680ab30494SMaxim Levitsky 		copied += sg_from->length;
690ab30494SMaxim Levitsky 
700ab30494SMaxim Levitsky 		sg_set_page(sg_to, sg_page(sg_from),
710ab30494SMaxim Levitsky 				sg_from->length, sg_from->offset);
720ab30494SMaxim Levitsky 
730ab30494SMaxim Levitsky 		if (sg_is_last(sg_from) || !len)
740ab30494SMaxim Levitsky 			goto out;
750ab30494SMaxim Levitsky 
760ab30494SMaxim Levitsky 		sg_from = sg_next(sg_from);
770ab30494SMaxim Levitsky 		sg_to = sg_next(sg_to);
780ab30494SMaxim Levitsky 	}
790ab30494SMaxim Levitsky 
800ab30494SMaxim Levitsky 	if (len && to_nents) {
810ab30494SMaxim Levitsky 		sg_set_page(sg_to, sg_page(sg_from), len, sg_from->offset);
820ab30494SMaxim Levitsky 		copied += len;
830ab30494SMaxim Levitsky 	}
840ab30494SMaxim Levitsky out:
850ab30494SMaxim Levitsky 	sg_mark_end(sg_to);
860ab30494SMaxim Levitsky 	return copied;
870ab30494SMaxim Levitsky }
880ab30494SMaxim Levitsky 
890ab30494SMaxim Levitsky /*
900ab30494SMaxim Levitsky  * Compares section of 'sg' starting from offset 'offset' and with length 'len'
910ab30494SMaxim Levitsky  * to linear buffer of length 'len' at address 'buffer'
920ab30494SMaxim Levitsky  * Returns 0 if equal and  -1 otherwice
930ab30494SMaxim Levitsky  */
msb_sg_compare_to_buffer(struct scatterlist * sg,size_t offset,u8 * buffer,size_t len)940ab30494SMaxim Levitsky static int msb_sg_compare_to_buffer(struct scatterlist *sg,
950ab30494SMaxim Levitsky 					size_t offset, u8 *buffer, size_t len)
960ab30494SMaxim Levitsky {
970ab30494SMaxim Levitsky 	int retval = 0, cmplen;
980ab30494SMaxim Levitsky 	struct sg_mapping_iter miter;
990ab30494SMaxim Levitsky 
1000ab30494SMaxim Levitsky 	sg_miter_start(&miter, sg, sg_nents(sg),
1010ab30494SMaxim Levitsky 					SG_MITER_ATOMIC | SG_MITER_FROM_SG);
1020ab30494SMaxim Levitsky 
1030ab30494SMaxim Levitsky 	while (sg_miter_next(&miter) && len > 0) {
1040ab30494SMaxim Levitsky 		if (offset >= miter.length) {
1050ab30494SMaxim Levitsky 			offset -= miter.length;
1060ab30494SMaxim Levitsky 			continue;
1070ab30494SMaxim Levitsky 		}
1080ab30494SMaxim Levitsky 
1090ab30494SMaxim Levitsky 		cmplen = min(miter.length - offset, len);
1100ab30494SMaxim Levitsky 		retval = memcmp(miter.addr + offset, buffer, cmplen) ? -1 : 0;
1110ab30494SMaxim Levitsky 		if (retval)
1120ab30494SMaxim Levitsky 			break;
1130ab30494SMaxim Levitsky 
1140ab30494SMaxim Levitsky 		buffer += cmplen;
1150ab30494SMaxim Levitsky 		len -= cmplen;
1160ab30494SMaxim Levitsky 		offset = 0;
1170ab30494SMaxim Levitsky 	}
1180ab30494SMaxim Levitsky 
1190ab30494SMaxim Levitsky 	if (!retval && len)
1200ab30494SMaxim Levitsky 		retval = -1;
1210ab30494SMaxim Levitsky 
1220ab30494SMaxim Levitsky 	sg_miter_stop(&miter);
1230ab30494SMaxim Levitsky 	return retval;
1240ab30494SMaxim Levitsky }
1250ab30494SMaxim Levitsky 
1260ab30494SMaxim Levitsky 
1270ab30494SMaxim Levitsky /* Get zone at which block with logical address 'lba' lives
1280ab30494SMaxim Levitsky  * Flash is broken into zones.
1290ab30494SMaxim Levitsky  * Each zone consists of 512 eraseblocks, out of which in first
1300ab30494SMaxim Levitsky  * zone 494 are used and 496 are for all following zones.
1310ab30494SMaxim Levitsky  * Therefore zone #0 hosts blocks 0-493, zone #1 blocks 494-988, etc...
1320ab30494SMaxim Levitsky  */
msb_get_zone_from_lba(int lba)1330ab30494SMaxim Levitsky static int msb_get_zone_from_lba(int lba)
1340ab30494SMaxim Levitsky {
1350ab30494SMaxim Levitsky 	if (lba < 494)
1360ab30494SMaxim Levitsky 		return 0;
1370ab30494SMaxim Levitsky 	return ((lba - 494) / 496) + 1;
1380ab30494SMaxim Levitsky }
1390ab30494SMaxim Levitsky 
1400ab30494SMaxim Levitsky /* Get zone of physical block. Trivial */
msb_get_zone_from_pba(int pba)1410ab30494SMaxim Levitsky static int msb_get_zone_from_pba(int pba)
1420ab30494SMaxim Levitsky {
1430ab30494SMaxim Levitsky 	return pba / MS_BLOCKS_IN_ZONE;
1440ab30494SMaxim Levitsky }
1450ab30494SMaxim Levitsky 
1460ab30494SMaxim Levitsky /* Debug test to validate free block counts */
msb_validate_used_block_bitmap(struct msb_data * msb)1470ab30494SMaxim Levitsky static int msb_validate_used_block_bitmap(struct msb_data *msb)
1480ab30494SMaxim Levitsky {
1490ab30494SMaxim Levitsky 	int total_free_blocks = 0;
1500ab30494SMaxim Levitsky 	int i;
1510ab30494SMaxim Levitsky 
1520ab30494SMaxim Levitsky 	if (!debug)
1530ab30494SMaxim Levitsky 		return 0;
1540ab30494SMaxim Levitsky 
1550ab30494SMaxim Levitsky 	for (i = 0; i < msb->zone_count; i++)
1560ab30494SMaxim Levitsky 		total_free_blocks += msb->free_block_count[i];
1570ab30494SMaxim Levitsky 
1580ab30494SMaxim Levitsky 	if (msb->block_count - bitmap_weight(msb->used_blocks_bitmap,
1590ab30494SMaxim Levitsky 					msb->block_count) == total_free_blocks)
1600ab30494SMaxim Levitsky 		return 0;
1610ab30494SMaxim Levitsky 
1620ab30494SMaxim Levitsky 	pr_err("BUG: free block counts don't match the bitmap");
1630ab30494SMaxim Levitsky 	msb->read_only = true;
1640ab30494SMaxim Levitsky 	return -EINVAL;
1650ab30494SMaxim Levitsky }
1660ab30494SMaxim Levitsky 
1670ab30494SMaxim Levitsky /* Mark physical block as used */
msb_mark_block_used(struct msb_data * msb,int pba)1680ab30494SMaxim Levitsky static void msb_mark_block_used(struct msb_data *msb, int pba)
1690ab30494SMaxim Levitsky {
1700ab30494SMaxim Levitsky 	int zone = msb_get_zone_from_pba(pba);
1710ab30494SMaxim Levitsky 
1720ab30494SMaxim Levitsky 	if (test_bit(pba, msb->used_blocks_bitmap)) {
1730ab30494SMaxim Levitsky 		pr_err(
1740ab30494SMaxim Levitsky 		"BUG: attempt to mark already used pba %d as used", pba);
1750ab30494SMaxim Levitsky 		msb->read_only = true;
1760ab30494SMaxim Levitsky 		return;
1770ab30494SMaxim Levitsky 	}
1780ab30494SMaxim Levitsky 
1790ab30494SMaxim Levitsky 	if (msb_validate_used_block_bitmap(msb))
1800ab30494SMaxim Levitsky 		return;
1810ab30494SMaxim Levitsky 
1820ab30494SMaxim Levitsky 	/* No races because all IO is single threaded */
1830ab30494SMaxim Levitsky 	__set_bit(pba, msb->used_blocks_bitmap);
1840ab30494SMaxim Levitsky 	msb->free_block_count[zone]--;
1850ab30494SMaxim Levitsky }
1860ab30494SMaxim Levitsky 
1870ab30494SMaxim Levitsky /* Mark physical block as free */
msb_mark_block_unused(struct msb_data * msb,int pba)1880ab30494SMaxim Levitsky static void msb_mark_block_unused(struct msb_data *msb, int pba)
1890ab30494SMaxim Levitsky {
1900ab30494SMaxim Levitsky 	int zone = msb_get_zone_from_pba(pba);
1910ab30494SMaxim Levitsky 
1920ab30494SMaxim Levitsky 	if (!test_bit(pba, msb->used_blocks_bitmap)) {
1930ab30494SMaxim Levitsky 		pr_err("BUG: attempt to mark already unused pba %d as unused" , pba);
1940ab30494SMaxim Levitsky 		msb->read_only = true;
1950ab30494SMaxim Levitsky 		return;
1960ab30494SMaxim Levitsky 	}
1970ab30494SMaxim Levitsky 
1980ab30494SMaxim Levitsky 	if (msb_validate_used_block_bitmap(msb))
1990ab30494SMaxim Levitsky 		return;
2000ab30494SMaxim Levitsky 
2010ab30494SMaxim Levitsky 	/* No races because all IO is single threaded */
2020ab30494SMaxim Levitsky 	__clear_bit(pba, msb->used_blocks_bitmap);
2030ab30494SMaxim Levitsky 	msb->free_block_count[zone]++;
2040ab30494SMaxim Levitsky }
2050ab30494SMaxim Levitsky 
2060ab30494SMaxim Levitsky /* Invalidate current register window */
msb_invalidate_reg_window(struct msb_data * msb)2070ab30494SMaxim Levitsky static void msb_invalidate_reg_window(struct msb_data *msb)
2080ab30494SMaxim Levitsky {
2090ab30494SMaxim Levitsky 	msb->reg_addr.w_offset = offsetof(struct ms_register, id);
2100ab30494SMaxim Levitsky 	msb->reg_addr.w_length = sizeof(struct ms_id_register);
2110ab30494SMaxim Levitsky 	msb->reg_addr.r_offset = offsetof(struct ms_register, id);
2120ab30494SMaxim Levitsky 	msb->reg_addr.r_length = sizeof(struct ms_id_register);
2130ab30494SMaxim Levitsky 	msb->addr_valid = false;
2140ab30494SMaxim Levitsky }
2150ab30494SMaxim Levitsky 
2160ab30494SMaxim Levitsky /* Start a state machine */
msb_run_state_machine(struct msb_data * msb,int (* state_func)(struct memstick_dev * card,struct memstick_request ** req))2170ab30494SMaxim Levitsky static int msb_run_state_machine(struct msb_data *msb, int   (*state_func)
2180ab30494SMaxim Levitsky 		(struct memstick_dev *card, struct memstick_request **req))
2190ab30494SMaxim Levitsky {
2200ab30494SMaxim Levitsky 	struct memstick_dev *card = msb->card;
2210ab30494SMaxim Levitsky 
2220ab30494SMaxim Levitsky 	WARN_ON(msb->state != -1);
2230ab30494SMaxim Levitsky 	msb->int_polling = false;
2240ab30494SMaxim Levitsky 	msb->state = 0;
2250ab30494SMaxim Levitsky 	msb->exit_error = 0;
2260ab30494SMaxim Levitsky 
2270ab30494SMaxim Levitsky 	memset(&card->current_mrq, 0, sizeof(card->current_mrq));
2280ab30494SMaxim Levitsky 
2290ab30494SMaxim Levitsky 	card->next_request = state_func;
2300ab30494SMaxim Levitsky 	memstick_new_req(card->host);
2310ab30494SMaxim Levitsky 	wait_for_completion(&card->mrq_complete);
2320ab30494SMaxim Levitsky 
2330ab30494SMaxim Levitsky 	WARN_ON(msb->state != -1);
2340ab30494SMaxim Levitsky 	return msb->exit_error;
2350ab30494SMaxim Levitsky }
2360ab30494SMaxim Levitsky 
2370ab30494SMaxim Levitsky /* State machines call that to exit */
msb_exit_state_machine(struct msb_data * msb,int error)2380ab30494SMaxim Levitsky static int msb_exit_state_machine(struct msb_data *msb, int error)
2390ab30494SMaxim Levitsky {
2400ab30494SMaxim Levitsky 	WARN_ON(msb->state == -1);
2410ab30494SMaxim Levitsky 
2420ab30494SMaxim Levitsky 	msb->state = -1;
2430ab30494SMaxim Levitsky 	msb->exit_error = error;
2440ab30494SMaxim Levitsky 	msb->card->next_request = h_msb_default_bad;
2450ab30494SMaxim Levitsky 
2460ab30494SMaxim Levitsky 	/* Invalidate reg window on errors */
2470ab30494SMaxim Levitsky 	if (error)
2480ab30494SMaxim Levitsky 		msb_invalidate_reg_window(msb);
2490ab30494SMaxim Levitsky 
2500ab30494SMaxim Levitsky 	complete(&msb->card->mrq_complete);
2510ab30494SMaxim Levitsky 	return -ENXIO;
2520ab30494SMaxim Levitsky }
2530ab30494SMaxim Levitsky 
2540ab30494SMaxim Levitsky /* read INT register */
msb_read_int_reg(struct msb_data * msb,long timeout)2550ab30494SMaxim Levitsky static int msb_read_int_reg(struct msb_data *msb, long timeout)
2560ab30494SMaxim Levitsky {
2570ab30494SMaxim Levitsky 	struct memstick_request *mrq = &msb->card->current_mrq;
2580ab30494SMaxim Levitsky 
2590ab30494SMaxim Levitsky 	WARN_ON(msb->state == -1);
2600ab30494SMaxim Levitsky 
2610ab30494SMaxim Levitsky 	if (!msb->int_polling) {
2620ab30494SMaxim Levitsky 		msb->int_timeout = jiffies +
2630ab30494SMaxim Levitsky 			msecs_to_jiffies(timeout == -1 ? 500 : timeout);
2640ab30494SMaxim Levitsky 		msb->int_polling = true;
2650ab30494SMaxim Levitsky 	} else if (time_after(jiffies, msb->int_timeout)) {
2660ab30494SMaxim Levitsky 		mrq->data[0] = MEMSTICK_INT_CMDNAK;
2670ab30494SMaxim Levitsky 		return 0;
2680ab30494SMaxim Levitsky 	}
2690ab30494SMaxim Levitsky 
2700ab30494SMaxim Levitsky 	if ((msb->caps & MEMSTICK_CAP_AUTO_GET_INT) &&
2710ab30494SMaxim Levitsky 				mrq->need_card_int && !mrq->error) {
2720ab30494SMaxim Levitsky 		mrq->data[0] = mrq->int_reg;
2730ab30494SMaxim Levitsky 		mrq->need_card_int = false;
2740ab30494SMaxim Levitsky 		return 0;
2750ab30494SMaxim Levitsky 	} else {
2760ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
2770ab30494SMaxim Levitsky 		return 1;
2780ab30494SMaxim Levitsky 	}
2790ab30494SMaxim Levitsky }
2800ab30494SMaxim Levitsky 
2810ab30494SMaxim Levitsky /* Read a register */
msb_read_regs(struct msb_data * msb,int offset,int len)2820ab30494SMaxim Levitsky static int msb_read_regs(struct msb_data *msb, int offset, int len)
2830ab30494SMaxim Levitsky {
2840ab30494SMaxim Levitsky 	struct memstick_request *req = &msb->card->current_mrq;
2850ab30494SMaxim Levitsky 
2860ab30494SMaxim Levitsky 	if (msb->reg_addr.r_offset != offset ||
2870ab30494SMaxim Levitsky 	    msb->reg_addr.r_length != len || !msb->addr_valid) {
2880ab30494SMaxim Levitsky 
2890ab30494SMaxim Levitsky 		msb->reg_addr.r_offset = offset;
2900ab30494SMaxim Levitsky 		msb->reg_addr.r_length = len;
2910ab30494SMaxim Levitsky 		msb->addr_valid = true;
2920ab30494SMaxim Levitsky 
2930ab30494SMaxim Levitsky 		memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
2940ab30494SMaxim Levitsky 			&msb->reg_addr, sizeof(msb->reg_addr));
2950ab30494SMaxim Levitsky 		return 0;
2960ab30494SMaxim Levitsky 	}
2970ab30494SMaxim Levitsky 
2980ab30494SMaxim Levitsky 	memstick_init_req(req, MS_TPC_READ_REG, NULL, len);
2990ab30494SMaxim Levitsky 	return 1;
3000ab30494SMaxim Levitsky }
3010ab30494SMaxim Levitsky 
3020ab30494SMaxim Levitsky /* Write a card register */
msb_write_regs(struct msb_data * msb,int offset,int len,void * buf)3030ab30494SMaxim Levitsky static int msb_write_regs(struct msb_data *msb, int offset, int len, void *buf)
3040ab30494SMaxim Levitsky {
3050ab30494SMaxim Levitsky 	struct memstick_request *req = &msb->card->current_mrq;
3060ab30494SMaxim Levitsky 
3070ab30494SMaxim Levitsky 	if (msb->reg_addr.w_offset != offset ||
3080ab30494SMaxim Levitsky 		msb->reg_addr.w_length != len  || !msb->addr_valid) {
3090ab30494SMaxim Levitsky 
3100ab30494SMaxim Levitsky 		msb->reg_addr.w_offset = offset;
3110ab30494SMaxim Levitsky 		msb->reg_addr.w_length = len;
3120ab30494SMaxim Levitsky 		msb->addr_valid = true;
3130ab30494SMaxim Levitsky 
3140ab30494SMaxim Levitsky 		memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
3150ab30494SMaxim Levitsky 			&msb->reg_addr, sizeof(msb->reg_addr));
3160ab30494SMaxim Levitsky 		return 0;
3170ab30494SMaxim Levitsky 	}
3180ab30494SMaxim Levitsky 
3190ab30494SMaxim Levitsky 	memstick_init_req(req, MS_TPC_WRITE_REG, buf, len);
3200ab30494SMaxim Levitsky 	return 1;
3210ab30494SMaxim Levitsky }
3220ab30494SMaxim Levitsky 
3230ab30494SMaxim Levitsky /* Handler for absence of IO */
h_msb_default_bad(struct memstick_dev * card,struct memstick_request ** mrq)3240ab30494SMaxim Levitsky static int h_msb_default_bad(struct memstick_dev *card,
3250ab30494SMaxim Levitsky 						struct memstick_request **mrq)
3260ab30494SMaxim Levitsky {
3270ab30494SMaxim Levitsky 	return -ENXIO;
3280ab30494SMaxim Levitsky }
3290ab30494SMaxim Levitsky 
3300ab30494SMaxim Levitsky /*
3310ab30494SMaxim Levitsky  * This function is a handler for reads of one page from device.
3320ab30494SMaxim Levitsky  * Writes output to msb->current_sg, takes sector address from msb->reg.param
3330ab30494SMaxim Levitsky  * Can also be used to read extra data only. Set params accordintly.
3340ab30494SMaxim Levitsky  */
h_msb_read_page(struct memstick_dev * card,struct memstick_request ** out_mrq)3350ab30494SMaxim Levitsky static int h_msb_read_page(struct memstick_dev *card,
3360ab30494SMaxim Levitsky 					struct memstick_request **out_mrq)
3370ab30494SMaxim Levitsky {
3380ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
3390ab30494SMaxim Levitsky 	struct memstick_request *mrq = *out_mrq = &card->current_mrq;
3400ab30494SMaxim Levitsky 	struct scatterlist sg[2];
3410ab30494SMaxim Levitsky 	u8 command, intreg;
3420ab30494SMaxim Levitsky 
3430ab30494SMaxim Levitsky 	if (mrq->error) {
3440ab30494SMaxim Levitsky 		dbg("read_page, unknown error");
3450ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, mrq->error);
3460ab30494SMaxim Levitsky 	}
3470ab30494SMaxim Levitsky again:
3480ab30494SMaxim Levitsky 	switch (msb->state) {
3490ab30494SMaxim Levitsky 	case MSB_RP_SEND_BLOCK_ADDRESS:
3500ab30494SMaxim Levitsky 		/* msb_write_regs sometimes "fails" because it needs to update
3513ae61376SShubhankar Kuranagatti 		 * the reg window, and thus it returns request for that.
3523ae61376SShubhankar Kuranagatti 		 * Then we stay in this state and retry
3533ae61376SShubhankar Kuranagatti 		 */
3540ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
3550ab30494SMaxim Levitsky 			offsetof(struct ms_register, param),
3560ab30494SMaxim Levitsky 			sizeof(struct ms_param_register),
3570ab30494SMaxim Levitsky 			(unsigned char *)&msb->regs.param))
3580ab30494SMaxim Levitsky 			return 0;
3590ab30494SMaxim Levitsky 
3600ab30494SMaxim Levitsky 		msb->state = MSB_RP_SEND_READ_COMMAND;
3610ab30494SMaxim Levitsky 		return 0;
3620ab30494SMaxim Levitsky 
3630ab30494SMaxim Levitsky 	case MSB_RP_SEND_READ_COMMAND:
3640ab30494SMaxim Levitsky 		command = MS_CMD_BLOCK_READ;
3650ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
3660ab30494SMaxim Levitsky 		msb->state = MSB_RP_SEND_INT_REQ;
3670ab30494SMaxim Levitsky 		return 0;
3680ab30494SMaxim Levitsky 
3690ab30494SMaxim Levitsky 	case MSB_RP_SEND_INT_REQ:
3700ab30494SMaxim Levitsky 		msb->state = MSB_RP_RECEIVE_INT_REQ_RESULT;
3710ab30494SMaxim Levitsky 		/* If dont actually need to send the int read request (only in
3723ae61376SShubhankar Kuranagatti 		 * serial mode), then just fall through
3733ae61376SShubhankar Kuranagatti 		 */
3740ab30494SMaxim Levitsky 		if (msb_read_int_reg(msb, -1))
3750ab30494SMaxim Levitsky 			return 0;
376df561f66SGustavo A. R. Silva 		fallthrough;
3770ab30494SMaxim Levitsky 
3780ab30494SMaxim Levitsky 	case MSB_RP_RECEIVE_INT_REQ_RESULT:
3790ab30494SMaxim Levitsky 		intreg = mrq->data[0];
3800ab30494SMaxim Levitsky 		msb->regs.status.interrupt = intreg;
3810ab30494SMaxim Levitsky 
3820ab30494SMaxim Levitsky 		if (intreg & MEMSTICK_INT_CMDNAK)
3830ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EIO);
3840ab30494SMaxim Levitsky 
3850ab30494SMaxim Levitsky 		if (!(intreg & MEMSTICK_INT_CED)) {
3860ab30494SMaxim Levitsky 			msb->state = MSB_RP_SEND_INT_REQ;
3870ab30494SMaxim Levitsky 			goto again;
3880ab30494SMaxim Levitsky 		}
3890ab30494SMaxim Levitsky 
3900ab30494SMaxim Levitsky 		msb->int_polling = false;
3910ab30494SMaxim Levitsky 		msb->state = (intreg & MEMSTICK_INT_ERR) ?
3920ab30494SMaxim Levitsky 			MSB_RP_SEND_READ_STATUS_REG : MSB_RP_SEND_OOB_READ;
3930ab30494SMaxim Levitsky 		goto again;
3940ab30494SMaxim Levitsky 
3950ab30494SMaxim Levitsky 	case MSB_RP_SEND_READ_STATUS_REG:
3960ab30494SMaxim Levitsky 		 /* read the status register to understand source of the INT_ERR */
3970ab30494SMaxim Levitsky 		if (!msb_read_regs(msb,
3980ab30494SMaxim Levitsky 			offsetof(struct ms_register, status),
3990ab30494SMaxim Levitsky 			sizeof(struct ms_status_register)))
4000ab30494SMaxim Levitsky 			return 0;
4010ab30494SMaxim Levitsky 
402a0dce7f0SAndrew Morton 		msb->state = MSB_RP_RECEIVE_STATUS_REG;
4030ab30494SMaxim Levitsky 		return 0;
4040ab30494SMaxim Levitsky 
405a0dce7f0SAndrew Morton 	case MSB_RP_RECEIVE_STATUS_REG:
4060ab30494SMaxim Levitsky 		msb->regs.status = *(struct ms_status_register *)mrq->data;
4070ab30494SMaxim Levitsky 		msb->state = MSB_RP_SEND_OOB_READ;
408df561f66SGustavo A. R. Silva 		fallthrough;
4090ab30494SMaxim Levitsky 
4100ab30494SMaxim Levitsky 	case MSB_RP_SEND_OOB_READ:
4110ab30494SMaxim Levitsky 		if (!msb_read_regs(msb,
4120ab30494SMaxim Levitsky 			offsetof(struct ms_register, extra_data),
4130ab30494SMaxim Levitsky 			sizeof(struct ms_extra_data_register)))
4140ab30494SMaxim Levitsky 			return 0;
4150ab30494SMaxim Levitsky 
4160ab30494SMaxim Levitsky 		msb->state = MSB_RP_RECEIVE_OOB_READ;
4170ab30494SMaxim Levitsky 		return 0;
4180ab30494SMaxim Levitsky 
4190ab30494SMaxim Levitsky 	case MSB_RP_RECEIVE_OOB_READ:
4200ab30494SMaxim Levitsky 		msb->regs.extra_data =
4210ab30494SMaxim Levitsky 			*(struct ms_extra_data_register *) mrq->data;
4220ab30494SMaxim Levitsky 		msb->state = MSB_RP_SEND_READ_DATA;
423df561f66SGustavo A. R. Silva 		fallthrough;
4240ab30494SMaxim Levitsky 
4250ab30494SMaxim Levitsky 	case MSB_RP_SEND_READ_DATA:
4260ab30494SMaxim Levitsky 		/* Skip that state if we only read the oob */
4270ab30494SMaxim Levitsky 		if (msb->regs.param.cp == MEMSTICK_CP_EXTRA) {
4280ab30494SMaxim Levitsky 			msb->state = MSB_RP_RECEIVE_READ_DATA;
4290ab30494SMaxim Levitsky 			goto again;
4300ab30494SMaxim Levitsky 		}
4310ab30494SMaxim Levitsky 
4320ab30494SMaxim Levitsky 		sg_init_table(sg, ARRAY_SIZE(sg));
4330ab30494SMaxim Levitsky 		msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
4340ab30494SMaxim Levitsky 			msb->current_sg_offset,
4350ab30494SMaxim Levitsky 			msb->page_size);
4360ab30494SMaxim Levitsky 
4370ab30494SMaxim Levitsky 		memstick_init_req_sg(mrq, MS_TPC_READ_LONG_DATA, sg);
4380ab30494SMaxim Levitsky 		msb->state = MSB_RP_RECEIVE_READ_DATA;
4390ab30494SMaxim Levitsky 		return 0;
4400ab30494SMaxim Levitsky 
4410ab30494SMaxim Levitsky 	case MSB_RP_RECEIVE_READ_DATA:
4420ab30494SMaxim Levitsky 		if (!(msb->regs.status.interrupt & MEMSTICK_INT_ERR)) {
4430ab30494SMaxim Levitsky 			msb->current_sg_offset += msb->page_size;
4440ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, 0);
4450ab30494SMaxim Levitsky 		}
4460ab30494SMaxim Levitsky 
4470ab30494SMaxim Levitsky 		if (msb->regs.status.status1 & MEMSTICK_UNCORR_ERROR) {
4480ab30494SMaxim Levitsky 			dbg("read_page: uncorrectable error");
4490ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EBADMSG);
4500ab30494SMaxim Levitsky 		}
4510ab30494SMaxim Levitsky 
4520ab30494SMaxim Levitsky 		if (msb->regs.status.status1 & MEMSTICK_CORR_ERROR) {
4530ab30494SMaxim Levitsky 			dbg("read_page: correctable error");
4540ab30494SMaxim Levitsky 			msb->current_sg_offset += msb->page_size;
4550ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EUCLEAN);
4560ab30494SMaxim Levitsky 		} else {
4570ab30494SMaxim Levitsky 			dbg("read_page: INT error, but no status error bits");
4580ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EIO);
4590ab30494SMaxim Levitsky 		}
4600ab30494SMaxim Levitsky 	}
4610ab30494SMaxim Levitsky 
4620ab30494SMaxim Levitsky 	BUG();
4630ab30494SMaxim Levitsky }
4640ab30494SMaxim Levitsky 
4650ab30494SMaxim Levitsky /*
4660ab30494SMaxim Levitsky  * Handler of writes of exactly one block.
4670ab30494SMaxim Levitsky  * Takes address from msb->regs.param.
4680ab30494SMaxim Levitsky  * Writes same extra data to blocks, also taken
4690ab30494SMaxim Levitsky  * from msb->regs.extra
4700ab30494SMaxim Levitsky  * Returns -EBADMSG if write fails due to uncorrectable error, or -EIO if
4710ab30494SMaxim Levitsky  * device refuses to take the command or something else
4720ab30494SMaxim Levitsky  */
h_msb_write_block(struct memstick_dev * card,struct memstick_request ** out_mrq)4730ab30494SMaxim Levitsky static int h_msb_write_block(struct memstick_dev *card,
4740ab30494SMaxim Levitsky 					struct memstick_request **out_mrq)
4750ab30494SMaxim Levitsky {
4760ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
4770ab30494SMaxim Levitsky 	struct memstick_request *mrq = *out_mrq = &card->current_mrq;
4780ab30494SMaxim Levitsky 	struct scatterlist sg[2];
4790ab30494SMaxim Levitsky 	u8 intreg, command;
4800ab30494SMaxim Levitsky 
4810ab30494SMaxim Levitsky 	if (mrq->error)
4820ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, mrq->error);
4830ab30494SMaxim Levitsky 
4840ab30494SMaxim Levitsky again:
4850ab30494SMaxim Levitsky 	switch (msb->state) {
4860ab30494SMaxim Levitsky 
4870ab30494SMaxim Levitsky 	/* HACK: Jmicon handling of TPCs between 8 and
4880ab30494SMaxim Levitsky 	 *	sizeof(memstick_request.data) is broken due to hardware
4890ab30494SMaxim Levitsky 	 *	bug in PIO mode that is used for these TPCs
4900ab30494SMaxim Levitsky 	 *	Therefore split the write
4910ab30494SMaxim Levitsky 	 */
4920ab30494SMaxim Levitsky 
4930ab30494SMaxim Levitsky 	case MSB_WB_SEND_WRITE_PARAMS:
4940ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
4950ab30494SMaxim Levitsky 			offsetof(struct ms_register, param),
4960ab30494SMaxim Levitsky 			sizeof(struct ms_param_register),
4970ab30494SMaxim Levitsky 			&msb->regs.param))
4980ab30494SMaxim Levitsky 			return 0;
4990ab30494SMaxim Levitsky 
5000ab30494SMaxim Levitsky 		msb->state = MSB_WB_SEND_WRITE_OOB;
5010ab30494SMaxim Levitsky 		return 0;
5020ab30494SMaxim Levitsky 
5030ab30494SMaxim Levitsky 	case MSB_WB_SEND_WRITE_OOB:
5040ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
5050ab30494SMaxim Levitsky 			offsetof(struct ms_register, extra_data),
5060ab30494SMaxim Levitsky 			sizeof(struct ms_extra_data_register),
5070ab30494SMaxim Levitsky 			&msb->regs.extra_data))
5080ab30494SMaxim Levitsky 			return 0;
5090ab30494SMaxim Levitsky 		msb->state = MSB_WB_SEND_WRITE_COMMAND;
5100ab30494SMaxim Levitsky 		return 0;
5110ab30494SMaxim Levitsky 
5120ab30494SMaxim Levitsky 
5130ab30494SMaxim Levitsky 	case MSB_WB_SEND_WRITE_COMMAND:
5140ab30494SMaxim Levitsky 		command = MS_CMD_BLOCK_WRITE;
5150ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
5160ab30494SMaxim Levitsky 		msb->state = MSB_WB_SEND_INT_REQ;
5170ab30494SMaxim Levitsky 		return 0;
5180ab30494SMaxim Levitsky 
5190ab30494SMaxim Levitsky 	case MSB_WB_SEND_INT_REQ:
5200ab30494SMaxim Levitsky 		msb->state = MSB_WB_RECEIVE_INT_REQ;
5210ab30494SMaxim Levitsky 		if (msb_read_int_reg(msb, -1))
5220ab30494SMaxim Levitsky 			return 0;
523df561f66SGustavo A. R. Silva 		fallthrough;
5240ab30494SMaxim Levitsky 
5250ab30494SMaxim Levitsky 	case MSB_WB_RECEIVE_INT_REQ:
5260ab30494SMaxim Levitsky 		intreg = mrq->data[0];
5270ab30494SMaxim Levitsky 		msb->regs.status.interrupt = intreg;
5280ab30494SMaxim Levitsky 
5290ab30494SMaxim Levitsky 		/* errors mean out of here, and fast... */
5300ab30494SMaxim Levitsky 		if (intreg & (MEMSTICK_INT_CMDNAK))
5310ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EIO);
5320ab30494SMaxim Levitsky 
5330ab30494SMaxim Levitsky 		if (intreg & MEMSTICK_INT_ERR)
5340ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EBADMSG);
5350ab30494SMaxim Levitsky 
5360ab30494SMaxim Levitsky 
5370ab30494SMaxim Levitsky 		/* for last page we need to poll CED */
5380ab30494SMaxim Levitsky 		if (msb->current_page == msb->pages_in_block) {
5390ab30494SMaxim Levitsky 			if (intreg & MEMSTICK_INT_CED)
5400ab30494SMaxim Levitsky 				return msb_exit_state_machine(msb, 0);
5410ab30494SMaxim Levitsky 			msb->state = MSB_WB_SEND_INT_REQ;
5420ab30494SMaxim Levitsky 			goto again;
5430ab30494SMaxim Levitsky 
5440ab30494SMaxim Levitsky 		}
5450ab30494SMaxim Levitsky 
5460ab30494SMaxim Levitsky 		/* for non-last page we need BREQ before writing next chunk */
5470ab30494SMaxim Levitsky 		if (!(intreg & MEMSTICK_INT_BREQ)) {
5480ab30494SMaxim Levitsky 			msb->state = MSB_WB_SEND_INT_REQ;
5490ab30494SMaxim Levitsky 			goto again;
5500ab30494SMaxim Levitsky 		}
5510ab30494SMaxim Levitsky 
5520ab30494SMaxim Levitsky 		msb->int_polling = false;
5530ab30494SMaxim Levitsky 		msb->state = MSB_WB_SEND_WRITE_DATA;
554df561f66SGustavo A. R. Silva 		fallthrough;
5550ab30494SMaxim Levitsky 
5560ab30494SMaxim Levitsky 	case MSB_WB_SEND_WRITE_DATA:
5570ab30494SMaxim Levitsky 		sg_init_table(sg, ARRAY_SIZE(sg));
5580ab30494SMaxim Levitsky 
5590ab30494SMaxim Levitsky 		if (msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
5600ab30494SMaxim Levitsky 			msb->current_sg_offset,
5610ab30494SMaxim Levitsky 			msb->page_size) < msb->page_size)
5620ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EIO);
5630ab30494SMaxim Levitsky 
5640ab30494SMaxim Levitsky 		memstick_init_req_sg(mrq, MS_TPC_WRITE_LONG_DATA, sg);
5650ab30494SMaxim Levitsky 		mrq->need_card_int = 1;
5660ab30494SMaxim Levitsky 		msb->state = MSB_WB_RECEIVE_WRITE_CONFIRMATION;
5670ab30494SMaxim Levitsky 		return 0;
5680ab30494SMaxim Levitsky 
5690ab30494SMaxim Levitsky 	case MSB_WB_RECEIVE_WRITE_CONFIRMATION:
5700ab30494SMaxim Levitsky 		msb->current_page++;
5710ab30494SMaxim Levitsky 		msb->current_sg_offset += msb->page_size;
5720ab30494SMaxim Levitsky 		msb->state = MSB_WB_SEND_INT_REQ;
5730ab30494SMaxim Levitsky 		goto again;
5740ab30494SMaxim Levitsky 	default:
5750ab30494SMaxim Levitsky 		BUG();
5760ab30494SMaxim Levitsky 	}
5770ab30494SMaxim Levitsky 
5780ab30494SMaxim Levitsky 	return 0;
5790ab30494SMaxim Levitsky }
5800ab30494SMaxim Levitsky 
5810ab30494SMaxim Levitsky /*
5820ab30494SMaxim Levitsky  * This function is used to send simple IO requests to device that consist
5830ab30494SMaxim Levitsky  * of register write + command
5840ab30494SMaxim Levitsky  */
h_msb_send_command(struct memstick_dev * card,struct memstick_request ** out_mrq)5850ab30494SMaxim Levitsky static int h_msb_send_command(struct memstick_dev *card,
5860ab30494SMaxim Levitsky 					struct memstick_request **out_mrq)
5870ab30494SMaxim Levitsky {
5880ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
5890ab30494SMaxim Levitsky 	struct memstick_request *mrq = *out_mrq = &card->current_mrq;
5900ab30494SMaxim Levitsky 	u8 intreg;
5910ab30494SMaxim Levitsky 
5920ab30494SMaxim Levitsky 	if (mrq->error) {
5930ab30494SMaxim Levitsky 		dbg("send_command: unknown error");
5940ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, mrq->error);
5950ab30494SMaxim Levitsky 	}
5960ab30494SMaxim Levitsky again:
5970ab30494SMaxim Levitsky 	switch (msb->state) {
5980ab30494SMaxim Levitsky 
5990ab30494SMaxim Levitsky 	/* HACK: see h_msb_write_block */
6000ab30494SMaxim Levitsky 	case MSB_SC_SEND_WRITE_PARAMS: /* write param register*/
6010ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
6020ab30494SMaxim Levitsky 			offsetof(struct ms_register, param),
6030ab30494SMaxim Levitsky 			sizeof(struct ms_param_register),
6040ab30494SMaxim Levitsky 			&msb->regs.param))
6050ab30494SMaxim Levitsky 			return 0;
6060ab30494SMaxim Levitsky 		msb->state = MSB_SC_SEND_WRITE_OOB;
6070ab30494SMaxim Levitsky 		return 0;
6080ab30494SMaxim Levitsky 
6090ab30494SMaxim Levitsky 	case MSB_SC_SEND_WRITE_OOB:
6100ab30494SMaxim Levitsky 		if (!msb->command_need_oob) {
6110ab30494SMaxim Levitsky 			msb->state = MSB_SC_SEND_COMMAND;
6120ab30494SMaxim Levitsky 			goto again;
6130ab30494SMaxim Levitsky 		}
6140ab30494SMaxim Levitsky 
6150ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
6160ab30494SMaxim Levitsky 			offsetof(struct ms_register, extra_data),
6170ab30494SMaxim Levitsky 			sizeof(struct ms_extra_data_register),
6180ab30494SMaxim Levitsky 			&msb->regs.extra_data))
6190ab30494SMaxim Levitsky 			return 0;
6200ab30494SMaxim Levitsky 
6210ab30494SMaxim Levitsky 		msb->state = MSB_SC_SEND_COMMAND;
6220ab30494SMaxim Levitsky 		return 0;
6230ab30494SMaxim Levitsky 
6240ab30494SMaxim Levitsky 	case MSB_SC_SEND_COMMAND:
6250ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_SET_CMD, &msb->command_value, 1);
6260ab30494SMaxim Levitsky 		msb->state = MSB_SC_SEND_INT_REQ;
6270ab30494SMaxim Levitsky 		return 0;
6280ab30494SMaxim Levitsky 
6290ab30494SMaxim Levitsky 	case MSB_SC_SEND_INT_REQ:
6300ab30494SMaxim Levitsky 		msb->state = MSB_SC_RECEIVE_INT_REQ;
6310ab30494SMaxim Levitsky 		if (msb_read_int_reg(msb, -1))
6320ab30494SMaxim Levitsky 			return 0;
633df561f66SGustavo A. R. Silva 		fallthrough;
6340ab30494SMaxim Levitsky 
6350ab30494SMaxim Levitsky 	case MSB_SC_RECEIVE_INT_REQ:
6360ab30494SMaxim Levitsky 		intreg = mrq->data[0];
6370ab30494SMaxim Levitsky 
6380ab30494SMaxim Levitsky 		if (intreg & MEMSTICK_INT_CMDNAK)
6390ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EIO);
6400ab30494SMaxim Levitsky 		if (intreg & MEMSTICK_INT_ERR)
6410ab30494SMaxim Levitsky 			return msb_exit_state_machine(msb, -EBADMSG);
6420ab30494SMaxim Levitsky 
6430ab30494SMaxim Levitsky 		if (!(intreg & MEMSTICK_INT_CED)) {
6440ab30494SMaxim Levitsky 			msb->state = MSB_SC_SEND_INT_REQ;
6450ab30494SMaxim Levitsky 			goto again;
6460ab30494SMaxim Levitsky 		}
6470ab30494SMaxim Levitsky 
6480ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, 0);
6490ab30494SMaxim Levitsky 	}
6500ab30494SMaxim Levitsky 
6510ab30494SMaxim Levitsky 	BUG();
6520ab30494SMaxim Levitsky }
6530ab30494SMaxim Levitsky 
6540ab30494SMaxim Levitsky /* Small handler for card reset */
h_msb_reset(struct memstick_dev * card,struct memstick_request ** out_mrq)6550ab30494SMaxim Levitsky static int h_msb_reset(struct memstick_dev *card,
6560ab30494SMaxim Levitsky 					struct memstick_request **out_mrq)
6570ab30494SMaxim Levitsky {
6580ab30494SMaxim Levitsky 	u8 command = MS_CMD_RESET;
6590ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
6600ab30494SMaxim Levitsky 	struct memstick_request *mrq = *out_mrq = &card->current_mrq;
6610ab30494SMaxim Levitsky 
6620ab30494SMaxim Levitsky 	if (mrq->error)
6630ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, mrq->error);
6640ab30494SMaxim Levitsky 
6650ab30494SMaxim Levitsky 	switch (msb->state) {
6660ab30494SMaxim Levitsky 	case MSB_RS_SEND:
6670ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
6680ab30494SMaxim Levitsky 		mrq->need_card_int = 0;
6690ab30494SMaxim Levitsky 		msb->state = MSB_RS_CONFIRM;
6700ab30494SMaxim Levitsky 		return 0;
6710ab30494SMaxim Levitsky 	case MSB_RS_CONFIRM:
6720ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, 0);
6730ab30494SMaxim Levitsky 	}
6740ab30494SMaxim Levitsky 	BUG();
6750ab30494SMaxim Levitsky }
6760ab30494SMaxim Levitsky 
6770ab30494SMaxim Levitsky /* This handler is used to do serial->parallel switch */
h_msb_parallel_switch(struct memstick_dev * card,struct memstick_request ** out_mrq)6780ab30494SMaxim Levitsky static int h_msb_parallel_switch(struct memstick_dev *card,
6790ab30494SMaxim Levitsky 					struct memstick_request **out_mrq)
6800ab30494SMaxim Levitsky {
6810ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
6820ab30494SMaxim Levitsky 	struct memstick_request *mrq = *out_mrq = &card->current_mrq;
6830ab30494SMaxim Levitsky 	struct memstick_host *host = card->host;
6840ab30494SMaxim Levitsky 
6850ab30494SMaxim Levitsky 	if (mrq->error) {
6860ab30494SMaxim Levitsky 		dbg("parallel_switch: error");
6870ab30494SMaxim Levitsky 		msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
6880ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, mrq->error);
6890ab30494SMaxim Levitsky 	}
6900ab30494SMaxim Levitsky 
6910ab30494SMaxim Levitsky 	switch (msb->state) {
6920ab30494SMaxim Levitsky 	case MSB_PS_SEND_SWITCH_COMMAND:
6930ab30494SMaxim Levitsky 		/* Set the parallel interface on memstick side */
6940ab30494SMaxim Levitsky 		msb->regs.param.system |= MEMSTICK_SYS_PAM;
6950ab30494SMaxim Levitsky 
6960ab30494SMaxim Levitsky 		if (!msb_write_regs(msb,
6970ab30494SMaxim Levitsky 			offsetof(struct ms_register, param),
6980ab30494SMaxim Levitsky 			1,
6990ab30494SMaxim Levitsky 			(unsigned char *)&msb->regs.param))
7000ab30494SMaxim Levitsky 			return 0;
7010ab30494SMaxim Levitsky 
7020ab30494SMaxim Levitsky 		msb->state = MSB_PS_SWICH_HOST;
7030ab30494SMaxim Levitsky 		return 0;
7040ab30494SMaxim Levitsky 
7050ab30494SMaxim Levitsky 	case MSB_PS_SWICH_HOST:
7060ab30494SMaxim Levitsky 		 /* Set parallel interface on our side + send a dummy request
7073ae61376SShubhankar Kuranagatti 		  * to see if card responds
7083ae61376SShubhankar Kuranagatti 		  */
7090ab30494SMaxim Levitsky 		host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
7100ab30494SMaxim Levitsky 		memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
7110ab30494SMaxim Levitsky 		msb->state = MSB_PS_CONFIRM;
7120ab30494SMaxim Levitsky 		return 0;
7130ab30494SMaxim Levitsky 
7140ab30494SMaxim Levitsky 	case MSB_PS_CONFIRM:
7150ab30494SMaxim Levitsky 		return msb_exit_state_machine(msb, 0);
7160ab30494SMaxim Levitsky 	}
7170ab30494SMaxim Levitsky 
7180ab30494SMaxim Levitsky 	BUG();
7190ab30494SMaxim Levitsky }
7200ab30494SMaxim Levitsky 
7210ab30494SMaxim Levitsky static int msb_switch_to_parallel(struct msb_data *msb);
7220ab30494SMaxim Levitsky 
7230ab30494SMaxim Levitsky /* Reset the card, to guard against hw errors beeing treated as bad blocks */
msb_reset(struct msb_data * msb,bool full)7240ab30494SMaxim Levitsky static int msb_reset(struct msb_data *msb, bool full)
7250ab30494SMaxim Levitsky {
7260ab30494SMaxim Levitsky 
7270ab30494SMaxim Levitsky 	bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM;
7280ab30494SMaxim Levitsky 	struct memstick_dev *card = msb->card;
7290ab30494SMaxim Levitsky 	struct memstick_host *host = card->host;
7300ab30494SMaxim Levitsky 	int error;
7310ab30494SMaxim Levitsky 
7320ab30494SMaxim Levitsky 	/* Reset the card */
7330ab30494SMaxim Levitsky 	msb->regs.param.system = MEMSTICK_SYS_BAMD;
7340ab30494SMaxim Levitsky 
7350ab30494SMaxim Levitsky 	if (full) {
7360ab30494SMaxim Levitsky 		error =  host->set_param(host,
7370ab30494SMaxim Levitsky 					MEMSTICK_POWER, MEMSTICK_POWER_OFF);
7380ab30494SMaxim Levitsky 		if (error)
7390ab30494SMaxim Levitsky 			goto out_error;
7400ab30494SMaxim Levitsky 
7410ab30494SMaxim Levitsky 		msb_invalidate_reg_window(msb);
7420ab30494SMaxim Levitsky 
7430ab30494SMaxim Levitsky 		error = host->set_param(host,
7440ab30494SMaxim Levitsky 					MEMSTICK_POWER, MEMSTICK_POWER_ON);
7450ab30494SMaxim Levitsky 		if (error)
7460ab30494SMaxim Levitsky 			goto out_error;
7470ab30494SMaxim Levitsky 
7480ab30494SMaxim Levitsky 		error = host->set_param(host,
7490ab30494SMaxim Levitsky 					MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
7500ab30494SMaxim Levitsky 		if (error) {
7510ab30494SMaxim Levitsky out_error:
7520ab30494SMaxim Levitsky 			dbg("Failed to reset the host controller");
7530ab30494SMaxim Levitsky 			msb->read_only = true;
7540ab30494SMaxim Levitsky 			return -EFAULT;
7550ab30494SMaxim Levitsky 		}
7560ab30494SMaxim Levitsky 	}
7570ab30494SMaxim Levitsky 
7580ab30494SMaxim Levitsky 	error = msb_run_state_machine(msb, h_msb_reset);
7590ab30494SMaxim Levitsky 	if (error) {
7600ab30494SMaxim Levitsky 		dbg("Failed to reset the card");
7610ab30494SMaxim Levitsky 		msb->read_only = true;
7620ab30494SMaxim Levitsky 		return -ENODEV;
7630ab30494SMaxim Levitsky 	}
7640ab30494SMaxim Levitsky 
7650ab30494SMaxim Levitsky 	/* Set parallel mode */
7660ab30494SMaxim Levitsky 	if (was_parallel)
7670ab30494SMaxim Levitsky 		msb_switch_to_parallel(msb);
7680ab30494SMaxim Levitsky 	return 0;
7690ab30494SMaxim Levitsky }
7700ab30494SMaxim Levitsky 
7710ab30494SMaxim Levitsky /* Attempts to switch interface to parallel mode */
msb_switch_to_parallel(struct msb_data * msb)7720ab30494SMaxim Levitsky static int msb_switch_to_parallel(struct msb_data *msb)
7730ab30494SMaxim Levitsky {
7740ab30494SMaxim Levitsky 	int error;
7750ab30494SMaxim Levitsky 
7760ab30494SMaxim Levitsky 	error = msb_run_state_machine(msb, h_msb_parallel_switch);
7770ab30494SMaxim Levitsky 	if (error) {
7780ab30494SMaxim Levitsky 		pr_err("Switch to parallel failed");
7790ab30494SMaxim Levitsky 		msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
7800ab30494SMaxim Levitsky 		msb_reset(msb, true);
7810ab30494SMaxim Levitsky 		return -EFAULT;
7820ab30494SMaxim Levitsky 	}
7830ab30494SMaxim Levitsky 
7840ab30494SMaxim Levitsky 	msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
7850ab30494SMaxim Levitsky 	return 0;
7860ab30494SMaxim Levitsky }
7870ab30494SMaxim Levitsky 
7880ab30494SMaxim Levitsky /* Changes overwrite flag on a page */
msb_set_overwrite_flag(struct msb_data * msb,u16 pba,u8 page,u8 flag)7890ab30494SMaxim Levitsky static int msb_set_overwrite_flag(struct msb_data *msb,
7900ab30494SMaxim Levitsky 						u16 pba, u8 page, u8 flag)
7910ab30494SMaxim Levitsky {
7920ab30494SMaxim Levitsky 	if (msb->read_only)
7930ab30494SMaxim Levitsky 		return -EROFS;
7940ab30494SMaxim Levitsky 
7950ab30494SMaxim Levitsky 	msb->regs.param.block_address = cpu_to_be16(pba);
7960ab30494SMaxim Levitsky 	msb->regs.param.page_address = page;
7970ab30494SMaxim Levitsky 	msb->regs.param.cp = MEMSTICK_CP_OVERWRITE;
7980ab30494SMaxim Levitsky 	msb->regs.extra_data.overwrite_flag = flag;
7990ab30494SMaxim Levitsky 	msb->command_value = MS_CMD_BLOCK_WRITE;
8000ab30494SMaxim Levitsky 	msb->command_need_oob = true;
8010ab30494SMaxim Levitsky 
8020ab30494SMaxim Levitsky 	dbg_verbose("changing overwrite flag to %02x for sector %d, page %d",
8030ab30494SMaxim Levitsky 							flag, pba, page);
8040ab30494SMaxim Levitsky 	return msb_run_state_machine(msb, h_msb_send_command);
8050ab30494SMaxim Levitsky }
8060ab30494SMaxim Levitsky 
msb_mark_bad(struct msb_data * msb,int pba)8070ab30494SMaxim Levitsky static int msb_mark_bad(struct msb_data *msb, int pba)
8080ab30494SMaxim Levitsky {
8090ab30494SMaxim Levitsky 	pr_notice("marking pba %d as bad", pba);
8100ab30494SMaxim Levitsky 	msb_reset(msb, true);
8110ab30494SMaxim Levitsky 	return msb_set_overwrite_flag(
8120ab30494SMaxim Levitsky 			msb, pba, 0, 0xFF & ~MEMSTICK_OVERWRITE_BKST);
8130ab30494SMaxim Levitsky }
8140ab30494SMaxim Levitsky 
msb_mark_page_bad(struct msb_data * msb,int pba,int page)8150ab30494SMaxim Levitsky static int msb_mark_page_bad(struct msb_data *msb, int pba, int page)
8160ab30494SMaxim Levitsky {
8170ab30494SMaxim Levitsky 	dbg("marking page %d of pba %d as bad", page, pba);
8180ab30494SMaxim Levitsky 	msb_reset(msb, true);
8190ab30494SMaxim Levitsky 	return msb_set_overwrite_flag(msb,
8200ab30494SMaxim Levitsky 		pba, page, ~MEMSTICK_OVERWRITE_PGST0);
8210ab30494SMaxim Levitsky }
8220ab30494SMaxim Levitsky 
8230ab30494SMaxim Levitsky /* Erases one physical block */
msb_erase_block(struct msb_data * msb,u16 pba)8240ab30494SMaxim Levitsky static int msb_erase_block(struct msb_data *msb, u16 pba)
8250ab30494SMaxim Levitsky {
8260ab30494SMaxim Levitsky 	int error, try;
8273ae61376SShubhankar Kuranagatti 
8280ab30494SMaxim Levitsky 	if (msb->read_only)
8290ab30494SMaxim Levitsky 		return -EROFS;
8300ab30494SMaxim Levitsky 
8310ab30494SMaxim Levitsky 	dbg_verbose("erasing pba %d", pba);
8320ab30494SMaxim Levitsky 
8330ab30494SMaxim Levitsky 	for (try = 1; try < 3; try++) {
8340ab30494SMaxim Levitsky 		msb->regs.param.block_address = cpu_to_be16(pba);
8350ab30494SMaxim Levitsky 		msb->regs.param.page_address = 0;
8360ab30494SMaxim Levitsky 		msb->regs.param.cp = MEMSTICK_CP_BLOCK;
8370ab30494SMaxim Levitsky 		msb->command_value = MS_CMD_BLOCK_ERASE;
8380ab30494SMaxim Levitsky 		msb->command_need_oob = false;
8390ab30494SMaxim Levitsky 
8400ab30494SMaxim Levitsky 
8410ab30494SMaxim Levitsky 		error = msb_run_state_machine(msb, h_msb_send_command);
8420ab30494SMaxim Levitsky 		if (!error || msb_reset(msb, true))
8430ab30494SMaxim Levitsky 			break;
8440ab30494SMaxim Levitsky 	}
8450ab30494SMaxim Levitsky 
8460ab30494SMaxim Levitsky 	if (error) {
8470ab30494SMaxim Levitsky 		pr_err("erase failed, marking pba %d as bad", pba);
8480ab30494SMaxim Levitsky 		msb_mark_bad(msb, pba);
8490ab30494SMaxim Levitsky 	}
8500ab30494SMaxim Levitsky 
8510ab30494SMaxim Levitsky 	dbg_verbose("erase success, marking pba %d as unused", pba);
8520ab30494SMaxim Levitsky 	msb_mark_block_unused(msb, pba);
8530ab30494SMaxim Levitsky 	__set_bit(pba, msb->erased_blocks_bitmap);
8540ab30494SMaxim Levitsky 	return error;
8550ab30494SMaxim Levitsky }
8560ab30494SMaxim Levitsky 
8570ab30494SMaxim Levitsky /* Reads one page from device */
msb_read_page(struct msb_data * msb,u16 pba,u8 page,struct ms_extra_data_register * extra,struct scatterlist * sg,int offset)8580ab30494SMaxim Levitsky static int msb_read_page(struct msb_data *msb,
8590ab30494SMaxim Levitsky 	u16 pba, u8 page, struct ms_extra_data_register *extra,
8600ab30494SMaxim Levitsky 					struct scatterlist *sg,  int offset)
8610ab30494SMaxim Levitsky {
8620ab30494SMaxim Levitsky 	int try, error;
8630ab30494SMaxim Levitsky 
8640ab30494SMaxim Levitsky 	if (pba == MS_BLOCK_INVALID) {
8650ab30494SMaxim Levitsky 		unsigned long flags;
8660ab30494SMaxim Levitsky 		struct sg_mapping_iter miter;
8670ab30494SMaxim Levitsky 		size_t len = msb->page_size;
8680ab30494SMaxim Levitsky 
8690ab30494SMaxim Levitsky 		dbg_verbose("read unmapped sector. returning 0xFF");
8700ab30494SMaxim Levitsky 
8710ab30494SMaxim Levitsky 		local_irq_save(flags);
8720ab30494SMaxim Levitsky 		sg_miter_start(&miter, sg, sg_nents(sg),
8730ab30494SMaxim Levitsky 				SG_MITER_ATOMIC | SG_MITER_TO_SG);
8740ab30494SMaxim Levitsky 
8750ab30494SMaxim Levitsky 		while (sg_miter_next(&miter) && len > 0) {
8760ab30494SMaxim Levitsky 
8770ab30494SMaxim Levitsky 			int chunklen;
8780ab30494SMaxim Levitsky 
8790ab30494SMaxim Levitsky 			if (offset && offset >= miter.length) {
8800ab30494SMaxim Levitsky 				offset -= miter.length;
8810ab30494SMaxim Levitsky 				continue;
8820ab30494SMaxim Levitsky 			}
8830ab30494SMaxim Levitsky 
8840ab30494SMaxim Levitsky 			chunklen = min(miter.length - offset, len);
8850ab30494SMaxim Levitsky 			memset(miter.addr + offset, 0xFF, chunklen);
8860ab30494SMaxim Levitsky 			len -= chunklen;
8870ab30494SMaxim Levitsky 			offset = 0;
8880ab30494SMaxim Levitsky 		}
8890ab30494SMaxim Levitsky 
8900ab30494SMaxim Levitsky 		sg_miter_stop(&miter);
8910ab30494SMaxim Levitsky 		local_irq_restore(flags);
8920ab30494SMaxim Levitsky 
8930ab30494SMaxim Levitsky 		if (offset)
8940ab30494SMaxim Levitsky 			return -EFAULT;
8950ab30494SMaxim Levitsky 
8960ab30494SMaxim Levitsky 		if (extra)
8970ab30494SMaxim Levitsky 			memset(extra, 0xFF, sizeof(*extra));
8980ab30494SMaxim Levitsky 		return 0;
8990ab30494SMaxim Levitsky 	}
9000ab30494SMaxim Levitsky 
9010ab30494SMaxim Levitsky 	if (pba >= msb->block_count) {
9020ab30494SMaxim Levitsky 		pr_err("BUG: attempt to read beyond the end of the card at pba %d", pba);
9030ab30494SMaxim Levitsky 		return -EINVAL;
9040ab30494SMaxim Levitsky 	}
9050ab30494SMaxim Levitsky 
9060ab30494SMaxim Levitsky 	for (try = 1; try < 3; try++) {
9070ab30494SMaxim Levitsky 		msb->regs.param.block_address = cpu_to_be16(pba);
9080ab30494SMaxim Levitsky 		msb->regs.param.page_address = page;
9090ab30494SMaxim Levitsky 		msb->regs.param.cp = MEMSTICK_CP_PAGE;
9100ab30494SMaxim Levitsky 
9110ab30494SMaxim Levitsky 		msb->current_sg = sg;
9120ab30494SMaxim Levitsky 		msb->current_sg_offset = offset;
9130ab30494SMaxim Levitsky 		error = msb_run_state_machine(msb, h_msb_read_page);
9140ab30494SMaxim Levitsky 
9150ab30494SMaxim Levitsky 
9160ab30494SMaxim Levitsky 		if (error == -EUCLEAN) {
9170ab30494SMaxim Levitsky 			pr_notice("correctable error on pba %d, page %d",
9180ab30494SMaxim Levitsky 				pba, page);
9190ab30494SMaxim Levitsky 			error = 0;
9200ab30494SMaxim Levitsky 		}
9210ab30494SMaxim Levitsky 
9220ab30494SMaxim Levitsky 		if (!error && extra)
9230ab30494SMaxim Levitsky 			*extra = msb->regs.extra_data;
9240ab30494SMaxim Levitsky 
9250ab30494SMaxim Levitsky 		if (!error || msb_reset(msb, true))
9260ab30494SMaxim Levitsky 			break;
9270ab30494SMaxim Levitsky 
9280ab30494SMaxim Levitsky 	}
9290ab30494SMaxim Levitsky 
9300ab30494SMaxim Levitsky 	/* Mark bad pages */
9310ab30494SMaxim Levitsky 	if (error == -EBADMSG) {
9320ab30494SMaxim Levitsky 		pr_err("uncorrectable error on read of pba %d, page %d",
9330ab30494SMaxim Levitsky 			pba, page);
9340ab30494SMaxim Levitsky 
9350ab30494SMaxim Levitsky 		if (msb->regs.extra_data.overwrite_flag &
9360ab30494SMaxim Levitsky 					MEMSTICK_OVERWRITE_PGST0)
9370ab30494SMaxim Levitsky 			msb_mark_page_bad(msb, pba, page);
9380ab30494SMaxim Levitsky 		return -EBADMSG;
9390ab30494SMaxim Levitsky 	}
9400ab30494SMaxim Levitsky 
9410ab30494SMaxim Levitsky 	if (error)
9420ab30494SMaxim Levitsky 		pr_err("read of pba %d, page %d failed with error %d",
9430ab30494SMaxim Levitsky 			pba, page, error);
9440ab30494SMaxim Levitsky 	return error;
9450ab30494SMaxim Levitsky }
9460ab30494SMaxim Levitsky 
9470ab30494SMaxim Levitsky /* Reads oob of page only */
msb_read_oob(struct msb_data * msb,u16 pba,u16 page,struct ms_extra_data_register * extra)9480ab30494SMaxim Levitsky static int msb_read_oob(struct msb_data *msb, u16 pba, u16 page,
9490ab30494SMaxim Levitsky 	struct ms_extra_data_register *extra)
9500ab30494SMaxim Levitsky {
9510ab30494SMaxim Levitsky 	int error;
9520ab30494SMaxim Levitsky 
9530ab30494SMaxim Levitsky 	BUG_ON(!extra);
9540ab30494SMaxim Levitsky 	msb->regs.param.block_address = cpu_to_be16(pba);
9550ab30494SMaxim Levitsky 	msb->regs.param.page_address = page;
9560ab30494SMaxim Levitsky 	msb->regs.param.cp = MEMSTICK_CP_EXTRA;
9570ab30494SMaxim Levitsky 
9580ab30494SMaxim Levitsky 	if (pba > msb->block_count) {
9590ab30494SMaxim Levitsky 		pr_err("BUG: attempt to read beyond the end of card at pba %d", pba);
9600ab30494SMaxim Levitsky 		return -EINVAL;
9610ab30494SMaxim Levitsky 	}
9620ab30494SMaxim Levitsky 
9630ab30494SMaxim Levitsky 	error = msb_run_state_machine(msb, h_msb_read_page);
9640ab30494SMaxim Levitsky 	*extra = msb->regs.extra_data;
9650ab30494SMaxim Levitsky 
9660ab30494SMaxim Levitsky 	if (error == -EUCLEAN) {
9670ab30494SMaxim Levitsky 		pr_notice("correctable error on pba %d, page %d",
9680ab30494SMaxim Levitsky 			pba, page);
9690ab30494SMaxim Levitsky 		return 0;
9700ab30494SMaxim Levitsky 	}
9710ab30494SMaxim Levitsky 
9720ab30494SMaxim Levitsky 	return error;
9730ab30494SMaxim Levitsky }
9740ab30494SMaxim Levitsky 
9750ab30494SMaxim Levitsky /* Reads a block and compares it with data contained in scatterlist orig_sg */
msb_verify_block(struct msb_data * msb,u16 pba,struct scatterlist * orig_sg,int offset)9760ab30494SMaxim Levitsky static int msb_verify_block(struct msb_data *msb, u16 pba,
9770ab30494SMaxim Levitsky 				struct scatterlist *orig_sg,  int offset)
9780ab30494SMaxim Levitsky {
9790ab30494SMaxim Levitsky 	struct scatterlist sg;
9800ab30494SMaxim Levitsky 	int page = 0, error;
9810ab30494SMaxim Levitsky 
9820ab30494SMaxim Levitsky 	sg_init_one(&sg, msb->block_buffer, msb->block_size);
9830ab30494SMaxim Levitsky 
9840ab30494SMaxim Levitsky 	while (page < msb->pages_in_block) {
9850ab30494SMaxim Levitsky 
9860ab30494SMaxim Levitsky 		error = msb_read_page(msb, pba, page,
9870ab30494SMaxim Levitsky 				NULL, &sg, page * msb->page_size);
9880ab30494SMaxim Levitsky 		if (error)
9890ab30494SMaxim Levitsky 			return error;
9900ab30494SMaxim Levitsky 		page++;
9910ab30494SMaxim Levitsky 	}
9920ab30494SMaxim Levitsky 
9930ab30494SMaxim Levitsky 	if (msb_sg_compare_to_buffer(orig_sg, offset,
9940ab30494SMaxim Levitsky 				msb->block_buffer, msb->block_size))
9950ab30494SMaxim Levitsky 		return -EIO;
9960ab30494SMaxim Levitsky 	return 0;
9970ab30494SMaxim Levitsky }
9980ab30494SMaxim Levitsky 
9990ab30494SMaxim Levitsky /* Writes exectly one block + oob */
msb_write_block(struct msb_data * msb,u16 pba,u32 lba,struct scatterlist * sg,int offset)10000ab30494SMaxim Levitsky static int msb_write_block(struct msb_data *msb,
10010ab30494SMaxim Levitsky 			u16 pba, u32 lba, struct scatterlist *sg, int offset)
10020ab30494SMaxim Levitsky {
10030ab30494SMaxim Levitsky 	int error, current_try = 1;
10043ae61376SShubhankar Kuranagatti 
10050ab30494SMaxim Levitsky 	BUG_ON(sg->length < msb->page_size);
10060ab30494SMaxim Levitsky 
10070ab30494SMaxim Levitsky 	if (msb->read_only)
10080ab30494SMaxim Levitsky 		return -EROFS;
10090ab30494SMaxim Levitsky 
10100ab30494SMaxim Levitsky 	if (pba == MS_BLOCK_INVALID) {
10110ab30494SMaxim Levitsky 		pr_err(
10120ab30494SMaxim Levitsky 			"BUG: write: attempt to write MS_BLOCK_INVALID block");
10130ab30494SMaxim Levitsky 		return -EINVAL;
10140ab30494SMaxim Levitsky 	}
10150ab30494SMaxim Levitsky 
10160ab30494SMaxim Levitsky 	if (pba >= msb->block_count || lba >= msb->logical_block_count) {
10170ab30494SMaxim Levitsky 		pr_err(
10180ab30494SMaxim Levitsky 		"BUG: write: attempt to write beyond the end of device");
10190ab30494SMaxim Levitsky 		return -EINVAL;
10200ab30494SMaxim Levitsky 	}
10210ab30494SMaxim Levitsky 
10220ab30494SMaxim Levitsky 	if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
10230ab30494SMaxim Levitsky 		pr_err("BUG: write: lba zone mismatch");
10240ab30494SMaxim Levitsky 		return -EINVAL;
10250ab30494SMaxim Levitsky 	}
10260ab30494SMaxim Levitsky 
10270ab30494SMaxim Levitsky 	if (pba == msb->boot_block_locations[0] ||
10280ab30494SMaxim Levitsky 		pba == msb->boot_block_locations[1]) {
10290ab30494SMaxim Levitsky 		pr_err("BUG: write: attempt to write to boot blocks!");
10300ab30494SMaxim Levitsky 		return -EINVAL;
10310ab30494SMaxim Levitsky 	}
10320ab30494SMaxim Levitsky 
10330ab30494SMaxim Levitsky 	while (1) {
10340ab30494SMaxim Levitsky 
10350ab30494SMaxim Levitsky 		if (msb->read_only)
10360ab30494SMaxim Levitsky 			return -EROFS;
10370ab30494SMaxim Levitsky 
10380ab30494SMaxim Levitsky 		msb->regs.param.cp = MEMSTICK_CP_BLOCK;
10390ab30494SMaxim Levitsky 		msb->regs.param.page_address = 0;
10400ab30494SMaxim Levitsky 		msb->regs.param.block_address = cpu_to_be16(pba);
10410ab30494SMaxim Levitsky 
10420ab30494SMaxim Levitsky 		msb->regs.extra_data.management_flag = 0xFF;
10430ab30494SMaxim Levitsky 		msb->regs.extra_data.overwrite_flag = 0xF8;
10440ab30494SMaxim Levitsky 		msb->regs.extra_data.logical_address = cpu_to_be16(lba);
10450ab30494SMaxim Levitsky 
10460ab30494SMaxim Levitsky 		msb->current_sg = sg;
10470ab30494SMaxim Levitsky 		msb->current_sg_offset = offset;
10480ab30494SMaxim Levitsky 		msb->current_page = 0;
10490ab30494SMaxim Levitsky 
10500ab30494SMaxim Levitsky 		error = msb_run_state_machine(msb, h_msb_write_block);
10510ab30494SMaxim Levitsky 
10520ab30494SMaxim Levitsky 		/* Sector we just wrote to is assumed erased since its pba
10533ae61376SShubhankar Kuranagatti 		 * was erased. If it wasn't erased, write will succeed
10543ae61376SShubhankar Kuranagatti 		 * and will just clear the bits that were set in the block
10553ae61376SShubhankar Kuranagatti 		 * thus test that what we have written,
10563ae61376SShubhankar Kuranagatti 		 * matches what we expect.
10573ae61376SShubhankar Kuranagatti 		 * We do trust the blocks that we erased
10583ae61376SShubhankar Kuranagatti 		 */
10590ab30494SMaxim Levitsky 		if (!error && (verify_writes ||
10600ab30494SMaxim Levitsky 				!test_bit(pba, msb->erased_blocks_bitmap)))
10610ab30494SMaxim Levitsky 			error = msb_verify_block(msb, pba, sg, offset);
10620ab30494SMaxim Levitsky 
10630ab30494SMaxim Levitsky 		if (!error)
10640ab30494SMaxim Levitsky 			break;
10650ab30494SMaxim Levitsky 
10660ab30494SMaxim Levitsky 		if (current_try > 1 || msb_reset(msb, true))
10670ab30494SMaxim Levitsky 			break;
10680ab30494SMaxim Levitsky 
10690ab30494SMaxim Levitsky 		pr_err("write failed, trying to erase the pba %d", pba);
10700ab30494SMaxim Levitsky 		error = msb_erase_block(msb, pba);
10710ab30494SMaxim Levitsky 		if (error)
10720ab30494SMaxim Levitsky 			break;
10730ab30494SMaxim Levitsky 
10740ab30494SMaxim Levitsky 		current_try++;
10750ab30494SMaxim Levitsky 	}
10760ab30494SMaxim Levitsky 	return error;
10770ab30494SMaxim Levitsky }
10780ab30494SMaxim Levitsky 
10790ab30494SMaxim Levitsky /* Finds a free block for write replacement */
msb_get_free_block(struct msb_data * msb,int zone)10800ab30494SMaxim Levitsky static u16 msb_get_free_block(struct msb_data *msb, int zone)
10810ab30494SMaxim Levitsky {
10820ab30494SMaxim Levitsky 	u16 pos;
10830ab30494SMaxim Levitsky 	int pba = zone * MS_BLOCKS_IN_ZONE;
10840ab30494SMaxim Levitsky 	int i;
10850ab30494SMaxim Levitsky 
10860ab30494SMaxim Levitsky 	get_random_bytes(&pos, sizeof(pos));
10870ab30494SMaxim Levitsky 
10880ab30494SMaxim Levitsky 	if (!msb->free_block_count[zone]) {
10890ab30494SMaxim Levitsky 		pr_err("NO free blocks in the zone %d, to use for a write, (media is WORN out) switching to RO mode", zone);
10900ab30494SMaxim Levitsky 		msb->read_only = true;
10910ab30494SMaxim Levitsky 		return MS_BLOCK_INVALID;
10920ab30494SMaxim Levitsky 	}
10930ab30494SMaxim Levitsky 
10940ab30494SMaxim Levitsky 	pos %= msb->free_block_count[zone];
10950ab30494SMaxim Levitsky 
109631cf7211SColin Ian King 	dbg_verbose("have %d choices for a free block, selected randomly: %d",
10970ab30494SMaxim Levitsky 		msb->free_block_count[zone], pos);
10980ab30494SMaxim Levitsky 
10990ab30494SMaxim Levitsky 	pba = find_next_zero_bit(msb->used_blocks_bitmap,
11000ab30494SMaxim Levitsky 							msb->block_count, pba);
11010ab30494SMaxim Levitsky 	for (i = 0; i < pos; ++i)
11020ab30494SMaxim Levitsky 		pba = find_next_zero_bit(msb->used_blocks_bitmap,
11030ab30494SMaxim Levitsky 						msb->block_count, pba + 1);
11040ab30494SMaxim Levitsky 
11050ab30494SMaxim Levitsky 	dbg_verbose("result of the free blocks scan: pba %d", pba);
11060ab30494SMaxim Levitsky 
11070ab30494SMaxim Levitsky 	if (pba == msb->block_count || (msb_get_zone_from_pba(pba)) != zone) {
110860885bfbSColin Ian King 		pr_err("BUG: can't get a free block");
11090ab30494SMaxim Levitsky 		msb->read_only = true;
11100ab30494SMaxim Levitsky 		return MS_BLOCK_INVALID;
11110ab30494SMaxim Levitsky 	}
11120ab30494SMaxim Levitsky 
11130ab30494SMaxim Levitsky 	msb_mark_block_used(msb, pba);
11140ab30494SMaxim Levitsky 	return pba;
11150ab30494SMaxim Levitsky }
11160ab30494SMaxim Levitsky 
msb_update_block(struct msb_data * msb,u16 lba,struct scatterlist * sg,int offset)11170ab30494SMaxim Levitsky static int msb_update_block(struct msb_data *msb, u16 lba,
11180ab30494SMaxim Levitsky 	struct scatterlist *sg, int offset)
11190ab30494SMaxim Levitsky {
11200ab30494SMaxim Levitsky 	u16 pba, new_pba;
11210ab30494SMaxim Levitsky 	int error, try;
11220ab30494SMaxim Levitsky 
11230ab30494SMaxim Levitsky 	pba = msb->lba_to_pba_table[lba];
11240ab30494SMaxim Levitsky 	dbg_verbose("start of a block update at lba  %d, pba %d", lba, pba);
11250ab30494SMaxim Levitsky 
11260ab30494SMaxim Levitsky 	if (pba != MS_BLOCK_INVALID) {
11270ab30494SMaxim Levitsky 		dbg_verbose("setting the update flag on the block");
11280ab30494SMaxim Levitsky 		msb_set_overwrite_flag(msb, pba, 0,
11290ab30494SMaxim Levitsky 				0xFF & ~MEMSTICK_OVERWRITE_UDST);
11300ab30494SMaxim Levitsky 	}
11310ab30494SMaxim Levitsky 
11320ab30494SMaxim Levitsky 	for (try = 0; try < 3; try++) {
11330ab30494SMaxim Levitsky 		new_pba = msb_get_free_block(msb,
11340ab30494SMaxim Levitsky 			msb_get_zone_from_lba(lba));
11350ab30494SMaxim Levitsky 
11360ab30494SMaxim Levitsky 		if (new_pba == MS_BLOCK_INVALID) {
11370ab30494SMaxim Levitsky 			error = -EIO;
11380ab30494SMaxim Levitsky 			goto out;
11390ab30494SMaxim Levitsky 		}
11400ab30494SMaxim Levitsky 
11410ab30494SMaxim Levitsky 		dbg_verbose("block update: writing updated block to the pba %d",
11420ab30494SMaxim Levitsky 								new_pba);
11430ab30494SMaxim Levitsky 		error = msb_write_block(msb, new_pba, lba, sg, offset);
11440ab30494SMaxim Levitsky 		if (error == -EBADMSG) {
11450ab30494SMaxim Levitsky 			msb_mark_bad(msb, new_pba);
11460ab30494SMaxim Levitsky 			continue;
11470ab30494SMaxim Levitsky 		}
11480ab30494SMaxim Levitsky 
11490ab30494SMaxim Levitsky 		if (error)
11500ab30494SMaxim Levitsky 			goto out;
11510ab30494SMaxim Levitsky 
11520ab30494SMaxim Levitsky 		dbg_verbose("block update: erasing the old block");
11530ab30494SMaxim Levitsky 		msb_erase_block(msb, pba);
11540ab30494SMaxim Levitsky 		msb->lba_to_pba_table[lba] = new_pba;
11550ab30494SMaxim Levitsky 		return 0;
11560ab30494SMaxim Levitsky 	}
11570ab30494SMaxim Levitsky out:
11580ab30494SMaxim Levitsky 	if (error) {
11590ab30494SMaxim Levitsky 		pr_err("block update error after %d tries,  switching to r/o mode", try);
11600ab30494SMaxim Levitsky 		msb->read_only = true;
11610ab30494SMaxim Levitsky 	}
11620ab30494SMaxim Levitsky 	return error;
11630ab30494SMaxim Levitsky }
11640ab30494SMaxim Levitsky 
11650ab30494SMaxim Levitsky /* Converts endiannes in the boot block for easy use */
msb_fix_boot_page_endianness(struct ms_boot_page * p)11660ab30494SMaxim Levitsky static void msb_fix_boot_page_endianness(struct ms_boot_page *p)
11670ab30494SMaxim Levitsky {
11680ab30494SMaxim Levitsky 	p->header.block_id = be16_to_cpu(p->header.block_id);
11690ab30494SMaxim Levitsky 	p->header.format_reserved = be16_to_cpu(p->header.format_reserved);
11700ab30494SMaxim Levitsky 	p->entry.disabled_block.start_addr
11710ab30494SMaxim Levitsky 		= be32_to_cpu(p->entry.disabled_block.start_addr);
11720ab30494SMaxim Levitsky 	p->entry.disabled_block.data_size
11730ab30494SMaxim Levitsky 		= be32_to_cpu(p->entry.disabled_block.data_size);
11740ab30494SMaxim Levitsky 	p->entry.cis_idi.start_addr
11750ab30494SMaxim Levitsky 		= be32_to_cpu(p->entry.cis_idi.start_addr);
11760ab30494SMaxim Levitsky 	p->entry.cis_idi.data_size
11770ab30494SMaxim Levitsky 		= be32_to_cpu(p->entry.cis_idi.data_size);
11780ab30494SMaxim Levitsky 	p->attr.block_size = be16_to_cpu(p->attr.block_size);
11790ab30494SMaxim Levitsky 	p->attr.number_of_blocks = be16_to_cpu(p->attr.number_of_blocks);
11800ab30494SMaxim Levitsky 	p->attr.number_of_effective_blocks
11810ab30494SMaxim Levitsky 		= be16_to_cpu(p->attr.number_of_effective_blocks);
11820ab30494SMaxim Levitsky 	p->attr.page_size = be16_to_cpu(p->attr.page_size);
11830ab30494SMaxim Levitsky 	p->attr.memory_manufacturer_code
11840ab30494SMaxim Levitsky 		= be16_to_cpu(p->attr.memory_manufacturer_code);
11850ab30494SMaxim Levitsky 	p->attr.memory_device_code = be16_to_cpu(p->attr.memory_device_code);
11860ab30494SMaxim Levitsky 	p->attr.implemented_capacity
11870ab30494SMaxim Levitsky 		= be16_to_cpu(p->attr.implemented_capacity);
11880ab30494SMaxim Levitsky 	p->attr.controller_number = be16_to_cpu(p->attr.controller_number);
11890ab30494SMaxim Levitsky 	p->attr.controller_function = be16_to_cpu(p->attr.controller_function);
11900ab30494SMaxim Levitsky }
11910ab30494SMaxim Levitsky 
msb_read_boot_blocks(struct msb_data * msb)11920ab30494SMaxim Levitsky static int msb_read_boot_blocks(struct msb_data *msb)
11930ab30494SMaxim Levitsky {
11940ab30494SMaxim Levitsky 	int pba = 0;
11950ab30494SMaxim Levitsky 	struct scatterlist sg;
11960ab30494SMaxim Levitsky 	struct ms_extra_data_register extra;
11970ab30494SMaxim Levitsky 	struct ms_boot_page *page;
11980ab30494SMaxim Levitsky 
11990ab30494SMaxim Levitsky 	msb->boot_block_locations[0] = MS_BLOCK_INVALID;
12000ab30494SMaxim Levitsky 	msb->boot_block_locations[1] = MS_BLOCK_INVALID;
12010ab30494SMaxim Levitsky 	msb->boot_block_count = 0;
12020ab30494SMaxim Levitsky 
12030ab30494SMaxim Levitsky 	dbg_verbose("Start of a scan for the boot blocks");
12040ab30494SMaxim Levitsky 
12050ab30494SMaxim Levitsky 	if (!msb->boot_page) {
12066da2ec56SKees Cook 		page = kmalloc_array(2, sizeof(struct ms_boot_page),
12076da2ec56SKees Cook 				     GFP_KERNEL);
12080ab30494SMaxim Levitsky 		if (!page)
12090ab30494SMaxim Levitsky 			return -ENOMEM;
12100ab30494SMaxim Levitsky 
12110ab30494SMaxim Levitsky 		msb->boot_page = page;
12120ab30494SMaxim Levitsky 	} else
12130ab30494SMaxim Levitsky 		page = msb->boot_page;
12140ab30494SMaxim Levitsky 
12150ab30494SMaxim Levitsky 	msb->block_count = MS_BLOCK_MAX_BOOT_ADDR;
12160ab30494SMaxim Levitsky 
12170ab30494SMaxim Levitsky 	for (pba = 0; pba < MS_BLOCK_MAX_BOOT_ADDR; pba++) {
12180ab30494SMaxim Levitsky 
12190ab30494SMaxim Levitsky 		sg_init_one(&sg, page, sizeof(*page));
12200ab30494SMaxim Levitsky 		if (msb_read_page(msb, pba, 0, &extra, &sg, 0)) {
12210ab30494SMaxim Levitsky 			dbg("boot scan: can't read pba %d", pba);
12220ab30494SMaxim Levitsky 			continue;
12230ab30494SMaxim Levitsky 		}
12240ab30494SMaxim Levitsky 
12250ab30494SMaxim Levitsky 		if (extra.management_flag & MEMSTICK_MANAGEMENT_SYSFLG) {
12266951c585SColin Ian King 			dbg("management flag doesn't indicate boot block %d",
12270ab30494SMaxim Levitsky 									pba);
12280ab30494SMaxim Levitsky 			continue;
12290ab30494SMaxim Levitsky 		}
12300ab30494SMaxim Levitsky 
12310ab30494SMaxim Levitsky 		if (be16_to_cpu(page->header.block_id) != MS_BLOCK_BOOT_ID) {
1232309de450SColin Ian King 			dbg("the pba at %d doesn't contain boot block ID", pba);
12330ab30494SMaxim Levitsky 			continue;
12340ab30494SMaxim Levitsky 		}
12350ab30494SMaxim Levitsky 
12360ab30494SMaxim Levitsky 		msb_fix_boot_page_endianness(page);
12370ab30494SMaxim Levitsky 		msb->boot_block_locations[msb->boot_block_count] = pba;
12380ab30494SMaxim Levitsky 
12390ab30494SMaxim Levitsky 		page++;
12400ab30494SMaxim Levitsky 		msb->boot_block_count++;
12410ab30494SMaxim Levitsky 
12420ab30494SMaxim Levitsky 		if (msb->boot_block_count == 2)
12430ab30494SMaxim Levitsky 			break;
12440ab30494SMaxim Levitsky 	}
12450ab30494SMaxim Levitsky 
12460ab30494SMaxim Levitsky 	if (!msb->boot_block_count) {
12470ab30494SMaxim Levitsky 		pr_err("media doesn't contain master page, aborting");
12480ab30494SMaxim Levitsky 		return -EIO;
12490ab30494SMaxim Levitsky 	}
12500ab30494SMaxim Levitsky 
12510ab30494SMaxim Levitsky 	dbg_verbose("End of scan for boot blocks");
12520ab30494SMaxim Levitsky 	return 0;
12530ab30494SMaxim Levitsky }
12540ab30494SMaxim Levitsky 
msb_read_bad_block_table(struct msb_data * msb,int block_nr)12550ab30494SMaxim Levitsky static int msb_read_bad_block_table(struct msb_data *msb, int block_nr)
12560ab30494SMaxim Levitsky {
12570ab30494SMaxim Levitsky 	struct ms_boot_page *boot_block;
12580ab30494SMaxim Levitsky 	struct scatterlist sg;
12590ab30494SMaxim Levitsky 	u16 *buffer = NULL;
12600ab30494SMaxim Levitsky 	int offset = 0;
12610ab30494SMaxim Levitsky 	int i, error = 0;
12620ab30494SMaxim Levitsky 	int data_size, data_offset, page, page_offset, size_to_read;
12630ab30494SMaxim Levitsky 	u16 pba;
12640ab30494SMaxim Levitsky 
12650ab30494SMaxim Levitsky 	BUG_ON(block_nr > 1);
12660ab30494SMaxim Levitsky 	boot_block = &msb->boot_page[block_nr];
12670ab30494SMaxim Levitsky 	pba = msb->boot_block_locations[block_nr];
12680ab30494SMaxim Levitsky 
12690ab30494SMaxim Levitsky 	if (msb->boot_block_locations[block_nr] == MS_BLOCK_INVALID)
12700ab30494SMaxim Levitsky 		return -EINVAL;
12710ab30494SMaxim Levitsky 
12720ab30494SMaxim Levitsky 	data_size = boot_block->entry.disabled_block.data_size;
12730ab30494SMaxim Levitsky 	data_offset = sizeof(struct ms_boot_page) +
12740ab30494SMaxim Levitsky 			boot_block->entry.disabled_block.start_addr;
12750ab30494SMaxim Levitsky 	if (!data_size)
12760ab30494SMaxim Levitsky 		return 0;
12770ab30494SMaxim Levitsky 
12780ab30494SMaxim Levitsky 	page = data_offset / msb->page_size;
12790ab30494SMaxim Levitsky 	page_offset = data_offset % msb->page_size;
12800ab30494SMaxim Levitsky 	size_to_read =
12810ab30494SMaxim Levitsky 		DIV_ROUND_UP(data_size + page_offset, msb->page_size) *
12820ab30494SMaxim Levitsky 			msb->page_size;
12830ab30494SMaxim Levitsky 
12840ab30494SMaxim Levitsky 	dbg("reading bad block of boot block at pba %d, offset %d len %d",
12850ab30494SMaxim Levitsky 		pba, data_offset, data_size);
12860ab30494SMaxim Levitsky 
12870ab30494SMaxim Levitsky 	buffer = kzalloc(size_to_read, GFP_KERNEL);
12880ab30494SMaxim Levitsky 	if (!buffer)
12890ab30494SMaxim Levitsky 		return -ENOMEM;
12900ab30494SMaxim Levitsky 
12910ab30494SMaxim Levitsky 	/* Read the buffer */
12920ab30494SMaxim Levitsky 	sg_init_one(&sg, buffer, size_to_read);
12930ab30494SMaxim Levitsky 
12940ab30494SMaxim Levitsky 	while (offset < size_to_read) {
12950ab30494SMaxim Levitsky 		error = msb_read_page(msb, pba, page, NULL, &sg, offset);
12960ab30494SMaxim Levitsky 		if (error)
12970ab30494SMaxim Levitsky 			goto out;
12980ab30494SMaxim Levitsky 
12990ab30494SMaxim Levitsky 		page++;
13000ab30494SMaxim Levitsky 		offset += msb->page_size;
13010ab30494SMaxim Levitsky 
13020ab30494SMaxim Levitsky 		if (page == msb->pages_in_block) {
13030ab30494SMaxim Levitsky 			pr_err(
13040ab30494SMaxim Levitsky 			"bad block table extends beyond the boot block");
13050ab30494SMaxim Levitsky 			break;
13060ab30494SMaxim Levitsky 		}
13070ab30494SMaxim Levitsky 	}
13080ab30494SMaxim Levitsky 
13090ab30494SMaxim Levitsky 	/* Process the bad block table */
13100ab30494SMaxim Levitsky 	for (i = page_offset; i < data_size / sizeof(u16); i++) {
13110ab30494SMaxim Levitsky 
13120ab30494SMaxim Levitsky 		u16 bad_block = be16_to_cpu(buffer[i]);
13130ab30494SMaxim Levitsky 
13140ab30494SMaxim Levitsky 		if (bad_block >= msb->block_count) {
13150ab30494SMaxim Levitsky 			dbg("bad block table contains invalid block %d",
13160ab30494SMaxim Levitsky 								bad_block);
13170ab30494SMaxim Levitsky 			continue;
13180ab30494SMaxim Levitsky 		}
13190ab30494SMaxim Levitsky 
13200ab30494SMaxim Levitsky 		if (test_bit(bad_block, msb->used_blocks_bitmap))  {
13210ab30494SMaxim Levitsky 			dbg("duplicate bad block %d in the table",
13220ab30494SMaxim Levitsky 				bad_block);
13230ab30494SMaxim Levitsky 			continue;
13240ab30494SMaxim Levitsky 		}
13250ab30494SMaxim Levitsky 
13260ab30494SMaxim Levitsky 		dbg("block %d is marked as factory bad", bad_block);
13270ab30494SMaxim Levitsky 		msb_mark_block_used(msb, bad_block);
13280ab30494SMaxim Levitsky 	}
13290ab30494SMaxim Levitsky out:
13300ab30494SMaxim Levitsky 	kfree(buffer);
13310ab30494SMaxim Levitsky 	return error;
13320ab30494SMaxim Levitsky }
13330ab30494SMaxim Levitsky 
msb_ftl_initialize(struct msb_data * msb)13340ab30494SMaxim Levitsky static int msb_ftl_initialize(struct msb_data *msb)
13350ab30494SMaxim Levitsky {
13360ab30494SMaxim Levitsky 	int i;
13370ab30494SMaxim Levitsky 
13380ab30494SMaxim Levitsky 	if (msb->ftl_initialized)
13390ab30494SMaxim Levitsky 		return 0;
13400ab30494SMaxim Levitsky 
13410ab30494SMaxim Levitsky 	msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
13420ab30494SMaxim Levitsky 	msb->logical_block_count = msb->zone_count * 496 - 2;
13430ab30494SMaxim Levitsky 
13442e531bc3SChristophe JAILLET 	msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
13452e531bc3SChristophe JAILLET 	msb->erased_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
13460ab30494SMaxim Levitsky 	msb->lba_to_pba_table =
13476da2ec56SKees Cook 		kmalloc_array(msb->logical_block_count, sizeof(u16),
13486da2ec56SKees Cook 			      GFP_KERNEL);
13490ab30494SMaxim Levitsky 
13500ab30494SMaxim Levitsky 	if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
13510ab30494SMaxim Levitsky 						!msb->erased_blocks_bitmap) {
13522e531bc3SChristophe JAILLET 		bitmap_free(msb->used_blocks_bitmap);
13532e531bc3SChristophe JAILLET 		bitmap_free(msb->erased_blocks_bitmap);
13540ab30494SMaxim Levitsky 		kfree(msb->lba_to_pba_table);
13550ab30494SMaxim Levitsky 		return -ENOMEM;
13560ab30494SMaxim Levitsky 	}
13570ab30494SMaxim Levitsky 
13580ab30494SMaxim Levitsky 	for (i = 0; i < msb->zone_count; i++)
13590ab30494SMaxim Levitsky 		msb->free_block_count[i] = MS_BLOCKS_IN_ZONE;
13600ab30494SMaxim Levitsky 
13610ab30494SMaxim Levitsky 	memset(msb->lba_to_pba_table, MS_BLOCK_INVALID,
13620ab30494SMaxim Levitsky 			msb->logical_block_count * sizeof(u16));
13630ab30494SMaxim Levitsky 
13640ab30494SMaxim Levitsky 	dbg("initial FTL tables created. Zone count = %d, Logical block count = %d",
13650ab30494SMaxim Levitsky 		msb->zone_count, msb->logical_block_count);
13660ab30494SMaxim Levitsky 
13670ab30494SMaxim Levitsky 	msb->ftl_initialized = true;
13680ab30494SMaxim Levitsky 	return 0;
13690ab30494SMaxim Levitsky }
13700ab30494SMaxim Levitsky 
msb_ftl_scan(struct msb_data * msb)13710ab30494SMaxim Levitsky static int msb_ftl_scan(struct msb_data *msb)
13720ab30494SMaxim Levitsky {
13730ab30494SMaxim Levitsky 	u16 pba, lba, other_block;
13746951c585SColin Ian King 	u8 overwrite_flag, management_flag, other_overwrite_flag;
13750ab30494SMaxim Levitsky 	int error;
13760ab30494SMaxim Levitsky 	struct ms_extra_data_register extra;
13770ab30494SMaxim Levitsky 	u8 *overwrite_flags = kzalloc(msb->block_count, GFP_KERNEL);
13780ab30494SMaxim Levitsky 
13790ab30494SMaxim Levitsky 	if (!overwrite_flags)
13800ab30494SMaxim Levitsky 		return -ENOMEM;
13810ab30494SMaxim Levitsky 
13820ab30494SMaxim Levitsky 	dbg("Start of media scanning");
13830ab30494SMaxim Levitsky 	for (pba = 0; pba < msb->block_count; pba++) {
13840ab30494SMaxim Levitsky 
13850ab30494SMaxim Levitsky 		if (pba == msb->boot_block_locations[0] ||
13860ab30494SMaxim Levitsky 			pba == msb->boot_block_locations[1]) {
13870ab30494SMaxim Levitsky 			dbg_verbose("pba %05d -> [boot block]", pba);
13880ab30494SMaxim Levitsky 			msb_mark_block_used(msb, pba);
13890ab30494SMaxim Levitsky 			continue;
13900ab30494SMaxim Levitsky 		}
13910ab30494SMaxim Levitsky 
13920ab30494SMaxim Levitsky 		if (test_bit(pba, msb->used_blocks_bitmap)) {
13930ab30494SMaxim Levitsky 			dbg_verbose("pba %05d -> [factory bad]", pba);
13940ab30494SMaxim Levitsky 			continue;
13950ab30494SMaxim Levitsky 		}
13960ab30494SMaxim Levitsky 
13970ab30494SMaxim Levitsky 		memset(&extra, 0, sizeof(extra));
13980ab30494SMaxim Levitsky 		error = msb_read_oob(msb, pba, 0, &extra);
13990ab30494SMaxim Levitsky 
14000ab30494SMaxim Levitsky 		/* can't trust the page if we can't read the oob */
14010ab30494SMaxim Levitsky 		if (error == -EBADMSG) {
14020ab30494SMaxim Levitsky 			pr_notice(
14030ab30494SMaxim Levitsky 			"oob of pba %d damaged, will try to erase it", pba);
14040ab30494SMaxim Levitsky 			msb_mark_block_used(msb, pba);
14050ab30494SMaxim Levitsky 			msb_erase_block(msb, pba);
14060ab30494SMaxim Levitsky 			continue;
14070ab30494SMaxim Levitsky 		} else if (error) {
14080ab30494SMaxim Levitsky 			pr_err("unknown error %d on read of oob of pba %d - aborting",
14090ab30494SMaxim Levitsky 				error, pba);
14100ab30494SMaxim Levitsky 
14110ab30494SMaxim Levitsky 			kfree(overwrite_flags);
14120ab30494SMaxim Levitsky 			return error;
14130ab30494SMaxim Levitsky 		}
14140ab30494SMaxim Levitsky 
14150ab30494SMaxim Levitsky 		lba = be16_to_cpu(extra.logical_address);
14166951c585SColin Ian King 		management_flag = extra.management_flag;
14170ab30494SMaxim Levitsky 		overwrite_flag = extra.overwrite_flag;
14180ab30494SMaxim Levitsky 		overwrite_flags[pba] = overwrite_flag;
14190ab30494SMaxim Levitsky 
14200ab30494SMaxim Levitsky 		/* Skip bad blocks */
14210ab30494SMaxim Levitsky 		if (!(overwrite_flag & MEMSTICK_OVERWRITE_BKST)) {
14220ab30494SMaxim Levitsky 			dbg("pba %05d -> [BAD]", pba);
14230ab30494SMaxim Levitsky 			msb_mark_block_used(msb, pba);
14240ab30494SMaxim Levitsky 			continue;
14250ab30494SMaxim Levitsky 		}
14260ab30494SMaxim Levitsky 
14270ab30494SMaxim Levitsky 		/* Skip system/drm blocks */
14286951c585SColin Ian King 		if ((management_flag & MEMSTICK_MANAGEMENT_FLAG_NORMAL) !=
14296951c585SColin Ian King 			MEMSTICK_MANAGEMENT_FLAG_NORMAL) {
14306951c585SColin Ian King 			dbg("pba %05d -> [reserved management flag %02x]",
14316951c585SColin Ian King 							pba, management_flag);
14320ab30494SMaxim Levitsky 			msb_mark_block_used(msb, pba);
14330ab30494SMaxim Levitsky 			continue;
14340ab30494SMaxim Levitsky 		}
14350ab30494SMaxim Levitsky 
14360ab30494SMaxim Levitsky 		/* Erase temporary tables */
14376951c585SColin Ian King 		if (!(management_flag & MEMSTICK_MANAGEMENT_ATFLG)) {
14380ab30494SMaxim Levitsky 			dbg("pba %05d -> [temp table] - will erase", pba);
14390ab30494SMaxim Levitsky 
14400ab30494SMaxim Levitsky 			msb_mark_block_used(msb, pba);
14410ab30494SMaxim Levitsky 			msb_erase_block(msb, pba);
14420ab30494SMaxim Levitsky 			continue;
14430ab30494SMaxim Levitsky 		}
14440ab30494SMaxim Levitsky 
14450ab30494SMaxim Levitsky 		if (lba == MS_BLOCK_INVALID) {
14460ab30494SMaxim Levitsky 			dbg_verbose("pba %05d -> [free]", pba);
14470ab30494SMaxim Levitsky 			continue;
14480ab30494SMaxim Levitsky 		}
14490ab30494SMaxim Levitsky 
14500ab30494SMaxim Levitsky 		msb_mark_block_used(msb, pba);
14510ab30494SMaxim Levitsky 
14520ab30494SMaxim Levitsky 		/* Block has LBA not according to zoning*/
14530ab30494SMaxim Levitsky 		if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
14540ab30494SMaxim Levitsky 			pr_notice("pba %05d -> [bad lba %05d] - will erase",
14550ab30494SMaxim Levitsky 								pba, lba);
14560ab30494SMaxim Levitsky 			msb_erase_block(msb, pba);
14570ab30494SMaxim Levitsky 			continue;
14580ab30494SMaxim Levitsky 		}
14590ab30494SMaxim Levitsky 
14600ab30494SMaxim Levitsky 		/* No collisions - great */
14610ab30494SMaxim Levitsky 		if (msb->lba_to_pba_table[lba] == MS_BLOCK_INVALID) {
14620ab30494SMaxim Levitsky 			dbg_verbose("pba %05d -> [lba %05d]", pba, lba);
14630ab30494SMaxim Levitsky 			msb->lba_to_pba_table[lba] = pba;
14640ab30494SMaxim Levitsky 			continue;
14650ab30494SMaxim Levitsky 		}
14660ab30494SMaxim Levitsky 
14670ab30494SMaxim Levitsky 		other_block = msb->lba_to_pba_table[lba];
14680ab30494SMaxim Levitsky 		other_overwrite_flag = overwrite_flags[other_block];
14690ab30494SMaxim Levitsky 
14700ab30494SMaxim Levitsky 		pr_notice("Collision between pba %d and pba %d",
14710ab30494SMaxim Levitsky 			pba, other_block);
14720ab30494SMaxim Levitsky 
14730ab30494SMaxim Levitsky 		if (!(overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
14740ab30494SMaxim Levitsky 			pr_notice("pba %d is marked as stable, use it", pba);
14750ab30494SMaxim Levitsky 			msb_erase_block(msb, other_block);
14760ab30494SMaxim Levitsky 			msb->lba_to_pba_table[lba] = pba;
14770ab30494SMaxim Levitsky 			continue;
14780ab30494SMaxim Levitsky 		}
14790ab30494SMaxim Levitsky 
14800ab30494SMaxim Levitsky 		if (!(other_overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
14810ab30494SMaxim Levitsky 			pr_notice("pba %d is marked as stable, use it",
14820ab30494SMaxim Levitsky 								other_block);
14830ab30494SMaxim Levitsky 			msb_erase_block(msb, pba);
14840ab30494SMaxim Levitsky 			continue;
14850ab30494SMaxim Levitsky 		}
14860ab30494SMaxim Levitsky 
14870ab30494SMaxim Levitsky 		pr_notice("collision between blocks %d and %d, without stable flag set on both, erasing pba %d",
14880ab30494SMaxim Levitsky 				pba, other_block, other_block);
14890ab30494SMaxim Levitsky 
14900ab30494SMaxim Levitsky 		msb_erase_block(msb, other_block);
14910ab30494SMaxim Levitsky 		msb->lba_to_pba_table[lba] = pba;
14920ab30494SMaxim Levitsky 	}
14930ab30494SMaxim Levitsky 
14940ab30494SMaxim Levitsky 	dbg("End of media scanning");
14950ab30494SMaxim Levitsky 	kfree(overwrite_flags);
14960ab30494SMaxim Levitsky 	return 0;
14970ab30494SMaxim Levitsky }
14980ab30494SMaxim Levitsky 
msb_cache_flush_timer(struct timer_list * t)1499e99e88a9SKees Cook static void msb_cache_flush_timer(struct timer_list *t)
15000ab30494SMaxim Levitsky {
1501e99e88a9SKees Cook 	struct msb_data *msb = from_timer(msb, t, cache_flush_timer);
15023ae61376SShubhankar Kuranagatti 
15030ab30494SMaxim Levitsky 	msb->need_flush_cache = true;
15040ab30494SMaxim Levitsky 	queue_work(msb->io_queue, &msb->io_work);
15050ab30494SMaxim Levitsky }
15060ab30494SMaxim Levitsky 
15070ab30494SMaxim Levitsky 
msb_cache_discard(struct msb_data * msb)15080ab30494SMaxim Levitsky static void msb_cache_discard(struct msb_data *msb)
15090ab30494SMaxim Levitsky {
15100ab30494SMaxim Levitsky 	if (msb->cache_block_lba == MS_BLOCK_INVALID)
15110ab30494SMaxim Levitsky 		return;
15120ab30494SMaxim Levitsky 
15130ab30494SMaxim Levitsky 	del_timer_sync(&msb->cache_flush_timer);
15140ab30494SMaxim Levitsky 
15150ab30494SMaxim Levitsky 	dbg_verbose("Discarding the write cache");
15160ab30494SMaxim Levitsky 	msb->cache_block_lba = MS_BLOCK_INVALID;
15170ab30494SMaxim Levitsky 	bitmap_zero(&msb->valid_cache_bitmap, msb->pages_in_block);
15180ab30494SMaxim Levitsky }
15190ab30494SMaxim Levitsky 
msb_cache_init(struct msb_data * msb)15200ab30494SMaxim Levitsky static int msb_cache_init(struct msb_data *msb)
15210ab30494SMaxim Levitsky {
1522e99e88a9SKees Cook 	timer_setup(&msb->cache_flush_timer, msb_cache_flush_timer, 0);
15230ab30494SMaxim Levitsky 
15240ab30494SMaxim Levitsky 	if (!msb->cache)
15250ab30494SMaxim Levitsky 		msb->cache = kzalloc(msb->block_size, GFP_KERNEL);
15260ab30494SMaxim Levitsky 	if (!msb->cache)
15270ab30494SMaxim Levitsky 		return -ENOMEM;
15280ab30494SMaxim Levitsky 
15290ab30494SMaxim Levitsky 	msb_cache_discard(msb);
15300ab30494SMaxim Levitsky 	return 0;
15310ab30494SMaxim Levitsky }
15320ab30494SMaxim Levitsky 
msb_cache_flush(struct msb_data * msb)15330ab30494SMaxim Levitsky static int msb_cache_flush(struct msb_data *msb)
15340ab30494SMaxim Levitsky {
15350ab30494SMaxim Levitsky 	struct scatterlist sg;
15360ab30494SMaxim Levitsky 	struct ms_extra_data_register extra;
15370ab30494SMaxim Levitsky 	int page, offset, error;
15380ab30494SMaxim Levitsky 	u16 pba, lba;
15390ab30494SMaxim Levitsky 
15400ab30494SMaxim Levitsky 	if (msb->read_only)
15410ab30494SMaxim Levitsky 		return -EROFS;
15420ab30494SMaxim Levitsky 
15430ab30494SMaxim Levitsky 	if (msb->cache_block_lba == MS_BLOCK_INVALID)
15440ab30494SMaxim Levitsky 		return 0;
15450ab30494SMaxim Levitsky 
15460ab30494SMaxim Levitsky 	lba = msb->cache_block_lba;
15470ab30494SMaxim Levitsky 	pba = msb->lba_to_pba_table[lba];
15480ab30494SMaxim Levitsky 
15490ab30494SMaxim Levitsky 	dbg_verbose("Flushing the write cache of pba %d (LBA %d)",
15500ab30494SMaxim Levitsky 						pba, msb->cache_block_lba);
15510ab30494SMaxim Levitsky 
15520ab30494SMaxim Levitsky 	sg_init_one(&sg, msb->cache , msb->block_size);
15530ab30494SMaxim Levitsky 
15540ab30494SMaxim Levitsky 	/* Read all missing pages in cache */
15550ab30494SMaxim Levitsky 	for (page = 0; page < msb->pages_in_block; page++) {
15560ab30494SMaxim Levitsky 
15570ab30494SMaxim Levitsky 		if (test_bit(page, &msb->valid_cache_bitmap))
15580ab30494SMaxim Levitsky 			continue;
15590ab30494SMaxim Levitsky 
15600ab30494SMaxim Levitsky 		offset = page * msb->page_size;
15610ab30494SMaxim Levitsky 
15620ab30494SMaxim Levitsky 		dbg_verbose("reading non-present sector %d of cache block %d",
15630ab30494SMaxim Levitsky 			page, lba);
15640ab30494SMaxim Levitsky 		error = msb_read_page(msb, pba, page, &extra, &sg, offset);
15650ab30494SMaxim Levitsky 
15660ab30494SMaxim Levitsky 		/* Bad pages are copied with 00 page status */
15670ab30494SMaxim Levitsky 		if (error == -EBADMSG) {
15680ab30494SMaxim Levitsky 			pr_err("read error on sector %d, contents probably damaged", page);
15690ab30494SMaxim Levitsky 			continue;
15700ab30494SMaxim Levitsky 		}
15710ab30494SMaxim Levitsky 
15720ab30494SMaxim Levitsky 		if (error)
15730ab30494SMaxim Levitsky 			return error;
15740ab30494SMaxim Levitsky 
15750ab30494SMaxim Levitsky 		if ((extra.overwrite_flag & MEMSTICK_OV_PG_NORMAL) !=
15760ab30494SMaxim Levitsky 							MEMSTICK_OV_PG_NORMAL) {
15770ab30494SMaxim Levitsky 			dbg("page %d is marked as bad", page);
15780ab30494SMaxim Levitsky 			continue;
15790ab30494SMaxim Levitsky 		}
15800ab30494SMaxim Levitsky 
15810ab30494SMaxim Levitsky 		set_bit(page, &msb->valid_cache_bitmap);
15820ab30494SMaxim Levitsky 	}
15830ab30494SMaxim Levitsky 
15840ab30494SMaxim Levitsky 	/* Write the cache now */
15850ab30494SMaxim Levitsky 	error = msb_update_block(msb, msb->cache_block_lba, &sg, 0);
15860ab30494SMaxim Levitsky 	pba = msb->lba_to_pba_table[msb->cache_block_lba];
15870ab30494SMaxim Levitsky 
15880ab30494SMaxim Levitsky 	/* Mark invalid pages */
15890ab30494SMaxim Levitsky 	if (!error) {
15900ab30494SMaxim Levitsky 		for (page = 0; page < msb->pages_in_block; page++) {
15910ab30494SMaxim Levitsky 
15920ab30494SMaxim Levitsky 			if (test_bit(page, &msb->valid_cache_bitmap))
15930ab30494SMaxim Levitsky 				continue;
15940ab30494SMaxim Levitsky 
15950ab30494SMaxim Levitsky 			dbg("marking page %d as containing damaged data",
15960ab30494SMaxim Levitsky 				page);
15970ab30494SMaxim Levitsky 			msb_set_overwrite_flag(msb,
15980ab30494SMaxim Levitsky 				pba , page, 0xFF & ~MEMSTICK_OV_PG_NORMAL);
15990ab30494SMaxim Levitsky 		}
16000ab30494SMaxim Levitsky 	}
16010ab30494SMaxim Levitsky 
16020ab30494SMaxim Levitsky 	msb_cache_discard(msb);
16030ab30494SMaxim Levitsky 	return error;
16040ab30494SMaxim Levitsky }
16050ab30494SMaxim Levitsky 
msb_cache_write(struct msb_data * msb,int lba,int page,bool add_to_cache_only,struct scatterlist * sg,int offset)16060ab30494SMaxim Levitsky static int msb_cache_write(struct msb_data *msb, int lba,
16070ab30494SMaxim Levitsky 	int page, bool add_to_cache_only, struct scatterlist *sg, int offset)
16080ab30494SMaxim Levitsky {
16090ab30494SMaxim Levitsky 	int error;
16100ab30494SMaxim Levitsky 	struct scatterlist sg_tmp[10];
16110ab30494SMaxim Levitsky 
16120ab30494SMaxim Levitsky 	if (msb->read_only)
16130ab30494SMaxim Levitsky 		return -EROFS;
16140ab30494SMaxim Levitsky 
16150ab30494SMaxim Levitsky 	if (msb->cache_block_lba == MS_BLOCK_INVALID ||
16160ab30494SMaxim Levitsky 						lba != msb->cache_block_lba)
16170ab30494SMaxim Levitsky 		if (add_to_cache_only)
16180ab30494SMaxim Levitsky 			return 0;
16190ab30494SMaxim Levitsky 
16200ab30494SMaxim Levitsky 	/* If we need to write different block */
16210ab30494SMaxim Levitsky 	if (msb->cache_block_lba != MS_BLOCK_INVALID &&
16220ab30494SMaxim Levitsky 						lba != msb->cache_block_lba) {
16230ab30494SMaxim Levitsky 		dbg_verbose("first flush the cache");
16240ab30494SMaxim Levitsky 		error = msb_cache_flush(msb);
16250ab30494SMaxim Levitsky 		if (error)
16260ab30494SMaxim Levitsky 			return error;
16270ab30494SMaxim Levitsky 	}
16280ab30494SMaxim Levitsky 
16290ab30494SMaxim Levitsky 	if (msb->cache_block_lba  == MS_BLOCK_INVALID) {
16300ab30494SMaxim Levitsky 		msb->cache_block_lba  = lba;
16310ab30494SMaxim Levitsky 		mod_timer(&msb->cache_flush_timer,
16320ab30494SMaxim Levitsky 			jiffies + msecs_to_jiffies(cache_flush_timeout));
16330ab30494SMaxim Levitsky 	}
16340ab30494SMaxim Levitsky 
16350ab30494SMaxim Levitsky 	dbg_verbose("Write of LBA %d page %d to cache ", lba, page);
16360ab30494SMaxim Levitsky 
16370ab30494SMaxim Levitsky 	sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
16380ab30494SMaxim Levitsky 	msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp), offset, msb->page_size);
16390ab30494SMaxim Levitsky 
16400ab30494SMaxim Levitsky 	sg_copy_to_buffer(sg_tmp, sg_nents(sg_tmp),
16410ab30494SMaxim Levitsky 		msb->cache + page * msb->page_size, msb->page_size);
16420ab30494SMaxim Levitsky 
16430ab30494SMaxim Levitsky 	set_bit(page, &msb->valid_cache_bitmap);
16440ab30494SMaxim Levitsky 	return 0;
16450ab30494SMaxim Levitsky }
16460ab30494SMaxim Levitsky 
msb_cache_read(struct msb_data * msb,int lba,int page,struct scatterlist * sg,int offset)16470ab30494SMaxim Levitsky static int msb_cache_read(struct msb_data *msb, int lba,
16480ab30494SMaxim Levitsky 				int page, struct scatterlist *sg, int offset)
16490ab30494SMaxim Levitsky {
16500ab30494SMaxim Levitsky 	int pba = msb->lba_to_pba_table[lba];
16510ab30494SMaxim Levitsky 	struct scatterlist sg_tmp[10];
16520ab30494SMaxim Levitsky 	int error = 0;
16530ab30494SMaxim Levitsky 
16540ab30494SMaxim Levitsky 	if (lba == msb->cache_block_lba &&
16550ab30494SMaxim Levitsky 			test_bit(page, &msb->valid_cache_bitmap)) {
16560ab30494SMaxim Levitsky 
16570ab30494SMaxim Levitsky 		dbg_verbose("Read of LBA %d (pba %d) sector %d from cache",
16580ab30494SMaxim Levitsky 							lba, pba, page);
16590ab30494SMaxim Levitsky 
16600ab30494SMaxim Levitsky 		sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
16610ab30494SMaxim Levitsky 		msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp),
16620ab30494SMaxim Levitsky 			offset, msb->page_size);
16630ab30494SMaxim Levitsky 		sg_copy_from_buffer(sg_tmp, sg_nents(sg_tmp),
16640ab30494SMaxim Levitsky 			msb->cache + msb->page_size * page,
16650ab30494SMaxim Levitsky 							msb->page_size);
16660ab30494SMaxim Levitsky 	} else {
16670ab30494SMaxim Levitsky 		dbg_verbose("Read of LBA %d (pba %d) sector %d from device",
16680ab30494SMaxim Levitsky 							lba, pba, page);
16690ab30494SMaxim Levitsky 
16700ab30494SMaxim Levitsky 		error = msb_read_page(msb, pba, page, NULL, sg, offset);
16710ab30494SMaxim Levitsky 		if (error)
16720ab30494SMaxim Levitsky 			return error;
16730ab30494SMaxim Levitsky 
16740ab30494SMaxim Levitsky 		msb_cache_write(msb, lba, page, true, sg, offset);
16750ab30494SMaxim Levitsky 	}
16760ab30494SMaxim Levitsky 	return error;
16770ab30494SMaxim Levitsky }
16780ab30494SMaxim Levitsky 
16790ab30494SMaxim Levitsky /* Emulated geometry table
16800ab30494SMaxim Levitsky  * This table content isn't that importaint,
16810ab30494SMaxim Levitsky  * One could put here different values, providing that they still
16820ab30494SMaxim Levitsky  * cover whole disk.
16833ae61376SShubhankar Kuranagatti  * 64 MB entry is what windows reports for my 64M memstick
16843ae61376SShubhankar Kuranagatti  */
16850ab30494SMaxim Levitsky 
16860ab30494SMaxim Levitsky static const struct chs_entry chs_table[] = {
16870ab30494SMaxim Levitsky /*        size sectors cylynders  heads */
16880ab30494SMaxim Levitsky 	{ 4,    16,    247,       2  },
16890ab30494SMaxim Levitsky 	{ 8,    16,    495,       2  },
16900ab30494SMaxim Levitsky 	{ 16,   16,    495,       4  },
16910ab30494SMaxim Levitsky 	{ 32,   16,    991,       4  },
16920ab30494SMaxim Levitsky 	{ 64,   16,    991,       8  },
16930ab30494SMaxim Levitsky 	{128,   16,    991,       16 },
16940ab30494SMaxim Levitsky 	{ 0 }
16950ab30494SMaxim Levitsky };
16960ab30494SMaxim Levitsky 
16970ab30494SMaxim Levitsky /* Load information about the card */
msb_init_card(struct memstick_dev * card)16980ab30494SMaxim Levitsky static int msb_init_card(struct memstick_dev *card)
16990ab30494SMaxim Levitsky {
17000ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
17010ab30494SMaxim Levitsky 	struct memstick_host *host = card->host;
17020ab30494SMaxim Levitsky 	struct ms_boot_page *boot_block;
17030ab30494SMaxim Levitsky 	int error = 0, i, raw_size_in_megs;
17040ab30494SMaxim Levitsky 
17050ab30494SMaxim Levitsky 	msb->caps = 0;
17060ab30494SMaxim Levitsky 
17070ab30494SMaxim Levitsky 	if (card->id.class >= MEMSTICK_CLASS_ROM &&
17080ab30494SMaxim Levitsky 				card->id.class <= MEMSTICK_CLASS_ROM)
17090ab30494SMaxim Levitsky 		msb->read_only = true;
17100ab30494SMaxim Levitsky 
17110ab30494SMaxim Levitsky 	msb->state = -1;
17120ab30494SMaxim Levitsky 	error = msb_reset(msb, false);
17130ab30494SMaxim Levitsky 	if (error)
17140ab30494SMaxim Levitsky 		return error;
17150ab30494SMaxim Levitsky 
17160ab30494SMaxim Levitsky 	/* Due to a bug in Jmicron driver written by Alex Dubov,
17173ae61376SShubhankar Kuranagatti 	 * its serial mode barely works,
17183ae61376SShubhankar Kuranagatti 	 * so we switch to parallel mode right away
17193ae61376SShubhankar Kuranagatti 	 */
17200ab30494SMaxim Levitsky 	if (host->caps & MEMSTICK_CAP_PAR4)
17210ab30494SMaxim Levitsky 		msb_switch_to_parallel(msb);
17220ab30494SMaxim Levitsky 
17230ab30494SMaxim Levitsky 	msb->page_size = sizeof(struct ms_boot_page);
17240ab30494SMaxim Levitsky 
17250ab30494SMaxim Levitsky 	/* Read the boot page */
17260ab30494SMaxim Levitsky 	error = msb_read_boot_blocks(msb);
17270ab30494SMaxim Levitsky 	if (error)
17280ab30494SMaxim Levitsky 		return -EIO;
17290ab30494SMaxim Levitsky 
17300ab30494SMaxim Levitsky 	boot_block = &msb->boot_page[0];
17310ab30494SMaxim Levitsky 
17320ab30494SMaxim Levitsky 	/* Save intersting attributes from boot page */
17330ab30494SMaxim Levitsky 	msb->block_count = boot_block->attr.number_of_blocks;
17340ab30494SMaxim Levitsky 	msb->page_size = boot_block->attr.page_size;
17350ab30494SMaxim Levitsky 
17360ab30494SMaxim Levitsky 	msb->pages_in_block = boot_block->attr.block_size * 2;
17370ab30494SMaxim Levitsky 	msb->block_size = msb->page_size * msb->pages_in_block;
17380ab30494SMaxim Levitsky 
17394853396fSArnd Bergmann 	if ((size_t)msb->page_size > PAGE_SIZE) {
17400ab30494SMaxim Levitsky 		/* this isn't supported by linux at all, anyway*/
17410ab30494SMaxim Levitsky 		dbg("device page %d size isn't supported", msb->page_size);
17420ab30494SMaxim Levitsky 		return -EINVAL;
17430ab30494SMaxim Levitsky 	}
17440ab30494SMaxim Levitsky 
17450ab30494SMaxim Levitsky 	msb->block_buffer = kzalloc(msb->block_size, GFP_KERNEL);
17460ab30494SMaxim Levitsky 	if (!msb->block_buffer)
17470ab30494SMaxim Levitsky 		return -ENOMEM;
17480ab30494SMaxim Levitsky 
17490ab30494SMaxim Levitsky 	raw_size_in_megs = (msb->block_size * msb->block_count) >> 20;
17500ab30494SMaxim Levitsky 
17510ab30494SMaxim Levitsky 	for (i = 0; chs_table[i].size; i++) {
17520ab30494SMaxim Levitsky 
17530ab30494SMaxim Levitsky 		if (chs_table[i].size != raw_size_in_megs)
17540ab30494SMaxim Levitsky 			continue;
17550ab30494SMaxim Levitsky 
17560ab30494SMaxim Levitsky 		msb->geometry.cylinders = chs_table[i].cyl;
17570ab30494SMaxim Levitsky 		msb->geometry.heads = chs_table[i].head;
17580ab30494SMaxim Levitsky 		msb->geometry.sectors = chs_table[i].sec;
17590ab30494SMaxim Levitsky 		break;
17600ab30494SMaxim Levitsky 	}
17610ab30494SMaxim Levitsky 
17620ab30494SMaxim Levitsky 	if (boot_block->attr.transfer_supporting == 1)
17630ab30494SMaxim Levitsky 		msb->caps |= MEMSTICK_CAP_PAR4;
17640ab30494SMaxim Levitsky 
17650ab30494SMaxim Levitsky 	if (boot_block->attr.device_type & 0x03)
17660ab30494SMaxim Levitsky 		msb->read_only = true;
17670ab30494SMaxim Levitsky 
17680ab30494SMaxim Levitsky 	dbg("Total block count = %d", msb->block_count);
17690ab30494SMaxim Levitsky 	dbg("Each block consists of %d pages", msb->pages_in_block);
17700ab30494SMaxim Levitsky 	dbg("Page size = %d bytes", msb->page_size);
17710ab30494SMaxim Levitsky 	dbg("Parallel mode supported: %d", !!(msb->caps & MEMSTICK_CAP_PAR4));
17720ab30494SMaxim Levitsky 	dbg("Read only: %d", msb->read_only);
17730ab30494SMaxim Levitsky 
17740ab30494SMaxim Levitsky #if 0
17750ab30494SMaxim Levitsky 	/* Now we can switch the interface */
17760ab30494SMaxim Levitsky 	if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
17770ab30494SMaxim Levitsky 		msb_switch_to_parallel(msb);
17780ab30494SMaxim Levitsky #endif
17790ab30494SMaxim Levitsky 
17800ab30494SMaxim Levitsky 	error = msb_cache_init(msb);
17810ab30494SMaxim Levitsky 	if (error)
17820ab30494SMaxim Levitsky 		return error;
17830ab30494SMaxim Levitsky 
17840ab30494SMaxim Levitsky 	error = msb_ftl_initialize(msb);
17850ab30494SMaxim Levitsky 	if (error)
17860ab30494SMaxim Levitsky 		return error;
17870ab30494SMaxim Levitsky 
17880ab30494SMaxim Levitsky 
17890ab30494SMaxim Levitsky 	/* Read the bad block table */
17900ab30494SMaxim Levitsky 	error = msb_read_bad_block_table(msb, 0);
17910ab30494SMaxim Levitsky 
17920ab30494SMaxim Levitsky 	if (error && error != -ENOMEM) {
17930ab30494SMaxim Levitsky 		dbg("failed to read bad block table from primary boot block, trying from backup");
17940ab30494SMaxim Levitsky 		error = msb_read_bad_block_table(msb, 1);
17950ab30494SMaxim Levitsky 	}
17960ab30494SMaxim Levitsky 
17970ab30494SMaxim Levitsky 	if (error)
17980ab30494SMaxim Levitsky 		return error;
17990ab30494SMaxim Levitsky 
18000ab30494SMaxim Levitsky 	/* *drum roll* Scan the media */
18010ab30494SMaxim Levitsky 	error = msb_ftl_scan(msb);
18020ab30494SMaxim Levitsky 	if (error) {
18030ab30494SMaxim Levitsky 		pr_err("Scan of media failed");
18040ab30494SMaxim Levitsky 		return error;
18050ab30494SMaxim Levitsky 	}
18060ab30494SMaxim Levitsky 
18070ab30494SMaxim Levitsky 	return 0;
18080ab30494SMaxim Levitsky 
18090ab30494SMaxim Levitsky }
18100ab30494SMaxim Levitsky 
msb_do_write_request(struct msb_data * msb,int lba,int page,struct scatterlist * sg,size_t len,int * sucessfuly_written)18110ab30494SMaxim Levitsky static int msb_do_write_request(struct msb_data *msb, int lba,
18120ab30494SMaxim Levitsky 	int page, struct scatterlist *sg, size_t len, int *sucessfuly_written)
18130ab30494SMaxim Levitsky {
18140ab30494SMaxim Levitsky 	int error = 0;
18150ab30494SMaxim Levitsky 	off_t offset = 0;
18160ab30494SMaxim Levitsky 	*sucessfuly_written = 0;
18170ab30494SMaxim Levitsky 
18180ab30494SMaxim Levitsky 	while (offset < len) {
18190ab30494SMaxim Levitsky 		if (page == 0 && len - offset >= msb->block_size) {
18200ab30494SMaxim Levitsky 
18210ab30494SMaxim Levitsky 			if (msb->cache_block_lba == lba)
18220ab30494SMaxim Levitsky 				msb_cache_discard(msb);
18230ab30494SMaxim Levitsky 
18240ab30494SMaxim Levitsky 			dbg_verbose("Writing whole lba %d", lba);
18250ab30494SMaxim Levitsky 			error = msb_update_block(msb, lba, sg, offset);
18260ab30494SMaxim Levitsky 			if (error)
18270ab30494SMaxim Levitsky 				return error;
18280ab30494SMaxim Levitsky 
18290ab30494SMaxim Levitsky 			offset += msb->block_size;
18300ab30494SMaxim Levitsky 			*sucessfuly_written += msb->block_size;
18310ab30494SMaxim Levitsky 			lba++;
18320ab30494SMaxim Levitsky 			continue;
18330ab30494SMaxim Levitsky 		}
18340ab30494SMaxim Levitsky 
18350ab30494SMaxim Levitsky 		error = msb_cache_write(msb, lba, page, false, sg, offset);
18360ab30494SMaxim Levitsky 		if (error)
18370ab30494SMaxim Levitsky 			return error;
18380ab30494SMaxim Levitsky 
18390ab30494SMaxim Levitsky 		offset += msb->page_size;
18400ab30494SMaxim Levitsky 		*sucessfuly_written += msb->page_size;
18410ab30494SMaxim Levitsky 
18420ab30494SMaxim Levitsky 		page++;
18430ab30494SMaxim Levitsky 		if (page == msb->pages_in_block) {
18440ab30494SMaxim Levitsky 			page = 0;
18450ab30494SMaxim Levitsky 			lba++;
18460ab30494SMaxim Levitsky 		}
18470ab30494SMaxim Levitsky 	}
18480ab30494SMaxim Levitsky 	return 0;
18490ab30494SMaxim Levitsky }
18500ab30494SMaxim Levitsky 
msb_do_read_request(struct msb_data * msb,int lba,int page,struct scatterlist * sg,int len,int * sucessfuly_read)18510ab30494SMaxim Levitsky static int msb_do_read_request(struct msb_data *msb, int lba,
18520ab30494SMaxim Levitsky 		int page, struct scatterlist *sg, int len, int *sucessfuly_read)
18530ab30494SMaxim Levitsky {
18540ab30494SMaxim Levitsky 	int error = 0;
18550ab30494SMaxim Levitsky 	int offset = 0;
18560ab30494SMaxim Levitsky 	*sucessfuly_read = 0;
18570ab30494SMaxim Levitsky 
18580ab30494SMaxim Levitsky 	while (offset < len) {
18590ab30494SMaxim Levitsky 
18600ab30494SMaxim Levitsky 		error = msb_cache_read(msb, lba, page, sg, offset);
18610ab30494SMaxim Levitsky 		if (error)
18620ab30494SMaxim Levitsky 			return error;
18630ab30494SMaxim Levitsky 
18640ab30494SMaxim Levitsky 		offset += msb->page_size;
18650ab30494SMaxim Levitsky 		*sucessfuly_read += msb->page_size;
18660ab30494SMaxim Levitsky 
18670ab30494SMaxim Levitsky 		page++;
18680ab30494SMaxim Levitsky 		if (page == msb->pages_in_block) {
18690ab30494SMaxim Levitsky 			page = 0;
18700ab30494SMaxim Levitsky 			lba++;
18710ab30494SMaxim Levitsky 		}
18720ab30494SMaxim Levitsky 	}
18730ab30494SMaxim Levitsky 	return 0;
18740ab30494SMaxim Levitsky }
18750ab30494SMaxim Levitsky 
msb_io_work(struct work_struct * work)18760ab30494SMaxim Levitsky static void msb_io_work(struct work_struct *work)
18770ab30494SMaxim Levitsky {
18780ab30494SMaxim Levitsky 	struct msb_data *msb = container_of(work, struct msb_data, io_work);
18790ab30494SMaxim Levitsky 	int page, error, len;
18800ab30494SMaxim Levitsky 	sector_t lba;
18810ab30494SMaxim Levitsky 	struct scatterlist *sg = msb->prealloc_sg;
1882db1142a8SJens Axboe 	struct request *req;
18830ab30494SMaxim Levitsky 
18840ab30494SMaxim Levitsky 	dbg_verbose("IO: work started");
18850ab30494SMaxim Levitsky 
18860ab30494SMaxim Levitsky 	while (1) {
1887db1142a8SJens Axboe 		spin_lock_irq(&msb->q_lock);
18880ab30494SMaxim Levitsky 
18890ab30494SMaxim Levitsky 		if (msb->need_flush_cache) {
18900ab30494SMaxim Levitsky 			msb->need_flush_cache = false;
1891db1142a8SJens Axboe 			spin_unlock_irq(&msb->q_lock);
18920ab30494SMaxim Levitsky 			msb_cache_flush(msb);
18930ab30494SMaxim Levitsky 			continue;
18940ab30494SMaxim Levitsky 		}
18950ab30494SMaxim Levitsky 
1896db1142a8SJens Axboe 		req = msb->req;
1897db1142a8SJens Axboe 		if (!req) {
18980ab30494SMaxim Levitsky 			dbg_verbose("IO: no more requests exiting");
1899db1142a8SJens Axboe 			spin_unlock_irq(&msb->q_lock);
19000ab30494SMaxim Levitsky 			return;
19010ab30494SMaxim Levitsky 		}
19020ab30494SMaxim Levitsky 
1903db1142a8SJens Axboe 		spin_unlock_irq(&msb->q_lock);
19040ab30494SMaxim Levitsky 
19050ab30494SMaxim Levitsky 		/* process the request */
19060ab30494SMaxim Levitsky 		dbg_verbose("IO: processing new request");
1907db1142a8SJens Axboe 		blk_rq_map_sg(msb->queue, req, sg);
19080ab30494SMaxim Levitsky 
1909db1142a8SJens Axboe 		lba = blk_rq_pos(req);
19100ab30494SMaxim Levitsky 
19110ab30494SMaxim Levitsky 		sector_div(lba, msb->page_size / 512);
1912545b5e2aSArnd Bergmann 		page = sector_div(lba, msb->pages_in_block);
19130ab30494SMaxim Levitsky 
19140ab30494SMaxim Levitsky 		if (rq_data_dir(msb->req) == READ)
19150ab30494SMaxim Levitsky 			error = msb_do_read_request(msb, lba, page, sg,
1916db1142a8SJens Axboe 				blk_rq_bytes(req), &len);
19170ab30494SMaxim Levitsky 		else
19180ab30494SMaxim Levitsky 			error = msb_do_write_request(msb, lba, page, sg,
1919db1142a8SJens Axboe 				blk_rq_bytes(req), &len);
19200ab30494SMaxim Levitsky 
1921db1142a8SJens Axboe 		if (len && !blk_update_request(req, BLK_STS_OK, len)) {
1922db1142a8SJens Axboe 			__blk_mq_end_request(req, BLK_STS_OK);
1923db1142a8SJens Axboe 			spin_lock_irq(&msb->q_lock);
19240ab30494SMaxim Levitsky 			msb->req = NULL;
1925db1142a8SJens Axboe 			spin_unlock_irq(&msb->q_lock);
1926db1142a8SJens Axboe 		}
19270ab30494SMaxim Levitsky 
19280ab30494SMaxim Levitsky 		if (error && msb->req) {
19292a842acaSChristoph Hellwig 			blk_status_t ret = errno_to_blk_status(error);
1930db1142a8SJens Axboe 
19310ab30494SMaxim Levitsky 			dbg_verbose("IO: ending one sector of the request with error");
1932db1142a8SJens Axboe 			blk_mq_end_request(req, ret);
1933db1142a8SJens Axboe 			spin_lock_irq(&msb->q_lock);
19340ab30494SMaxim Levitsky 			msb->req = NULL;
1935db1142a8SJens Axboe 			spin_unlock_irq(&msb->q_lock);
19360ab30494SMaxim Levitsky 		}
19370ab30494SMaxim Levitsky 
19380ab30494SMaxim Levitsky 		if (msb->req)
19390ab30494SMaxim Levitsky 			dbg_verbose("IO: request still pending");
19400ab30494SMaxim Levitsky 	}
19410ab30494SMaxim Levitsky }
19420ab30494SMaxim Levitsky 
19430ab30494SMaxim Levitsky static DEFINE_IDR(msb_disk_idr); /*set of used disk numbers */
19440ab30494SMaxim Levitsky static DEFINE_MUTEX(msb_disk_lock); /* protects against races in open/release */
19450ab30494SMaxim Levitsky 
msb_data_clear(struct msb_data * msb)19460ab30494SMaxim Levitsky static void msb_data_clear(struct msb_data *msb)
19470ab30494SMaxim Levitsky {
19480ab30494SMaxim Levitsky 	kfree(msb->boot_page);
19492e531bc3SChristophe JAILLET 	bitmap_free(msb->used_blocks_bitmap);
195054eb7a55SChristophe JAILLET 	bitmap_free(msb->erased_blocks_bitmap);
19510ab30494SMaxim Levitsky 	kfree(msb->lba_to_pba_table);
19520ab30494SMaxim Levitsky 	kfree(msb->cache);
19530ab30494SMaxim Levitsky 	msb->card = NULL;
19540ab30494SMaxim Levitsky }
19550ab30494SMaxim Levitsky 
msb_bd_getgeo(struct block_device * bdev,struct hd_geometry * geo)19560ab30494SMaxim Levitsky static int msb_bd_getgeo(struct block_device *bdev,
19570ab30494SMaxim Levitsky 				 struct hd_geometry *geo)
19580ab30494SMaxim Levitsky {
19590ab30494SMaxim Levitsky 	struct msb_data *msb = bdev->bd_disk->private_data;
19600ab30494SMaxim Levitsky 	*geo = msb->geometry;
19610ab30494SMaxim Levitsky 	return 0;
19620ab30494SMaxim Levitsky }
19630ab30494SMaxim Levitsky 
msb_bd_free_disk(struct gendisk * disk)1964e2efa079SChristoph Hellwig static void msb_bd_free_disk(struct gendisk *disk)
1965e2efa079SChristoph Hellwig {
1966e2efa079SChristoph Hellwig 	struct msb_data *msb = disk->private_data;
1967e2efa079SChristoph Hellwig 
1968e2efa079SChristoph Hellwig 	mutex_lock(&msb_disk_lock);
1969e2efa079SChristoph Hellwig 	idr_remove(&msb_disk_idr, msb->disk_id);
1970e2efa079SChristoph Hellwig 	mutex_unlock(&msb_disk_lock);
1971e2efa079SChristoph Hellwig 
1972e2efa079SChristoph Hellwig 	kfree(msb);
1973e2efa079SChristoph Hellwig }
1974e2efa079SChristoph Hellwig 
msb_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)1975db1142a8SJens Axboe static blk_status_t msb_queue_rq(struct blk_mq_hw_ctx *hctx,
1976db1142a8SJens Axboe 				 const struct blk_mq_queue_data *bd)
19770ab30494SMaxim Levitsky {
1978db1142a8SJens Axboe 	struct memstick_dev *card = hctx->queue->queuedata;
19790ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
1980db1142a8SJens Axboe 	struct request *req = bd->rq;
19810ab30494SMaxim Levitsky 
19820ab30494SMaxim Levitsky 	dbg_verbose("Submit request");
19830ab30494SMaxim Levitsky 
1984db1142a8SJens Axboe 	spin_lock_irq(&msb->q_lock);
1985db1142a8SJens Axboe 
19860ab30494SMaxim Levitsky 	if (msb->card_dead) {
19870ab30494SMaxim Levitsky 		dbg("Refusing requests on removed card");
19880ab30494SMaxim Levitsky 
19890ab30494SMaxim Levitsky 		WARN_ON(!msb->io_queue_stopped);
19900ab30494SMaxim Levitsky 
1991db1142a8SJens Axboe 		spin_unlock_irq(&msb->q_lock);
1992db1142a8SJens Axboe 		blk_mq_start_request(req);
1993db1142a8SJens Axboe 		return BLK_STS_IOERR;
19940ab30494SMaxim Levitsky 	}
19950ab30494SMaxim Levitsky 
1996db1142a8SJens Axboe 	if (msb->req) {
1997db1142a8SJens Axboe 		spin_unlock_irq(&msb->q_lock);
1998db1142a8SJens Axboe 		return BLK_STS_DEV_RESOURCE;
1999db1142a8SJens Axboe 	}
2000db1142a8SJens Axboe 
2001db1142a8SJens Axboe 	blk_mq_start_request(req);
2002db1142a8SJens Axboe 	msb->req = req;
20030ab30494SMaxim Levitsky 
20040ab30494SMaxim Levitsky 	if (!msb->io_queue_stopped)
20050ab30494SMaxim Levitsky 		queue_work(msb->io_queue, &msb->io_work);
2006db1142a8SJens Axboe 
2007db1142a8SJens Axboe 	spin_unlock_irq(&msb->q_lock);
2008db1142a8SJens Axboe 	return BLK_STS_OK;
20090ab30494SMaxim Levitsky }
20100ab30494SMaxim Levitsky 
msb_check_card(struct memstick_dev * card)20110ab30494SMaxim Levitsky static int msb_check_card(struct memstick_dev *card)
20120ab30494SMaxim Levitsky {
20130ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
20143ae61376SShubhankar Kuranagatti 
20150ab30494SMaxim Levitsky 	return (msb->card_dead == 0);
20160ab30494SMaxim Levitsky }
20170ab30494SMaxim Levitsky 
msb_stop(struct memstick_dev * card)20180ab30494SMaxim Levitsky static void msb_stop(struct memstick_dev *card)
20190ab30494SMaxim Levitsky {
20200ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
20210ab30494SMaxim Levitsky 	unsigned long flags;
20220ab30494SMaxim Levitsky 
20230ab30494SMaxim Levitsky 	dbg("Stopping all msblock IO");
20240ab30494SMaxim Levitsky 
2025db1142a8SJens Axboe 	blk_mq_stop_hw_queues(msb->queue);
20260ab30494SMaxim Levitsky 	spin_lock_irqsave(&msb->q_lock, flags);
20270ab30494SMaxim Levitsky 	msb->io_queue_stopped = true;
20280ab30494SMaxim Levitsky 	spin_unlock_irqrestore(&msb->q_lock, flags);
20290ab30494SMaxim Levitsky 
20300ab30494SMaxim Levitsky 	del_timer_sync(&msb->cache_flush_timer);
20310ab30494SMaxim Levitsky 	flush_workqueue(msb->io_queue);
20320ab30494SMaxim Levitsky 
20330ab30494SMaxim Levitsky 	spin_lock_irqsave(&msb->q_lock, flags);
2034db1142a8SJens Axboe 	if (msb->req) {
2035db1142a8SJens Axboe 		blk_mq_requeue_request(msb->req, false);
20360ab30494SMaxim Levitsky 		msb->req = NULL;
20370ab30494SMaxim Levitsky 	}
2038db1142a8SJens Axboe 	spin_unlock_irqrestore(&msb->q_lock, flags);
20390ab30494SMaxim Levitsky }
20400ab30494SMaxim Levitsky 
msb_start(struct memstick_dev * card)20410ab30494SMaxim Levitsky static void msb_start(struct memstick_dev *card)
20420ab30494SMaxim Levitsky {
20430ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
20440ab30494SMaxim Levitsky 	unsigned long flags;
20450ab30494SMaxim Levitsky 
20460ab30494SMaxim Levitsky 	dbg("Resuming IO from msblock");
20470ab30494SMaxim Levitsky 
20480ab30494SMaxim Levitsky 	msb_invalidate_reg_window(msb);
20490ab30494SMaxim Levitsky 
20500ab30494SMaxim Levitsky 	spin_lock_irqsave(&msb->q_lock, flags);
20510ab30494SMaxim Levitsky 	if (!msb->io_queue_stopped || msb->card_dead) {
20520ab30494SMaxim Levitsky 		spin_unlock_irqrestore(&msb->q_lock, flags);
20530ab30494SMaxim Levitsky 		return;
20540ab30494SMaxim Levitsky 	}
20550ab30494SMaxim Levitsky 	spin_unlock_irqrestore(&msb->q_lock, flags);
20560ab30494SMaxim Levitsky 
20570ab30494SMaxim Levitsky 	/* Kick cache flush anyway, its harmless */
20580ab30494SMaxim Levitsky 	msb->need_flush_cache = true;
20590ab30494SMaxim Levitsky 	msb->io_queue_stopped = false;
20600ab30494SMaxim Levitsky 
2061db1142a8SJens Axboe 	blk_mq_start_hw_queues(msb->queue);
20620ab30494SMaxim Levitsky 
20630ab30494SMaxim Levitsky 	queue_work(msb->io_queue, &msb->io_work);
20640ab30494SMaxim Levitsky 
20650ab30494SMaxim Levitsky }
20660ab30494SMaxim Levitsky 
20670ab30494SMaxim Levitsky static const struct block_device_operations msb_bdops = {
2068e2efa079SChristoph Hellwig 	.owner		= THIS_MODULE,
20690ab30494SMaxim Levitsky 	.getgeo		= msb_bd_getgeo,
2070e2efa079SChristoph Hellwig 	.free_disk	= msb_bd_free_disk,
20710ab30494SMaxim Levitsky };
20720ab30494SMaxim Levitsky 
2073db1142a8SJens Axboe static const struct blk_mq_ops msb_mq_ops = {
2074db1142a8SJens Axboe 	.queue_rq	= msb_queue_rq,
2075db1142a8SJens Axboe };
2076db1142a8SJens Axboe 
20770ab30494SMaxim Levitsky /* Registers the block device */
msb_init_disk(struct memstick_dev * card)20780ab30494SMaxim Levitsky static int msb_init_disk(struct memstick_dev *card)
20790ab30494SMaxim Levitsky {
20800ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
20810ab30494SMaxim Levitsky 	int rc;
20820ab30494SMaxim Levitsky 	unsigned long capacity;
20830ab30494SMaxim Levitsky 
20840ab30494SMaxim Levitsky 	mutex_lock(&msb_disk_lock);
20850ab30494SMaxim Levitsky 	msb->disk_id = idr_alloc(&msb_disk_idr, card, 0, 256, GFP_KERNEL);
20860ab30494SMaxim Levitsky 	mutex_unlock(&msb_disk_lock);
20870ab30494SMaxim Levitsky 
20880ab30494SMaxim Levitsky 	if (msb->disk_id  < 0)
20890ab30494SMaxim Levitsky 		return msb->disk_id;
20900ab30494SMaxim Levitsky 
2091f368b7d7SChristoph Hellwig 	rc = blk_mq_alloc_sq_tag_set(&msb->tag_set, &msb_mq_ops, 2,
2092db1142a8SJens Axboe 				     BLK_MQ_F_SHOULD_MERGE);
2093f368b7d7SChristoph Hellwig 	if (rc)
2094f368b7d7SChristoph Hellwig 		goto out_release_id;
20950ab30494SMaxim Levitsky 
2096f368b7d7SChristoph Hellwig 	msb->disk = blk_mq_alloc_disk(&msb->tag_set, card);
2097f368b7d7SChristoph Hellwig 	if (IS_ERR(msb->disk)) {
2098f368b7d7SChristoph Hellwig 		rc = PTR_ERR(msb->disk);
2099f368b7d7SChristoph Hellwig 		goto out_free_tag_set;
2100f368b7d7SChristoph Hellwig 	}
2101f368b7d7SChristoph Hellwig 	msb->queue = msb->disk->queue;
21020ab30494SMaxim Levitsky 
21030ab30494SMaxim Levitsky 	blk_queue_max_hw_sectors(msb->queue, MS_BLOCK_MAX_PAGES);
21040ab30494SMaxim Levitsky 	blk_queue_max_segments(msb->queue, MS_BLOCK_MAX_SEGS);
21050ab30494SMaxim Levitsky 	blk_queue_max_segment_size(msb->queue,
21060ab30494SMaxim Levitsky 				   MS_BLOCK_MAX_PAGES * msb->page_size);
21070ab30494SMaxim Levitsky 	blk_queue_logical_block_size(msb->queue, msb->page_size);
21080ab30494SMaxim Levitsky 
21090ab30494SMaxim Levitsky 	sprintf(msb->disk->disk_name, "msblk%d", msb->disk_id);
21100ab30494SMaxim Levitsky 	msb->disk->fops = &msb_bdops;
21110ab30494SMaxim Levitsky 	msb->disk->private_data = msb;
21120ab30494SMaxim Levitsky 
21130ab30494SMaxim Levitsky 	capacity = msb->pages_in_block * msb->logical_block_count;
21140ab30494SMaxim Levitsky 	capacity *= (msb->page_size / 512);
21150ab30494SMaxim Levitsky 	set_capacity(msb->disk, capacity);
21160ab30494SMaxim Levitsky 	dbg("Set total disk size to %lu sectors", capacity);
21170ab30494SMaxim Levitsky 
21180ab30494SMaxim Levitsky 	msb->io_queue = alloc_ordered_workqueue("ms_block", WQ_MEM_RECLAIM);
2119*4f431a04SJiasheng Jiang 	if (!msb->io_queue) {
2120*4f431a04SJiasheng Jiang 		rc = -ENOMEM;
2121*4f431a04SJiasheng Jiang 		goto out_cleanup_disk;
2122*4f431a04SJiasheng Jiang 	}
2123*4f431a04SJiasheng Jiang 
21240ab30494SMaxim Levitsky 	INIT_WORK(&msb->io_work, msb_io_work);
21250ab30494SMaxim Levitsky 	sg_init_table(msb->prealloc_sg, MS_BLOCK_MAX_SEGS+1);
21260ab30494SMaxim Levitsky 
21270ab30494SMaxim Levitsky 	if (msb->read_only)
21280ab30494SMaxim Levitsky 		set_disk_ro(msb->disk, 1);
21290ab30494SMaxim Levitsky 
21300ab30494SMaxim Levitsky 	msb_start(card);
21312304c55fSLuis Chamberlain 	rc = device_add_disk(&card->dev, msb->disk, NULL);
21322304c55fSLuis Chamberlain 	if (rc)
2133*4f431a04SJiasheng Jiang 		goto out_destroy_workqueue;
21340ab30494SMaxim Levitsky 	dbg("Disk added");
21350ab30494SMaxim Levitsky 	return 0;
21360ab30494SMaxim Levitsky 
2137*4f431a04SJiasheng Jiang out_destroy_workqueue:
2138*4f431a04SJiasheng Jiang 	destroy_workqueue(msb->io_queue);
21392304c55fSLuis Chamberlain out_cleanup_disk:
21408b9ab626SChristoph Hellwig 	put_disk(msb->disk);
2141f368b7d7SChristoph Hellwig out_free_tag_set:
2142f368b7d7SChristoph Hellwig 	blk_mq_free_tag_set(&msb->tag_set);
21430ab30494SMaxim Levitsky out_release_id:
21440ab30494SMaxim Levitsky 	mutex_lock(&msb_disk_lock);
21450ab30494SMaxim Levitsky 	idr_remove(&msb_disk_idr, msb->disk_id);
21460ab30494SMaxim Levitsky 	mutex_unlock(&msb_disk_lock);
21470ab30494SMaxim Levitsky 	return rc;
21480ab30494SMaxim Levitsky }
21490ab30494SMaxim Levitsky 
msb_probe(struct memstick_dev * card)21500ab30494SMaxim Levitsky static int msb_probe(struct memstick_dev *card)
21510ab30494SMaxim Levitsky {
21520ab30494SMaxim Levitsky 	struct msb_data *msb;
21530ab30494SMaxim Levitsky 	int rc = 0;
21540ab30494SMaxim Levitsky 
21550ab30494SMaxim Levitsky 	msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
21560ab30494SMaxim Levitsky 	if (!msb)
21570ab30494SMaxim Levitsky 		return -ENOMEM;
21580ab30494SMaxim Levitsky 	memstick_set_drvdata(card, msb);
21590ab30494SMaxim Levitsky 	msb->card = card;
21600ab30494SMaxim Levitsky 	spin_lock_init(&msb->q_lock);
21610ab30494SMaxim Levitsky 
21620ab30494SMaxim Levitsky 	rc = msb_init_card(card);
21630ab30494SMaxim Levitsky 	if (rc)
21640ab30494SMaxim Levitsky 		goto out_free;
21650ab30494SMaxim Levitsky 
21660ab30494SMaxim Levitsky 	rc = msb_init_disk(card);
21670ab30494SMaxim Levitsky 	if (!rc) {
21680ab30494SMaxim Levitsky 		card->check = msb_check_card;
21690ab30494SMaxim Levitsky 		card->stop = msb_stop;
21700ab30494SMaxim Levitsky 		card->start = msb_start;
21710ab30494SMaxim Levitsky 		return 0;
21720ab30494SMaxim Levitsky 	}
21730ab30494SMaxim Levitsky out_free:
21740ab30494SMaxim Levitsky 	memstick_set_drvdata(card, NULL);
21750ab30494SMaxim Levitsky 	msb_data_clear(msb);
21760ab30494SMaxim Levitsky 	kfree(msb);
21770ab30494SMaxim Levitsky 	return rc;
21780ab30494SMaxim Levitsky }
21790ab30494SMaxim Levitsky 
msb_remove(struct memstick_dev * card)21800ab30494SMaxim Levitsky static void msb_remove(struct memstick_dev *card)
21810ab30494SMaxim Levitsky {
21820ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
21830ab30494SMaxim Levitsky 	unsigned long flags;
21840ab30494SMaxim Levitsky 
21850ab30494SMaxim Levitsky 	if (!msb->io_queue_stopped)
21860ab30494SMaxim Levitsky 		msb_stop(card);
21870ab30494SMaxim Levitsky 
21880ab30494SMaxim Levitsky 	dbg("Removing the disk device");
21890ab30494SMaxim Levitsky 
21900ab30494SMaxim Levitsky 	/* Take care of unhandled + new requests from now on */
21910ab30494SMaxim Levitsky 	spin_lock_irqsave(&msb->q_lock, flags);
21920ab30494SMaxim Levitsky 	msb->card_dead = true;
21930ab30494SMaxim Levitsky 	spin_unlock_irqrestore(&msb->q_lock, flags);
2194db1142a8SJens Axboe 	blk_mq_start_hw_queues(msb->queue);
21950ab30494SMaxim Levitsky 
21960ab30494SMaxim Levitsky 	/* Remove the disk */
21970ab30494SMaxim Levitsky 	del_gendisk(msb->disk);
2198db1142a8SJens Axboe 	blk_mq_free_tag_set(&msb->tag_set);
21990ab30494SMaxim Levitsky 	msb->queue = NULL;
22000ab30494SMaxim Levitsky 
22010ab30494SMaxim Levitsky 	mutex_lock(&msb_disk_lock);
22020ab30494SMaxim Levitsky 	msb_data_clear(msb);
22030ab30494SMaxim Levitsky 	mutex_unlock(&msb_disk_lock);
22040ab30494SMaxim Levitsky 
2205e2efa079SChristoph Hellwig 	put_disk(msb->disk);
22060ab30494SMaxim Levitsky 	memstick_set_drvdata(card, NULL);
22070ab30494SMaxim Levitsky }
22080ab30494SMaxim Levitsky 
22090ab30494SMaxim Levitsky #ifdef CONFIG_PM
22100ab30494SMaxim Levitsky 
msb_suspend(struct memstick_dev * card,pm_message_t state)22110ab30494SMaxim Levitsky static int msb_suspend(struct memstick_dev *card, pm_message_t state)
22120ab30494SMaxim Levitsky {
22130ab30494SMaxim Levitsky 	msb_stop(card);
22140ab30494SMaxim Levitsky 	return 0;
22150ab30494SMaxim Levitsky }
22160ab30494SMaxim Levitsky 
msb_resume(struct memstick_dev * card)22170ab30494SMaxim Levitsky static int msb_resume(struct memstick_dev *card)
22180ab30494SMaxim Levitsky {
22190ab30494SMaxim Levitsky 	struct msb_data *msb = memstick_get_drvdata(card);
22200ab30494SMaxim Levitsky 	struct msb_data *new_msb = NULL;
22210ab30494SMaxim Levitsky 	bool card_dead = true;
22220ab30494SMaxim Levitsky 
22230ab30494SMaxim Levitsky #ifndef CONFIG_MEMSTICK_UNSAFE_RESUME
22240ab30494SMaxim Levitsky 	msb->card_dead = true;
22250ab30494SMaxim Levitsky 	return 0;
22260ab30494SMaxim Levitsky #endif
22270ab30494SMaxim Levitsky 	mutex_lock(&card->host->lock);
22280ab30494SMaxim Levitsky 
22290ab30494SMaxim Levitsky 	new_msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
22300ab30494SMaxim Levitsky 	if (!new_msb)
22310ab30494SMaxim Levitsky 		goto out;
22320ab30494SMaxim Levitsky 
22330ab30494SMaxim Levitsky 	new_msb->card = card;
22340ab30494SMaxim Levitsky 	memstick_set_drvdata(card, new_msb);
22350ab30494SMaxim Levitsky 	spin_lock_init(&new_msb->q_lock);
22360ab30494SMaxim Levitsky 	sg_init_table(msb->prealloc_sg, MS_BLOCK_MAX_SEGS+1);
22370ab30494SMaxim Levitsky 
22380ab30494SMaxim Levitsky 	if (msb_init_card(card))
22390ab30494SMaxim Levitsky 		goto out;
22400ab30494SMaxim Levitsky 
22410ab30494SMaxim Levitsky 	if (msb->block_size != new_msb->block_size)
22420ab30494SMaxim Levitsky 		goto out;
22430ab30494SMaxim Levitsky 
22440ab30494SMaxim Levitsky 	if (memcmp(msb->boot_page, new_msb->boot_page,
22450ab30494SMaxim Levitsky 					sizeof(struct ms_boot_page)))
22460ab30494SMaxim Levitsky 		goto out;
22470ab30494SMaxim Levitsky 
22480ab30494SMaxim Levitsky 	if (msb->logical_block_count != new_msb->logical_block_count ||
22490ab30494SMaxim Levitsky 		memcmp(msb->lba_to_pba_table, new_msb->lba_to_pba_table,
22500ab30494SMaxim Levitsky 						msb->logical_block_count))
22510ab30494SMaxim Levitsky 		goto out;
22520ab30494SMaxim Levitsky 
22530ab30494SMaxim Levitsky 	if (msb->block_count != new_msb->block_count ||
2254aabf199cSChristophe JAILLET 	    !bitmap_equal(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
2255aabf199cSChristophe JAILLET 							msb->block_count))
22560ab30494SMaxim Levitsky 		goto out;
22570ab30494SMaxim Levitsky 
22580ab30494SMaxim Levitsky 	card_dead = false;
22590ab30494SMaxim Levitsky out:
22600ab30494SMaxim Levitsky 	if (card_dead)
22610ab30494SMaxim Levitsky 		dbg("Card was removed/replaced during suspend");
22620ab30494SMaxim Levitsky 
22630ab30494SMaxim Levitsky 	msb->card_dead = card_dead;
22640ab30494SMaxim Levitsky 	memstick_set_drvdata(card, msb);
22650ab30494SMaxim Levitsky 
22660ab30494SMaxim Levitsky 	if (new_msb) {
22670ab30494SMaxim Levitsky 		msb_data_clear(new_msb);
22680ab30494SMaxim Levitsky 		kfree(new_msb);
22690ab30494SMaxim Levitsky 	}
22700ab30494SMaxim Levitsky 
22710ab30494SMaxim Levitsky 	msb_start(card);
22720ab30494SMaxim Levitsky 	mutex_unlock(&card->host->lock);
22730ab30494SMaxim Levitsky 	return 0;
22740ab30494SMaxim Levitsky }
22750ab30494SMaxim Levitsky #else
22760ab30494SMaxim Levitsky 
22770ab30494SMaxim Levitsky #define msb_suspend NULL
22780ab30494SMaxim Levitsky #define msb_resume NULL
22790ab30494SMaxim Levitsky 
22800ab30494SMaxim Levitsky #endif /* CONFIG_PM */
22810ab30494SMaxim Levitsky 
22820ab30494SMaxim Levitsky static struct memstick_device_id msb_id_tbl[] = {
22830ab30494SMaxim Levitsky 	{MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
22840ab30494SMaxim Levitsky 	 MEMSTICK_CLASS_FLASH},
22850ab30494SMaxim Levitsky 
22860ab30494SMaxim Levitsky 	{MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
22870ab30494SMaxim Levitsky 	 MEMSTICK_CLASS_ROM},
22880ab30494SMaxim Levitsky 
22890ab30494SMaxim Levitsky 	{MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
22900ab30494SMaxim Levitsky 	 MEMSTICK_CLASS_RO},
22910ab30494SMaxim Levitsky 
22920ab30494SMaxim Levitsky 	{MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
22930ab30494SMaxim Levitsky 	 MEMSTICK_CLASS_WP},
22940ab30494SMaxim Levitsky 
22950ab30494SMaxim Levitsky 	{MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_DUO, MEMSTICK_CATEGORY_STORAGE_DUO,
22960ab30494SMaxim Levitsky 	 MEMSTICK_CLASS_DUO},
22970ab30494SMaxim Levitsky 	{}
22980ab30494SMaxim Levitsky };
22990ab30494SMaxim Levitsky MODULE_DEVICE_TABLE(memstick, msb_id_tbl);
23000ab30494SMaxim Levitsky 
23010ab30494SMaxim Levitsky 
23020ab30494SMaxim Levitsky static struct memstick_driver msb_driver = {
23030ab30494SMaxim Levitsky 	.driver = {
23040ab30494SMaxim Levitsky 		.name  = DRIVER_NAME,
23050ab30494SMaxim Levitsky 		.owner = THIS_MODULE
23060ab30494SMaxim Levitsky 	},
23070ab30494SMaxim Levitsky 	.id_table = msb_id_tbl,
23080ab30494SMaxim Levitsky 	.probe    = msb_probe,
23090ab30494SMaxim Levitsky 	.remove   = msb_remove,
23100ab30494SMaxim Levitsky 	.suspend  = msb_suspend,
23110ab30494SMaxim Levitsky 	.resume   = msb_resume
23120ab30494SMaxim Levitsky };
23130ab30494SMaxim Levitsky 
msb_init(void)23140ab30494SMaxim Levitsky static int __init msb_init(void)
23150ab30494SMaxim Levitsky {
2316ca945e71SNeilBrown 	int rc = memstick_register_driver(&msb_driver);
23173ae61376SShubhankar Kuranagatti 
2318ca945e71SNeilBrown 	if (rc)
23190ab30494SMaxim Levitsky 		pr_err("failed to register memstick driver (error %d)\n", rc);
23200ab30494SMaxim Levitsky 
23210ab30494SMaxim Levitsky 	return rc;
23220ab30494SMaxim Levitsky }
23230ab30494SMaxim Levitsky 
msb_exit(void)23240ab30494SMaxim Levitsky static void __exit msb_exit(void)
23250ab30494SMaxim Levitsky {
23260ab30494SMaxim Levitsky 	memstick_unregister_driver(&msb_driver);
23270ab30494SMaxim Levitsky 	idr_destroy(&msb_disk_idr);
23280ab30494SMaxim Levitsky }
23290ab30494SMaxim Levitsky 
23300ab30494SMaxim Levitsky module_init(msb_init);
23310ab30494SMaxim Levitsky module_exit(msb_exit);
23320ab30494SMaxim Levitsky 
23330ab30494SMaxim Levitsky module_param(cache_flush_timeout, int, S_IRUGO);
23340ab30494SMaxim Levitsky MODULE_PARM_DESC(cache_flush_timeout,
23350ab30494SMaxim Levitsky 				"Cache flush timeout in msec (1000 default)");
23360ab30494SMaxim Levitsky module_param(debug, int, S_IRUGO | S_IWUSR);
23370ab30494SMaxim Levitsky MODULE_PARM_DESC(debug, "Debug level (0-2)");
23380ab30494SMaxim Levitsky 
23390ab30494SMaxim Levitsky module_param(verify_writes, bool, S_IRUGO);
23400ab30494SMaxim Levitsky MODULE_PARM_DESC(verify_writes, "Read back and check all data that is written");
23410ab30494SMaxim Levitsky 
23420ab30494SMaxim Levitsky MODULE_LICENSE("GPL");
23430ab30494SMaxim Levitsky MODULE_AUTHOR("Maxim Levitsky");
23440ab30494SMaxim Levitsky MODULE_DESCRIPTION("Sony MemoryStick block device driver");
2345