Subsystems (2)
Maxim Levitsky
maintainerHi,
This is a revised generic driver for memstick standard cards, that I posted a year ago.
I removed the scatter-gather list abuse from it, but otherwice its unchanged.
I tested it quite a lot here and everything is fine.
Could you merge it?
Best regards,
Maxim Levitsky
Maxim Levitsky
maintainerPATCH 1/1 memstick: add support for legacy memorysticks
Based partially on MS standard spec quotes from Alex Dubov.
As any code that works with user data this driver isn't recommended to use to write cards that contain valuable data.
It tries its best though to avoid data corruption
and possible damage to the card.
Tested on MS DUO 64 MB card on Ricoh R592 card reader..
--- MAINTAINERS | 5 + drivers/memstick/core/Kconfig | 12 + drivers/memstick/core/Makefile | 2 +- drivers/memstick/core/ms_block.c | 2424 ++++++++++++++++++++++++++++++++++++++ drivers/memstick/core/ms_block.h | 246 ++++ 5 files changed, 2688 insertions(+), 1 deletion(-) create mode 100644 drivers/memstick/core/ms_block.c create mode 100644 drivers/memstick/core/ms_block.h
MAINTAINERS +5 -0
@@ -6393,6 +6393,11 @@ W: http://tifmxx.berlios.de/ S: Maintained F: drivers/memstick/host/tifm_ms.c +SONY MEMORYSTICK STANDARD SUPPORT +M: Maxim Levitsky <[email protected]> +S: Maintained +F: drivers/memstick/core/ms_block.* + SOUND M: Jaroslav Kysela <[email protected]> M: Takashi Iwai <[email protected]>
drivers/memstick/core/Kconfig +12 -0
@@ -24,3 +24,15 @@ config MSPRO_BLOCK support. This provides a block device driver, which you can use to mount the filesystem. Almost everyone wishing MemoryStick support should say Y or M here. + +config MS_BLOCK + tristate "MemoryStick Standard device driver" + depends on BLOCK && EXPERIMENTAL + help + Say Y here to enable the MemoryStick Standard device driver + support. This provides a block device driver, which you can use + to mount the filesystem. + This driver works with old (bulky) MemoryStick and MemoryStick Duo + but not PRO. Say Y if you have such card. + Driver is new and not yet well tested, thus it can damage your card + (even permanently)
drivers/memstick/core/Makefile +1 -1
@@ -3,5 +3,5 @@ # obj-$(CONFIG_MEMSTICK) += memstick.o - +obj-$(CONFIG_MS_BLOCK) += ms_block.o obj-$(CONFIG_MSPRO_BLOCK) += mspro_block.o
drivers/memstick/core/ms_block.c new file +2424 -0
2,424 lines not on this page · raw message
drivers/memstick/core/ms_block.h new file +246 -0
246 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Andrew Morton
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 10 lines · On Wed, 19 Sep 2012 16:19:02 +0300 Maxim Levitsky <[email protected]>…
Based partially on MS standard spec quotes from Alex Dubov.
As any code that works with user data this driver isn't recommended to use to write cards that contain valuable data.
It tries its best though to avoid data corruption
and possible damage to the card.Tested on MS DUO 64 MB card on Ricoh R592 card reader..
Generally looks nice to me, although I wouldn't know a memstick if one stuck to my shoe.
I have a bunch of fairly trivial comments, and one head-scratcher for
Tejun: what's up with the local-irqs-disabled requirement in sg_miter_next()?
drivers/memstick/core/ms_block.c line 46
Quoted 12 lines
...
drivers/memstick/core/ms_block.c new file +10
-0+static int sg_nents (struct scatterlist *sg) +{ +int nents =0 ; +while (sg) { + nents++; + sg =sg_next (sg); + } + +return nents; +}
I think we may as well put this in scatterlist.c immediately. It wouldn't surprise me if we already have open-coded copies of this lying around the tree.
drivers/memstick/core/ms_block.c line 53
Quoted 6 lines
drivers/memstick/core/ms_block.c new file +6
-0+/* +* Copies section of 'sg_from' starting from offset 'offset' and with length +* 'len' To another scatterlist of to_nents enties +*/ +static int sg_copy (struct scatterlist *sg_from,struct scatterlist *sg_to, +int to_nents,int offset,size_t len)
The should return a size_t, to match the type of len'. And the type of offset' is fishy - there's nothing which intrinsically limits these things to 2G?
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +5
-0+{ +int copied =0 ; + +while (offset >0 ) { +
drivers/memstick/core/ms_block.c line 68
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+if (offset >= sg_from->length) { +if (sg_is_last (sg_from)) +return 0 ; + + offset -= sg_from->length; + sg_from =sg_next (sg_from); +continue ; + } + + copied =min (len, (size_t )(sg_from->length - offset));
min_t would be more conventional. Or perhaps even min(), depending on `offset's new type (but probably not).
Quoted 32 lines
drivers/memstick/core/ms_block.c new file +32
-0+sg_set_page (sg_to,sg_page (sg_from), + copied, sg_from->offset + offset); + + len -= copied; + offset =0 ; + +if (sg_is_last (sg_from) || !len) + goto out; + + sg_to =sg_next (sg_to); + to_nents--; + sg_from =sg_next (sg_from); + } + +while (len > sg_from->length && to_nents--) { + + len -= sg_from->length; + copied += sg_from->length; + +sg_set_page (sg_to,sg_page (sg_from), + sg_from->length, sg_from->offset); + +if (sg_is_last (sg_from) || !len) + goto out; + + sg_from =sg_next (sg_from); + sg_to =sg_next (sg_to); + } + +if (len && to_nents) { +sg_set_page (sg_to,sg_page (sg_from), len, sg_from->offset); + copied += len;
drivers/memstick/core/ms_block.c line 110
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ } + +out: +sg_mark_end (sg_to); +return copied; +} + +/* +* Compares section of 'sg' starting from offset 'offset' and with length 'len' +* to linear buffer of length 'len' at address 'buffer'
The comment should document the return value.
drivers/memstick/core/ms_block.c line 119
Quoted 9 lines
+ */
drivers/memstick/core/ms_block.c new file +8
-0+static bool sg_compare_to_buffer (struct scatterlist *sg, +int offset,u8 *buffer,size_t len) +{ +unsigned long flags; +int retval =0 ; +struct sg_mapping_iter miter; + +local_irq_save (flags);
hm, the "IRQ disabled if SG_MITER_ATOMIC" requirement of sg_miter_next() is weird and unexpected.
Tejun's 137d3edb48425f8 added the code whch has this strange requirement, but it is unexplained. Wazzup?
Quoted 40 lines
drivers/memstick/core/ms_block.c new file +37
-0+sg_miter_start (&miter, sg,sg_nents (sg), +SG_MITER_ATOMIC |SG_MITER_FROM_SG ); + +while (sg_miter_next (&miter) && len >0 ) { + +int cmplen; + +if (offset >= miter.length) { + offset -= miter.length; +continue ; + } + + cmplen =min (miter.length - offset, len); + retval =memcmp (miter.addr + offset, buffer, cmplen); +if (retval) +break ; + + buffer += cmplen; + len -= cmplen; + offset =0 ; + } + +if (!retval && len) + retval =-1 ; + +sg_miter_stop (&miter); +local_irq_restore (flags); +return retval; +} +static void msb_mark_block_unused (struct msb_data *msb,int pba) +{ +int zone =msb_get_zone_from_pba (pba); + +if (!test_bit (pba, msb->used_blocks_bitmap)) { +ms_printk ("BUG: attempt to mark " +"already unused pba %d as unused" , pba); + msb->read_only = true;
drivers/memstick/core/ms_block.c line 228
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+return ; + } + +#ifdef DEBUG +if (msb_validate_used_block_bitmap (msb)) +return ; +#endif +clear_bit (pba, msb->used_blocks_bitmap); + msb->free_block_count[zone]++; +}
Presumably there is some lock which the caller must hold to prevent the obvious races in the above two functions. I suggest adding a comment whcih documents this requirement, then take advantage of that lock to switch to the more efficient __set_bit() and __clear_bit().
Quoted 1 line
+/* Invalidate current register window*/
hm, checkpatch misses the missing space before */
Quoted 22 lines
drivers/memstick/core/ms_block.c new file +22
-0+static void msb_invalidate_reg_window (struct msb_data *msb) +{ + msb->reg_addr.w_offset = offsetof(struct ms_register , id); + msb->reg_addr.w_length =sizeof (struct ms_id_register ); + msb->reg_addr.r_offset = offsetof(struct ms_register , id); + msb->reg_addr.r_length =sizeof (struct ms_id_register ); + msb->addr_valid = false; +} + +/* Sane way to start a state machine*/ +static int msb_run_state_machine (struct msb_data *msb,int (*state_func) + (struct memstick_dev *card,struct memstick_request **req)) +{ +struct memstick_dev *card = msb->card; + +WARN_ON (msb->state !=-1 ); + msb->int_polling = false; + msb->state =0 ; + msb->exit_error =0 ; + +memset (&card->current_mrq,0 ,sizeof (card->current_mrq)); +
drivers/memstick/core/ms_block.c line 262
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ card->next_request = state_func; +memstick_new_req (card->host); +wait_for_completion (&card->mrq_complete); + +WARN_ON (msb->state !=-1 ); +return msb->exit_error; +} + +/* State machines call that to exit */ +int msb_exit_state_machine (struct msb_data *msb,int error)
static
Quoted 7 lines
drivers/memstick/core/ms_block.c new file +7
-0+{ +WARN_ON (msb->state ==-1 ); + + msb->state =-1 ; + msb->exit_error = error; + msb->card->next_request = h_msb_default_bad; +
drivers/memstick/core/ms_block.c line 279
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+/* Invalidate reg window on errors */ +if (error) +msb_invalidate_reg_window (msb); + +complete (&msb->card->mrq_complete); +return -ENXIO ; +} + +/* read INT register */ +int msb_read_int_reg (struct msb_data *msb,long timeout)
static
drivers/memstick/core/ms_block.c line 282
Quoted 3 lines
drivers/memstick/core/ms_block.c new file +3
-0+{ +struct memstick_request *mrq = &msb->card->current_mrq; +WARN_ON (msb->state ==-1 );
It's conventional to put a newline between end-of-locals and start-of-code.
Quoted 12 lines
drivers/memstick/core/ms_block.c new file +12
-0+if (!msb->int_polling) { + msb->int_timeout = jiffies + +msecs_to_jiffies (timeout ==-1 ?500 : timeout); + msb->int_polling = true; + }else if (time_after (jiffies, msb->int_timeout)) { + mrq->data[0 ] =MEMSTICK_INT_CMDNAK ; +return 0 ; + } + +if ((msb->caps &MEMSTICK_CAP_AUTO_GET_INT ) && + mrq->need_card_int && !mrq->error) { + mrq->data[0 ] = mrq->int_reg;
drivers/memstick/core/ms_block.c line 305
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ mrq->need_card_int = false; +return 0 ; + }else { +memstick_init_req (mrq,MS_TPC_GET_INT ,NULL ,1 ); +return 1 ; + } +} + +/* Read a register */ +int msb_read_regs (struct msb_data *msb,int offset,int len)
static. I'll stop checking them now ;)
Quoted 31 lines
drivers/memstick/core/ms_block.c new file +31
-0+{ +struct memstick_request *req = &msb->card->current_mrq; + +if (msb->reg_addr.r_offset != offset || + msb->reg_addr.r_length != len || !msb->addr_valid) { + + msb->reg_addr.r_offset = offset; + msb->reg_addr.r_length = len; + msb->addr_valid = true; + +memstick_init_req (req,MS_TPC_SET_RW_REG_ADRS , + &msb->reg_addr,sizeof (msb->reg_addr)); +return 0 ; + } + +memstick_init_req (req,MS_TPC_READ_REG ,NULL , len); +return 1 ; +} + +/* Write a card register */ +int msb_write_regs (struct msb_data *msb,int offset,int len,void *buf) +{ +struct memstick_request *req = &msb->card->current_mrq; + +if (msb->reg_addr.w_offset != offset || + msb->reg_addr.w_length != len || !msb->addr_valid) { + + msb->reg_addr.w_offset = offset; + msb->reg_addr.w_length = len; + msb->addr_valid = true; +
drivers/memstick/core/ms_block.c line 346
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+memstick_init_req (req,MS_TPC_SET_RW_REG_ADRS , + &msb->reg_addr,sizeof (msb->reg_addr)); +return 0 ; + } + +memstick_init_req (req,MS_TPC_WRITE_REG , buf, len); +return 1 ; +} + +/* Handler for absense of IO */
"absence" :)
Quoted 118 lines
drivers/memstick/core/ms_block.c new file +118
-0+static int h_msb_default_bad (struct memstick_dev *card, +struct memstick_request **mrq) +{ +return -ENXIO ; +} + +/* +* This function is a handler for reads of one page from device. +* Writes output to msb->current_sg, takes sector address from msb->reg.param +* Can also be used to read extra data only. Set params accordintly. +*/ +static int h_msb_read_page (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +struct scatterlist sg[2 ]; +u8 command, intreg; + +if (mrq->error) { +dbg ("read_page, unknown error" ); +return msb_exit_state_machine (msb, mrq->error); + } +again: +switch (msb->state) { +case 0 :/* Write the sector address */ +if (!msb_write_regs (msb, + offsetof(struct ms_register , param), +sizeof (struct ms_param_register ), + (unsigned char *)&msb->regs.param)) +return 0 ; +break ; + +case 1 :/* Execute the read command*/ + command =MS_CMD_BLOCK_READ ; +memstick_init_req (mrq,MS_TPC_SET_CMD , &command,1 ); +break ; + +case 2 :/* send INT request */ +if (msb_read_int_reg (msb,-1 )) +break ; + msb->state++; + +case 3 :/* get result of the INT request*/ + intreg = mrq->data[0 ]; + msb->regs.status.interrupt = intreg; + +if (intreg &MEMSTICK_INT_CMDNAK ) +return msb_exit_state_machine (msb, -EIO ); + +if (!(intreg &MEMSTICK_INT_CED )) { + msb->state--; + goto again; + } + + msb->int_polling = false; + +if (intreg &MEMSTICK_INT_ERR ) + msb->state++; +else + msb->state =6 ; + + goto again; + +case 4 :/* read the status register +to understand source of the INT_ERR */ +if (!msb_read_regs (msb, + offsetof(struct ms_register , status), +sizeof (struct ms_status_register ))) +return 0 ; +break ; + +case 5 :/* get results of status check */ + msb->regs.status = *(struct ms_status_register *)mrq->data; + msb->state++; + +case 6 :/* Send extra data read request */ +if (!msb_read_regs (msb, + offsetof(struct ms_register , extra_data), +sizeof (struct ms_extra_data_register ))) +return 0 ; +break ; + +case 7 :/* Save result of extra data request */ + msb->regs.extra_data = + *(struct ms_extra_data_register *) mrq->data; + msb->state++; + +case 8 :/* Send the MS_TPC_READ_LONG_DATA to read IO buffer */ + +/* Skip that state if we only read the oob */ +if (msb->regs.param.cp ==MEMSTICK_CP_EXTRA ) { + msb->state++; + goto again; + } + +sg_init_table (sg,ARRAY_SIZE (sg)); +sg_copy (msb->current_sg, sg,ARRAY_SIZE (sg), + msb->current_sg_offset, + msb->page_size); + +memstick_init_req_sg (mrq,MS_TPC_READ_LONG_DATA , sg); +break ; + +case 9 :/* check validity of data buffer & done */ + +if (!(msb->regs.status.interrupt &MEMSTICK_INT_ERR )) { + msb->current_sg_offset += msb->page_size; +return msb_exit_state_machine (msb,0 ); + } + +if (msb->regs.status.status1 &MEMSTICK_UNCORR_ERROR ) { +dbg ("read_page: uncorrectable error" ); +return msb_exit_state_machine (msb, -EBADMSG ); + } + +if (msb->regs.status.status1 &MEMSTICK_CORR_ERROR ) { +dbg ("read_page: correctable error" );
drivers/memstick/core/ms_block.c line 474
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ msb->current_sg_offset += msb->page_size; +return msb_exit_state_machine (msb, -EUCLEAN ); + }else { +dbg ("read_page: INT error, but no status error bits" ); +return msb_exit_state_machine (msb, -EIO ); + } +default : +BUG (); + } + msb->state++;
I think this code would be quite a lot cleaner, safer and more readable if you were to do away with the "msb->state++" concept altogether. Just do
enum msb_states {
state_foo1,
state_foo2
...
};and do
msb->state = state_foo2;
in each switch case.
This way, the reader doesn't have to scratch his head over what the state _used_ to be, and the states have nice names to help the reader understand what's going on.
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +2
-0+return 0 ; +}
drivers/memstick/core/ms_block.c line 596
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+/* +* This function is used to send simple IO requests to device that consist +* of register write + command +*/ +static int h_msb_send_command (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +
stray newline
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+u8 intreg; + +if (mrq->error) { +dbg ("send_command: unknown error" ); +return msb_exit_state_machine (msb, mrq->error); + } +again: +switch (msb->state) { + +/* HACK: see h_msb_write_block */
drivers/memstick/core/ms_block.c line 665
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +7
-0+ +static int h_msb_parallel_switch (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +
stray newline. Please fix 'em up - it does make the code a little discordant to read.
Quoted 29 lines
drivers/memstick/core/ms_block.c new file +29
-0+struct memstick_host *host = card->host; + +if (mrq->error) { +dbg ("parallel_switch: error" ); + msb->regs.param.system &= ~MEMSTICK_SYS_PAM ; +return msb_exit_state_machine (msb, mrq->error); + } + +switch (msb->state) { +case 0 :/* Set the parallel interface on memstick side */ + msb->regs.param.system |=MEMSTICK_SYS_PAM ; + +if (!msb_write_regs (msb, + offsetof(struct ms_register , param), +1 , + (unsigned char *)&msb->regs.param)) +return 0 ; +break ; + +case 1 :/* Set parallel interface on our side + send a dummy request +to see if card responds */ + host->set_param (host,MEMSTICK_INTERFACE ,MEMSTICK_PAR4 ); +memstick_init_req (mrq,MS_TPC_GET_INT ,NULL ,1 ); +break ; + +case 2 : +return msb_exit_state_machine (msb,0 ); + } + msb->state++;
drivers/memstick/core/ms_block.c line 726
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+return 0 ; +} + +static int msb_switch_to_parallel (struct msb_data *msb); + +/* Reset the card, to guard against hw errors beeing treated as bad blocks */ +static int msb_reset (struct msb_data *msb,bool full) +{ + +bool was_parallel = msb->regs.param.system &MEMSTICK_SYS_PAM ;
fyi, alas, this is (probably) slower than using an `int'. gcc will convert the result of the expression into 1-or-0.
Quoted 45 lines
drivers/memstick/core/ms_block.c new file +42
-0+struct memstick_dev *card = msb->card; +struct memstick_host *host = card->host; +int error; + +/* Reset the card */ + msb->regs.param.system =MEMSTICK_SYS_BAMD ; + +if (full) { + error = host->set_param (host, +MEMSTICK_POWER ,MEMSTICK_POWER_OFF ); +if (error) + goto out_error; + +msb_invalidate_reg_window (msb); + + error = host->set_param (host, +MEMSTICK_POWER ,MEMSTICK_POWER_ON ); +if (error) + goto out_error; + + error = host->set_param (host, +MEMSTICK_INTERFACE ,MEMSTICK_SERIAL ); +if (error) { +static int msb_erase_block (struct msb_data *msb,u16 pba) +{ +int error, try; +if (msb->read_only) +return -EROFS ; + +dbg_verbose ("erasing pba %d" , pba); + +for (try =1 ; try <3 ; try++) { + msb->regs.param.block_address =cpu_to_be16 (pba); + msb->regs.param.page_address =0 ; + msb->regs.param.cp =MEMSTICK_CP_BLOCK ; + msb->command_value =MS_CMD_BLOCK_ERASE ; + msb->command_need_oob = false; + + + error =msb_run_state_machine (msb, h_msb_send_command); +if (!error ||msb_reset (msb, true)) +break ;
drivers/memstick/core/ms_block.c line 851
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ } + +if (error) { +ms_printk ("erase failed, marking pba %d as bad" , pba); +msb_mark_bad (msb, pba); + } + +dbg_verbose ("erase success, marking pba %d as unused" , pba); +msb_mark_block_unused (msb, pba); +set_bit (pba, msb->erased_blocks_bitmap);
Should be able to use __set_bit() here, as msb_mark_block_unused() required caller-provided locking.
drivers/memstick/core/ms_block.c line 952
Quoted 12 lines
drivers/memstick/core/ms_block.c new file +9
-0+return error; +} + +static int msb_read_oob (struct msb_data *msb,u16 pba,u16 page, +struct ms_extra_data_register *extra) +{ +int error; +BUG_ON (!extra); +
vertical whitespace oddnesses
Quoted 28 lines
drivers/memstick/core/ms_block.c new file +28
-0+ msb->regs.param.block_address =cpu_to_be16 (pba); + msb->regs.param.page_address = page; + msb->regs.param.cp =MEMSTICK_CP_EXTRA ; + +if (pba > msb->block_count) { +ms_printk ("BUG: attempt to read beyond" +" the end of card at pba %d" , pba); +return -EINVAL ; + } + + error =msb_run_state_machine (msb, h_msb_read_page); + *extra = msb->regs.extra_data; + +if (error == -EUCLEAN ) { +ms_printk ("correctable error on pba %d, page %d" , + pba, page); +return 0 ; + } + +return error; +} + + +/* Reads a block and compares it with data contained in scatterlist orig_sg */ +static bool msb_verify_block (struct msb_data *msb,u16 pba, +struct scatterlist *orig_sg,int offset) +{ +struct scatterlist sg;
drivers/memstick/core/ms_block.c line 990
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+int page =0 , error; + +sg_init_one (&sg, msb->block_buffer, msb->block_size); + +while (page < msb->pages_in_block) { + + error =msb_read_page (msb, pba, page, +NULL , &sg, page * msb->page_size); +if (error) +return -EIO ;
msb_read_page() returned an errno. Propagate that, rather than overwriting it?
drivers/memstick/core/ms_block.c
Quoted 13 lines
drivers/memstick/core/ms_block.c new file +10
-0+ page++; + } + +if (sg_compare_to_buffer (orig_sg, offset, + msb->block_buffer, msb->block_size)) +return -EIO ; +return 0 ; +} + +static int msb_read_bad_block_table (struct msb_data *msb,int block_nr)
hm. Should block_nr be a sector_t?
drivers/memstick/core/ms_block.c line 1264
Quoted 9 lines
drivers/memstick/core/ms_block.c new file +9
-0+{ +struct ms_boot_page *boot_block; +struct scatterlist sg; +u16 *buffer =NULL ; +int offset =0 ; + +int i, error =0 ; +int data_size, data_offset, page, page_offset, size_to_read; +u16 pba;
vertical whitespace oddness
drivers/memstick/core/ms_block.c line 1609
Quoted 13 lines
drivers/memstick/core/ms_block.c new file +10
-0+BUG_ON (block_nr >1 ); + + boot_block = &msb->boot_page[block_nr]; + pba = msb->boot_block_locations[block_nr]; + +static int msb_cache_write (struct msb_data *msb,int lba, +int page,bool add_to_cache_only,struct scatterlist *sg,int offset) +{ +int error; +struct scatterlist sg_tmp[10 ];
newline..
Quoted 34 lines
drivers/memstick/core/ms_block.c new file +34
-0+if (msb->read_only) +return -EROFS ; + +if (msb->cache_block_lba ==MS_BLOCK_INVALID || + lba != msb->cache_block_lba) +if (add_to_cache_only) +return 0 ; + +/* If we need to write different block */ +if (msb->cache_block_lba !=MS_BLOCK_INVALID && + lba != msb->cache_block_lba) { +dbg_verbose ("first flush the cache" ); + error =msb_cache_flush (msb); +if (error) +return error; + } + +if (msb->cache_block_lba ==MS_BLOCK_INVALID ) { + msb->cache_block_lba = lba; +mod_timer (&msb->cache_flush_timer, + jiffies +msecs_to_jiffies (cache_flush_timeout)); + } + +dbg_verbose ("Write of LBA %d page %d to cache " , lba, page); + +sg_init_table (sg_tmp,ARRAY_SIZE (sg_tmp)); +sg_copy (sg, sg_tmp,ARRAY_SIZE (sg_tmp), offset, msb->page_size); + +sg_copy_to_buffer (sg_tmp,sg_nents (sg_tmp), + msb->cache + page * msb->page_size, msb->page_size); + +set_bit (page, &msb->valid_cache_bitmap); +return 0 ; +}
drivers/memstick/core/ms_block.c
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +7
-0+ +static int msb_do_write_request (struct msb_data *msb,int lba, +int page,struct scatterlist *sg,int len,int *sucessfuly_written) +{ +int error =0 ; +int offset =0 ; + *sucessfuly_written =0 ;
(more vertical whitespace glitches)
Again the types look a bit wrong. I'd have expected len' and offset' to be size_t.
Quoted 27 lines
drivers/memstick/core/ms_block.c new file +27
-0+while (offset < len) { +if (page ==0 && len - offset >= msb->block_size) { + +if (msb->cache_block_lba == lba) +msb_cache_discard (msb); + +dbg_verbose ("Writing whole lba %d" , lba); + error =msb_update_block (msb, lba, sg, offset); +if (error) +return error; + + offset += msb->block_size; + *sucessfuly_written += msb->block_size; + lba++; +continue ; + } + + error =msb_cache_write (msb, lba, page, false, sg, offset); +if (error) +return error; + + offset += msb->page_size; + *sucessfuly_written += msb->page_size; + + page++; +if (page == msb->pages_in_block) { + page =0 ;
drivers/memstick/core/ms_block.c
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +7
-0+ lba++; + } + } +return 0 ; +} +static DEFINE_IDR (msb_disk_idr); +static DEFINE_MUTEX (msb_disk_lock);
A little comment which describes the overall role of msb_disk_idr wold be nice.
Quoted 3 lines
...
Maxim Levitsky
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 11 lines · Andrew Morton
Quoted 8 lines · On Wed, 19 Sep 2012 16:19:02 +0300 Maxim Levitsky <[email protected]>…
Based partially on MS standard spec quotes from Alex Dubov.
As any code that works with user data this driver isn't recommended to use to write cards that contain valuable data.
It tries its best though to avoid data corruption
and possible damage to the card.
Quoted 8 lines
Quoted 2 lines
Tested on MS DUO 64 MB card on Ricoh R592 card reader..
Generally looks nice to me, although I wouldn't know a memstick if one stuck to my shoe.
I have a bunch of fairly trivial comments, and one head-scratcher for
Tejun: what's up with the local-irqs-disabled requirement insg_miter_next()?
Awesome, thanks for the review!
I will address all comments very soon.
Quoted 17 lines
drivers/memstick/core/ms_block.c line 46
Quoted 13 lines
...
drivers/memstick/core/ms_block.c new file +10
-0+static int sg_nents (struct scatterlist *sg) +{ +int nents =0 ; +while (sg) { + nents++; + sg =sg_next (sg); + } + +return nents; +}I think we may as well put this in
scatterlist.cimmediately. It wouldn't surprise me if we already have open-coded copies of this lying around the tree.
I agree, but as usual afraid that it wont be accepted here.
Quoted 11 lines
drivers/memstick/core/ms_block.c line 53
Quoted 7 lines
drivers/memstick/core/ms_block.c new file +6
-0+/* +* Copies section of 'sg_from' starting from offset 'offset' and with length +* 'len' To another scatterlist of to_nents enties +*/ +static int sg_copy (struct scatterlist *sg_from,struct scatterlist *sg_to, +int to_nents,int offset,size_t len)The should return a
size_t, to match the type oflen'. And the type ofoffset' is fishy - there's nothing which intrinsically limits these things to 2G?
In my case, I will never have large offsets here, but I agree with you completely.
Quoted 11 lines
Quoted 11 lines
drivers/memstick/core/ms_block.c new file +10
-0+{ +int copied =0 ; + +while (offset >0 ) { + +if (offset >= sg_from->length) { +if (sg_is_last (sg_from)) +return 0 ; + + offset -= sg_from->length;
Quoted 8 lines
drivers/memstick/core/ms_block.c line 68
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +5
-0+ sg_from =sg_next (sg_from); +continue ; + } + + copied =min (len, (size_t )(sg_from->length - offset));
min_twould be more conventional. Or perhaps evenmin(), depending on `offset's new type (but probably not).
Agree.
Quoted 109 lines
Quoted 33 lines
drivers/memstick/core/ms_block.c new file +32
-0+sg_set_page (sg_to,sg_page (sg_from), + copied, sg_from->offset + offset); + + len -= copied; + offset =0 ; + +if (sg_is_last (sg_from) || !len) + goto out; + + sg_to =sg_next (sg_to); + to_nents--; + sg_from =sg_next (sg_from); + } + +while (len > sg_from->length && to_nents--) { + + len -= sg_from->length; + copied += sg_from->length; + +sg_set_page (sg_to,sg_page (sg_from), + sg_from->length, sg_from->offset); + +if (sg_is_last (sg_from) || !len) + goto out; + + sg_from =sg_next (sg_from); + sg_to =sg_next (sg_to); + } + +if (len && to_nents) { +sg_set_page (sg_to,sg_page (sg_from), len, sg_from->offset); + copied += len;drivers/memstick/core/ms_block.c line 110
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ } + +out: +sg_mark_end (sg_to); +return copied; +} + +/* +* Compares section of 'sg' starting from offset 'offset' and with length 'len' +* to linear buffer of length 'len' at address 'buffer' The comment should document the return value.
drivers/memstick/core/ms_block.c line 119
Quoted 9 lines
+ */
drivers/memstick/core/ms_block.c new file +8
-0+static bool sg_compare_to_buffer (struct scatterlist *sg, +int offset,u8 *buffer,size_t len) +{ +unsigned long flags; +int retval =0 ; +struct sg_mapping_iter miter; + +local_irq_save (flags);hm, the "IRQ disabled if
SG_MITER_ATOMIC" requirement ofsg_miter_next()is weird and unexpected.Tejun's
137d3edb48425f8added the code whch has this strange requirement, but it is unexplained. Wazzup?Quoted 47 lines
drivers/memstick/core/ms_block.c new file +44
-0+sg_miter_start (&miter, sg,sg_nents (sg), +SG_MITER_ATOMIC |SG_MITER_FROM_SG ); + +while (sg_miter_next (&miter) && len >0 ) { + +int cmplen; + +if (offset >= miter.length) { + offset -= miter.length; +continue ; + } + + cmplen =min (miter.length - offset, len); + retval =memcmp (miter.addr + offset, buffer, cmplen); +if (retval) +break ; + + buffer += cmplen; + len -= cmplen; + offset =0 ; + } + +if (!retval && len) + retval =-1 ; + +sg_miter_stop (&miter); +local_irq_restore (flags); +return retval; +} +static void msb_mark_block_unused (struct msb_data *msb,int pba) +{ +int zone =msb_get_zone_from_pba (pba); + +if (!test_bit (pba, msb->used_blocks_bitmap)) { +ms_printk ("BUG: attempt to mark " +"already unused pba %d as unused" , pba); + msb->read_only = true; +return ; + } + +#ifdef DEBUG +if (msb_validate_used_block_bitmap (msb)) +return ; +#endif
Quoted 8 lines
drivers/memstick/core/ms_block.c line 228
Quoted 3 lines
drivers/memstick/core/ms_block.c new file +3
-0+clear_bit (pba, msb->used_blocks_bitmap); + msb->free_block_count[zone]++; +}Presumably there is some lock which the caller must hold to prevent the obvious races in the above two functions. I suggest adding a comment whcih documents this requirement, then take advantage of that lock to switch to the more efficient
__set_bit()and__clear_bit().
There can't be races in the driver, since it contains a single thread that does all the IO it got from block layer.
The thread is awaken each time the request function of block device is called.
This why I didn't do much locking in here. In addition I found out that this is quite common way to implement a block device driver.
Quoted 275 lines
Quoted 2 lines
+/* Invalidate current register window*/
hm, checkpatch misses the missing space before */
Quoted 22 lines
drivers/memstick/core/ms_block.c new file +22
-0+static void msb_invalidate_reg_window (struct msb_data *msb) +{ + msb->reg_addr.w_offset = offsetof(struct ms_register , id); + msb->reg_addr.w_length =sizeof (struct ms_id_register ); + msb->reg_addr.r_offset = offsetof(struct ms_register , id); + msb->reg_addr.r_length =sizeof (struct ms_id_register ); + msb->addr_valid = false; +} + +/* Sane way to start a state machine*/ +static int msb_run_state_machine (struct msb_data *msb,int (*state_func) + (struct memstick_dev *card,struct memstick_request **req)) +{ +struct memstick_dev *card = msb->card; + +WARN_ON (msb->state !=-1 ); + msb->int_polling = false; + msb->state =0 ; + msb->exit_error =0 ; + +memset (&card->current_mrq,0 ,sizeof (card->current_mrq)); +drivers/memstick/core/ms_block.c line 262
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ card->next_request = state_func; +memstick_new_req (card->host); +wait_for_completion (&card->mrq_complete); + +WARN_ON (msb->state !=-1 ); +return msb->exit_error; +} + +/* State machines call that to exit */ +int msb_exit_state_machine (struct msb_data *msb,int error)static
Quoted 7 lines
drivers/memstick/core/ms_block.c new file +7
-0+{ +WARN_ON (msb->state ==-1 ); + + msb->state =-1 ; + msb->exit_error = error; + msb->card->next_request = h_msb_default_bad; +drivers/memstick/core/ms_block.c line 279
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+/* Invalidate reg window on errors */ +if (error) +msb_invalidate_reg_window (msb); + +complete (&msb->card->mrq_complete); +return -ENXIO ; +} + +/* read INT register */ +int msb_read_int_reg (struct msb_data *msb,long timeout)static
drivers/memstick/core/ms_block.c line 282
Quoted 3 lines
drivers/memstick/core/ms_block.c new file +3
-0+{ +struct memstick_request *mrq = &msb->card->current_mrq; +WARN_ON (msb->state ==-1 );It's conventional to put a newline between end-of-locals and start-of-code.
Quoted 12 lines
drivers/memstick/core/ms_block.c new file +12
-0+if (!msb->int_polling) { + msb->int_timeout = jiffies + +msecs_to_jiffies (timeout ==-1 ?500 : timeout); + msb->int_polling = true; + }else if (time_after (jiffies, msb->int_timeout)) { + mrq->data[0 ] =MEMSTICK_INT_CMDNAK ; +return 0 ; + } + +if ((msb->caps &MEMSTICK_CAP_AUTO_GET_INT ) && + mrq->need_card_int && !mrq->error) { + mrq->data[0 ] = mrq->int_reg;drivers/memstick/core/ms_block.c line 305
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ mrq->need_card_int = false; +return 0 ; + }else { +memstick_init_req (mrq,MS_TPC_GET_INT ,NULL ,1 ); +return 1 ; + } +} + +/* Read a register */ +int msb_read_regs (struct msb_data *msb,int offset,int len)static. I'll stop checking them now ;)
Quoted 31 lines
drivers/memstick/core/ms_block.c new file +31
-0+{ +struct memstick_request *req = &msb->card->current_mrq; + +if (msb->reg_addr.r_offset != offset || + msb->reg_addr.r_length != len || !msb->addr_valid) { + + msb->reg_addr.r_offset = offset; + msb->reg_addr.r_length = len; + msb->addr_valid = true; + +memstick_init_req (req,MS_TPC_SET_RW_REG_ADRS , + &msb->reg_addr,sizeof (msb->reg_addr)); +return 0 ; + } + +memstick_init_req (req,MS_TPC_READ_REG ,NULL , len); +return 1 ; +} + +/* Write a card register */ +int msb_write_regs (struct msb_data *msb,int offset,int len,void *buf) +{ +struct memstick_request *req = &msb->card->current_mrq; + +if (msb->reg_addr.w_offset != offset || + msb->reg_addr.w_length != len || !msb->addr_valid) { + + msb->reg_addr.w_offset = offset; + msb->reg_addr.w_length = len; + msb->addr_valid = true; +drivers/memstick/core/ms_block.c line 346
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+memstick_init_req (req,MS_TPC_SET_RW_REG_ADRS , + &msb->reg_addr,sizeof (msb->reg_addr)); +return 0 ; + } + +memstick_init_req (req,MS_TPC_WRITE_REG , buf, len); +return 1 ; +} + +/* Handler for absense of IO */ "absence" :)
Quoted 118 lines
drivers/memstick/core/ms_block.c new file +118
-0+static int h_msb_default_bad (struct memstick_dev *card, +struct memstick_request **mrq) +{ +return -ENXIO ; +} + +/* +* This function is a handler for reads of one page from device. +* Writes output to msb->current_sg, takes sector address from msb->reg.param +* Can also be used to read extra data only. Set params accordintly. +*/ +static int h_msb_read_page (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +struct scatterlist sg[2 ]; +u8 command, intreg; + +if (mrq->error) { +dbg ("read_page, unknown error" ); +return msb_exit_state_machine (msb, mrq->error); + } +again: +switch (msb->state) { +case 0 :/* Write the sector address */ +if (!msb_write_regs (msb, + offsetof(struct ms_register , param), +sizeof (struct ms_param_register ), + (unsigned char *)&msb->regs.param)) +return 0 ; +break ; + +case 1 :/* Execute the read command*/ + command =MS_CMD_BLOCK_READ ; +memstick_init_req (mrq,MS_TPC_SET_CMD , &command,1 ); +break ; + +case 2 :/* send INT request */ +if (msb_read_int_reg (msb,-1 )) +break ; + msb->state++; + +case 3 :/* get result of the INT request*/ + intreg = mrq->data[0 ]; + msb->regs.status.interrupt = intreg; + +if (intreg &MEMSTICK_INT_CMDNAK ) +return msb_exit_state_machine (msb, -EIO ); + +if (!(intreg &MEMSTICK_INT_CED )) { + msb->state--; + goto again; + } + + msb->int_polling = false; + +if (intreg &MEMSTICK_INT_ERR ) + msb->state++; +else + msb->state =6 ; + + goto again; + +case 4 :/* read the status register +to understand source of the INT_ERR */ +if (!msb_read_regs (msb, + offsetof(struct ms_register , status), +sizeof (struct ms_status_register ))) +return 0 ; +break ; + +case 5 :/* get results of status check */ + msb->regs.status = *(struct ms_status_register *)mrq->data; + msb->state++; + +case 6 :/* Send extra data read request */ +if (!msb_read_regs (msb, + offsetof(struct ms_register , extra_data), +sizeof (struct ms_extra_data_register ))) +return 0 ; +break ; + +case 7 :/* Save result of extra data request */ + msb->regs.extra_data = + *(struct ms_extra_data_register *) mrq->data; + msb->state++; + +case 8 :/* Send the MS_TPC_READ_LONG_DATA to read IO buffer */ + +/* Skip that state if we only read the oob */ +if (msb->regs.param.cp ==MEMSTICK_CP_EXTRA ) { + msb->state++; + goto again; + } + +sg_init_table (sg,ARRAY_SIZE (sg)); +sg_copy (msb->current_sg, sg,ARRAY_SIZE (sg), + msb->current_sg_offset, + msb->page_size); + +memstick_init_req_sg (mrq,MS_TPC_READ_LONG_DATA , sg); +break ; + +case 9 :/* check validity of data buffer & done */ + +if (!(msb->regs.status.interrupt &MEMSTICK_INT_ERR )) { + msb->current_sg_offset += msb->page_size; +return msb_exit_state_machine (msb,0 ); + } + +if (msb->regs.status.status1 &MEMSTICK_UNCORR_ERROR ) { +dbg ("read_page: uncorrectable error" ); +return msb_exit_state_machine (msb, -EBADMSG ); + } + +if (msb->regs.status.status1 &MEMSTICK_CORR_ERROR ) { +dbg ("read_page: correctable error" );drivers/memstick/core/ms_block.c line 474
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+ msb->current_sg_offset += msb->page_size; +return msb_exit_state_machine (msb, -EUCLEAN ); + }else { +dbg ("read_page: INT error, but no status error bits" ); +return msb_exit_state_machine (msb, -EIO ); + } +default : +BUG (); + } + msb->state++;I think this code would be quite a lot cleaner, safer and more readable if you were to do away with the "msb->state++" concept altogether. Just do
enum msb_states { state_foo1, state_foo2 ... };and do
Quoted 7 lines
msb->state = state_foo2;in each switch case.
This way, the reader doesn't have to scratch his head over what the state _used_ to be, and the states have nice names to help the reader understand what's going on.
I now remember hearing about that back then too, and on second thought, I'll do that.
Quoted 78 lines
Quoted 7 lines
drivers/memstick/core/ms_block.c new file +2
-0+return 0 ; +}drivers/memstick/core/ms_block.c line 596
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+/* +* This function is used to send simple IO requests to device that consist +* of register write + command +*/ +static int h_msb_send_command (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +stray newline
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +10
-0+u8 intreg; + +if (mrq->error) { +dbg ("send_command: unknown error" ); +return msb_exit_state_machine (msb, mrq->error); + } +again: +switch (msb->state) { + +/* HACK: see h_msb_write_block */ drivers/memstick/core/ms_block.c line 665
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +7
-0+ +static int h_msb_parallel_switch (struct memstick_dev *card, +struct memstick_request **out_mrq) +{ +struct msb_data *msb =memstick_get_drvdata (card); +struct memstick_request *mrq = *out_mrq = &card->current_mrq; +stray newline. Please fix 'em up - it does make the code a little discordant to read.
Quoted 34 lines
drivers/memstick/core/ms_block.c new file +34
-0+struct memstick_host *host = card->host; + +if (mrq->error) { +dbg ("parallel_switch: error" ); + msb->regs.param.system &= ~MEMSTICK_SYS_PAM ; +return msb_exit_state_machine (msb, mrq->error); + } + +switch (msb->state) { +case 0 :/* Set the parallel interface on memstick side */ + msb->regs.param.system |=MEMSTICK_SYS_PAM ; + +if (!msb_write_regs (msb, + offsetof(struct ms_register , param), +1 , + (unsigned char *)&msb->regs.param)) +return 0 ; +break ; + +case 1 :/* Set parallel interface on our side + send a dummy request +to see if card responds */ + host->set_param (host,MEMSTICK_INTERFACE ,MEMSTICK_PAR4 ); +memstick_init_req (mrq,MS_TPC_GET_INT ,NULL ,1 ); +break ; + +case 2 : +return msb_exit_state_machine (msb,0 ); + } + msb->state++; +return 0 ; +} + +static int msb_switch_to_parallel (struct msb_data *msb); +
Quoted 8 lines
drivers/memstick/core/ms_block.c line 726
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +5
-0+/* Reset the card, to guard against hw errors beeing treated as bad blocks */ +static int msb_reset (struct msb_data *msb,bool full) +{ + +bool was_parallel = msb->regs.param.system &MEMSTICK_SYS_PAM ;fyi, alas, this is (probably) slower than using an `int'. gcc will convert the result of the expression into 1-or-0.
Didn't knew about that. Here this code is executed once per card insert, so it sure doesn't matter.
Quoted 51 lines
Quoted 51 lines
drivers/memstick/core/ms_block.c new file +47
-0+struct memstick_dev *card = msb->card; +struct memstick_host *host = card->host; +int error; + +/* Reset the card */ + msb->regs.param.system =MEMSTICK_SYS_BAMD ; + +if (full) { + error = host->set_param (host, +MEMSTICK_POWER ,MEMSTICK_POWER_OFF ); +if (error) + goto out_error; + +msb_invalidate_reg_window (msb); + + error = host->set_param (host, +MEMSTICK_POWER ,MEMSTICK_POWER_ON ); +if (error) + goto out_error; + + error = host->set_param (host, +MEMSTICK_INTERFACE ,MEMSTICK_SERIAL ); +if (error) { +static int msb_erase_block (struct msb_data *msb,u16 pba) +{ +int error, try; +if (msb->read_only) +return -EROFS ; + +dbg_verbose ("erasing pba %d" , pba); + +for (try =1 ; try <3 ; try++) { + msb->regs.param.block_address =cpu_to_be16 (pba); + msb->regs.param.page_address =0 ; + msb->regs.param.cp =MEMSTICK_CP_BLOCK ; + msb->command_value =MS_CMD_BLOCK_ERASE ; + msb->command_need_oob = false; + + + error =msb_run_state_machine (msb, h_msb_send_command); +if (!error ||msb_reset (msb, true)) +break ; + } + +if (error) { +ms_printk ("erase failed, marking pba %d as bad" , pba); +msb_mark_bad (msb, pba);
Quoted 8 lines
drivers/memstick/core/ms_block.c line 851
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +5
-0+ } + +dbg_verbose ("erase success, marking pba %d as unused" , pba); +msb_mark_block_unused (msb, pba); +set_bit (pba, msb->erased_blocks_bitmap);Should be able to use
__set_bit()here, asmsb_mark_block_unused()required caller-provided locking.
Again, I think I don't need locking here.
Quoted 49 lines
drivers/memstick/core/ms_block.c line 952
Quoted 13 lines
drivers/memstick/core/ms_block.c new file +9
-0+return error; +} + +static int msb_read_oob (struct msb_data *msb,u16 pba,u16 page, +struct ms_extra_data_register *extra) +{ +int error; +BUG_ON (!extra); +vertical whitespace oddnesses
Quoted 33 lines
drivers/memstick/core/ms_block.c new file +33
-0+ msb->regs.param.block_address =cpu_to_be16 (pba); + msb->regs.param.page_address = page; + msb->regs.param.cp =MEMSTICK_CP_EXTRA ; + +if (pba > msb->block_count) { +ms_printk ("BUG: attempt to read beyond" +" the end of card at pba %d" , pba); +return -EINVAL ; + } + + error =msb_run_state_machine (msb, h_msb_read_page); + *extra = msb->regs.extra_data; + +if (error == -EUCLEAN ) { +ms_printk ("correctable error on pba %d, page %d" , + pba, page); +return 0 ; + } + +return error; +} + + +/* Reads a block and compares it with data contained in scatterlist orig_sg */ +static bool msb_verify_block (struct msb_data *msb,u16 pba, +struct scatterlist *orig_sg,int offset) +{ +struct scatterlist sg; +int page =0 , error; + +sg_init_one (&sg, msb->block_buffer, msb->block_size); + +while (page < msb->pages_in_block) {
Quoted 8 lines
drivers/memstick/core/ms_block.c line 990
Quoted 5 lines
drivers/memstick/core/ms_block.c new file +5
-0+ + error =msb_read_page (msb, pba, page, +NULL , &sg, page * msb->page_size); +if (error) +return -EIO ;
msb_read_page()returned an errno. Propagate that, rather than overwriting it?
Sure!
Quoted 11 lines
Quoted 11 lines
drivers/memstick/core/ms_block.c new file +9
-0+ page++; + } + +if (sg_compare_to_buffer (orig_sg, offset, + msb->block_buffer, msb->block_size)) +return -EIO ; +return 0 ; +} +
Quoted 5 lines
Quoted 3 lines
...
+static int
msb_read_bad_block_table(struct msb_data *msb, int block_nr)hm. Should
block_nrbe asector_t?
Actually no, as its an index in internal array of 'boot blocks', aka superblocks and there should be just two of them msb_read_boot_blocks reads them.
Each of boot blocks contains a pointer to bad blocks table.
Quoted 113 lines
drivers/memstick/core/ms_block.c line 1264
Quoted 11 lines
drivers/memstick/core/ms_block.c new file +9
-0+{ +struct ms_boot_page *boot_block; +struct scatterlist sg; +u16 *buffer =NULL ; +int offset =0 ; + +int i, error =0 ; +int data_size, data_offset, page, page_offset, size_to_read; +u16 pba;vertical whitespace oddness
drivers/memstick/core/ms_block.c line 1609
Quoted 13 lines
drivers/memstick/core/ms_block.c new file +10
-0+BUG_ON (block_nr >1 ); + + boot_block = &msb->boot_page[block_nr]; + pba = msb->boot_block_locations[block_nr]; + +static int msb_cache_write (struct msb_data *msb,int lba, +int page,bool add_to_cache_only,struct scatterlist *sg,int offset) +{ +int error; +struct scatterlist sg_tmp[10 ];newline..
Quoted 34 lines
drivers/memstick/core/ms_block.c new file +34
-0+if (msb->read_only) +return -EROFS ; + +if (msb->cache_block_lba ==MS_BLOCK_INVALID || + lba != msb->cache_block_lba) +if (add_to_cache_only) +return 0 ; + +/* If we need to write different block */ +if (msb->cache_block_lba !=MS_BLOCK_INVALID && + lba != msb->cache_block_lba) { +dbg_verbose ("first flush the cache" ); + error =msb_cache_flush (msb); +if (error) +return error; + } + +if (msb->cache_block_lba ==MS_BLOCK_INVALID ) { + msb->cache_block_lba = lba; +mod_timer (&msb->cache_flush_timer, + jiffies +msecs_to_jiffies (cache_flush_timeout)); + } + +dbg_verbose ("Write of LBA %d page %d to cache " , lba, page); + +sg_init_table (sg_tmp,ARRAY_SIZE (sg_tmp)); +sg_copy (sg, sg_tmp,ARRAY_SIZE (sg_tmp), offset, msb->page_size); + +sg_copy_to_buffer (sg_tmp,sg_nents (sg_tmp), + msb->cache + page * msb->page_size, msb->page_size); + +set_bit (page, &msb->valid_cache_bitmap); +return 0 ; +}drivers/memstick/core/ms_block.c
Quoted 10 lines
drivers/memstick/core/ms_block.c new file +7
-0+ +static int msb_do_write_request (struct msb_data *msb,int lba, +int page,struct scatterlist *sg,int len,int *sucessfuly_written) +{ +int error =0 ; +int offset =0 ; + *sucessfuly_written =0 ;(more vertical whitespace glitches)
Again the types look a bit wrong. I'd have expected
len' andoffset' to besize_t.Quoted 33 lines
drivers/memstick/core/ms_block.c new file +32
-0+while (offset < len) { +if (page ==0 && len - offset >= msb->block_size) { + +if (msb->cache_block_lba == lba) +msb_cache_discard (msb); + +dbg_verbose ("Writing whole lba %d" , lba); + error =msb_update_block (msb, lba, sg, offset); +if (error) +return error; + + offset += msb->block_size; + *sucessfuly_written += msb->block_size; + lba++; +continue ; + } + + error =msb_cache_write (msb, lba, page, false, sg, offset); +if (error) +return error; + + offset += msb->page_size; + *sucessfuly_written += msb->page_size; + + page++; +if (page == msb->pages_in_block) { + page =0 ; + lba++; + } + } +return 0 ; +}
Quoted 7 lines
drivers/memstick/core/ms_block.c line 1967
Quoted 4 lines
...
drivers/memstick/core/ms_block.c new file +2
-0+static DEFINE_IDR (msb_disk_idr); +static DEFINE_MUTEX (msb_disk_lock);A little comment which describes the overall role of
msb_disk_idrwold be nice.
Sure!
The things I didn't comment on I agree too, I will fix all these non-static functions and newlines. Newlines I put mostly to make the code a bit more clear to myself, but I really don't mind removing these.
Thanks again for a review, I will address all the issues very soon.
Signature · 2 lines
Best regards,
Maxim LevitskyRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 6 lines · Maxim Levitsky
There can't be races in the driver, since it contains a single thread that does all the IO it got from block layer.
The thread is awaken each time the request function of block device is called.
This why I didn't do much locking in here. In addition I found out that this is quite common way to implement a block device driver.
Please use workqueue instead of raw kthread.
Thanks!
Signature · 1 line
tejun
Maxim Levitsky
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 11 lines · Tejun Heo
Quoted 6 lines · Maxim Levitsky
There can't be races in the driver, since it contains a single thread that does all the IO it got from block layer.
The thread is awaken each time the request function of block device is called.
This why I didn't do much locking in here. In addition I found out that this is quite common way to implement a block device driver.Please use workqueue instead of raw kthread.
Thanks!
Now that my exams done....
Can you spare me from using a workqueue?
The point is that using current model I wake the worker thread as much as I want to, and I know that it will be woken once an will do all the work till request queue is empty.
With workqueues, it doesn't work this way. I have to pass the request as a work item or something like that.
Any pointers?
Best regards,
Maxim Levitsky
Maxim Levitsky
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 12 lines · Maxim Levitsky
Quoted 11 lines · Tejun Heo
Quoted 6 lines · Maxim Levitsky
There can't be races in the driver, since it contains a single thread that does all the IO it got from block layer.
The thread is awaken each time the request function of block device is called.
This why I didn't do much locking in here. In addition I found out that this is quite common way to implement a block device driver.Please use workqueue instead of raw kthread.
Thanks!
Quoted 8 lines
Now that my exams done....
Can you spare me from using a workqueue?
The point is that using current model I wake the worker thread as much as I want to, and I know that it will be woken once an will do all the work till request queue is empty.
With workqueues, it doesn't work this way. I have to pass the request as a work item or something like that.
Any pointers?
Also probably due to that reason MMC doesn't use a workqueue ether, but a raw kthread, in pretty much same way I do.
Signature · 2 lines
Best regards,
Maxim LevitskyRe: [PATCH] memstick: add support for legacy memorysticks
Hello,
Quoted 2 lines · Maxim Levitsky
Quoted 2 lines
Now that my exams done....
Can you spare me from using a workqueue?
I'd much prefer if you convert to workqueue.
Quoted 3 lines
Quoted 3 lines
The point is that using current model I wake the worker thread as much as I want to, and I know that it will be woken once an will do all the work till request queue is empty.
You can do exactly the same thing by scheduling the same work item multiple times. "Waking up" just becomes "scheduling the work item".
Quoted 3 lines
Quoted 3 lines
With workqueues, it doesn't work this way. I have to pass the request as a work item or something like that.
Any pointers?
No, there's no reason to change the structure of the code in any way. Just use a work item as you would use a kthread.
Quoted 2 lines
Also probably due to that reason MMC doesn't use a workqueue ether, but a raw kthread, in pretty much same way I do.
Mostly because I haven't gotten around to convert it yet. The problems with direct kthread usage are that they're much more difficult to get completely correct with freeze and exit conditions - the last time I checked it was easier to spot broken ones than correct ones - and they create dedicated threads which usually are underutilized.
Thanks.
Signature · 1 line
tejun
Maxim Levitsky
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 6 lines · Tejun Heo
Hello,
Quoted 2 lines · Maxim Levitsky
Quoted 2 lines
Now that my exams done....
Can you spare me from using a workqueue?
Quoted 8 lines
I'd much prefer if you convert to workqueue.
Quoted 3 lines
Quoted 3 lines
The point is that using current model I wake the worker thread as much as I want to, and I know that it will be woken once an will do all the work till request queue is empty.
You can do exactly the same thing by scheduling the same work item multiple times. "Waking up" just becomes "scheduling the work item".
I don't believe that will work this way.
I will dig through the source, and see how to do that.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
With workqueues, it doesn't work this way. I have to pass the request as a work item or something like that.
Any pointers?No, there's no reason to change the structure of the code in any way. Just use a work item as you would use a kthread.
Except that if I schedule a same work item few times, these work items will be 'processed' in parallel, although there is just one work to do, work of pulling the requests from block queue until it has them, and dispatching them through my code.
Or I can get a guarantee that work items wont be processed in parallel? Stiil, even with that only first work item will do the actual work, others will wake the workqueue for nothing, but I am ok with that.
Quoted 7 lines
Quoted 3 lines
Also probably due to that reason MMC doesn't use a workqueue ether, but a raw kthread, in pretty much same way I do.
Mostly because I haven't gotten around to convert it yet. The problems with direct kthread usage are that they're much more difficult to get completely correct with freeze and exit conditions -
Agreed, I had my own share of frustration with this. I got it right in the end though.
Quoted 3 lines
the last time I checked it was easier to spot broken ones than correct ones - and they create dedicated threads which usually are underutilized.
True too, although, a sleeping thread doesn't cost much.
Quoted 3 lines
Thanks.
Signature · 2 lines
Best regards,
Maxim LevitskyMaxim Levitsky
maintainerRe: [PATCH] memstick: add support for legacy memorysticks
Quoted 26 lines · Maxim Levitsky
Quoted 6 lines · Tejun Heo
Hello,
Quoted 2 lines · Maxim Levitsky
Quoted 2 lines
Now that my exams done....
Can you spare me from using a workqueue?Quoted 8 lines
I'd much prefer if you convert to workqueue.
Quoted 3 lines
Quoted 3 lines
The point is that using current model I wake the worker thread as much as I want to, and I know that it will be woken once an will do all the work till request queue is empty.
You can do exactly the same thing by scheduling the same work item multiple times. "Waking up" just becomes "scheduling the work item".
I don't believe that will work this way.
I will dig through the source, and see how to do that.Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
With workqueues, it doesn't work this way. I have to pass the request as a work item or something like that.
Any pointers?No, there's no reason to change the structure of the code in any way. Just use a work item as you would use a kthread.
Quoted 7 lines
Except that if I schedule a same work item few times, these work items will be 'processed' in parallel, although there is just one work to do, work of pulling the requests from block queue until it has them, and dispatching them through my code.
Or I can get a guarantee that work items wont be processed in parallel? Stiil, even with that only first work item will do the actual work, others will wake the workqueue for nothing, but I am ok with that.
Should have looked through the source. Understand now.
Just one quick question, should I create my own workqueue or use schedule_work? if I use the later and my work function sleeps, will it harmfully affect other users of this function?
Signature · 2 lines
Best regards,
Maxim LevitskyRe: [PATCH] memstick: add support for legacy memorysticks
Hello,
Quoted 4 lines · Maxim Levitsky
Should have looked through the source. Understand now.
Just one quick question, should I create my own workqueue or useschedule_work? if I use the later and my work function sleeps, will it harmfully affect other users of this function?
If memstick can be in memory reclaim path (most block devices are), you would want to create a dedicated workqueue w/ rescuer. For details, please read Documentation/workqueue.txt.
Thanks.
Signature · 1 line
tejun
Re: [PATCH] memstick: add support for legacy memorysticks
In reply to Maxim LevitskyHello,
Quoted 5 lines · Maxim Levitsky
Except that if I schedule a same work item few times, these work items will be 'processed' in parallel, although there is just one work to do, work of pulling the requests from block queue until it has them, and dispatching them through my code.
Or I can get a guarantee that work items wont be processed in parallel?
You need to use system_nrt_wq for that before 3.7-rc1. After 3.7-rc1, any workqueue will guarantee that.
Quoted 2 lines
Stiil, even with that only first work item will do the actual work, others will wake the workqueue for nothing, but I am ok with that.
It's just like waking up spuriously. The work item is guaranteed to be executed at least once after any given schedule/queue_work() call.
Thanks.
Signature · 1 line
tejun
Re: [PATCH] memstick: add support for legacy memorysticks
In reply to Andrew MortonHello, Andrew.
Quoted 15 lines · Andrew Morton
I have a bunch of fairly trivial comments, and one head-scratcher for
Tejun: what's up with the local-irqs-disabled requirement insg_miter_next()?Review
Quoted 8 lines
(quoted diff) +8
-0+static bool sg_compare_to_buffer(struct scatterlist *sg, + int offset, u8 *buffer, size_t len) +{ + unsigned long flags; + int retval = 0; + struct sg_mapping_iter miter; + + local_irq_save(flags);hm, the "IRQ disabled if
SG_MITER_ATOMIC" requirement ofsg_miter_next()is weird and unexpected.
Quoted 2 lines
Tejun's
137d3edb48425f8added the code whch has this strange requirement, but it is unexplained. Wazzup?
Heh, that's long time ago. Trying to remember... so it was written before kmap_atomic() is converted to stack based implementation and had to be supplied with the specific KMAP index. Atomic mapping iterator should be useable from irq context too (and there were other irq handler paths using KM_BIO_SRC_IRQ as the name implies), so it couldn't allow irq to happen while mapping is in use. At the time, having to disable IRQ to use any of KM_*_IRQs was rather self-evident.
I think we now can relax the requirement. All that's required is not to sleep or preempted while mapped. Something like the following is in order?
--------8<--------
Subject: scatterlist: atomic sg_mapping_iter no longer needs disabling IRQ
SG mapping iterator w/ SG_MITER_ATOMIC set required IRQ disabled because it originally used KM_BIO_SRC_IRQ to allow use from IRQ handlers. kmap_atomic() has long been updated to handle stacking atomic mapping requests on per-cpu basis and only requires not sleeping while mapped.
Update sg_mapping_iter such that atomic iterators only require disabling preemption instead of disabling IRQ.
While at it, convert wte weird @ARG@ notations to @ARG in the comment of sg_miter_start().
--- lib/scatterlist.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
lib/scatterlist.c +8 -8
@@ -404,14 +404,13 @@ EXPORT_SYMBOL(sg_miter_start);* @miter: sg mapping iter to proceed * * Description: -* Proceeds @miter@ to the next mapping. @miter@ should have been -* started using sg_miter_start(). On successful return, -* @miter@->page, @miter@->addr and @miter@->length point to the -* current mapping. +* Proceeds @miter to the next mapping. @miter should have been started +* using sg_miter_start(). On successful return, @miter->page, +* @miter->addr and @miter->length point to the current mapping. * * Context: -* IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till -* @miter@ is stopped. May sleep if !SG_MITER_ATOMIC. +* Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled +* till @miter is stopped. May sleep if !SG_MITER_ATOMIC. * * Returns: * true if @miter contains the next mapping. false if end of sg
@@ -465,7 +464,8 @@ EXPORT_SYMBOL(sg_miter_next);* resources (kmap) need to be released during iteration. * * Context: -* IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. +* Preemption disabled if the SG_MITER_ATOMIC is set. Don't care +* otherwise. */ void sg_miter_stop (struct sg_mapping_iter *miter) {
@@ -479,7 +479,7 @@ void sg_miter_stop(struct sg_mapping_iter *miter)flush_kernel_dcache_page (miter->page);if (miter->__flags &SG_MITER_ATOMIC ) { -WARN_ON (!irqs_disabled ()); +WARN_ON_ONCE (preemptible ());kunmap_atomic (miter->addr); }else kunmap (miter->page);