@@ -174,14 +174,17 @@ static int mmc_decode_csd(struct mmc_card *card)
174174}
175175
176176/*
177- * Read and decode extended CSD.
177+ * Read extended CSD.
178178 */
179- static int mmc_read_ext_csd (struct mmc_card * card )
179+ static int mmc_get_ext_csd (struct mmc_card * card , u8 * * new_ext_csd )
180180{
181181 int err ;
182182 u8 * ext_csd ;
183183
184184 BUG_ON (!card );
185+ BUG_ON (!new_ext_csd );
186+
187+ * new_ext_csd = NULL ;
185188
186189 if (card -> csd .mmca_vsn < CSD_SPEC_VER_4 )
187190 return 0 ;
@@ -199,12 +202,15 @@ static int mmc_read_ext_csd(struct mmc_card *card)
199202
200203 err = mmc_send_ext_csd (card , ext_csd );
201204 if (err ) {
205+ kfree (ext_csd );
206+ * new_ext_csd = NULL ;
207+
202208 /* If the host or the card can't do the switch,
203209 * fail more gracefully. */
204210 if ((err != - EINVAL )
205211 && (err != - ENOSYS )
206212 && (err != - EFAULT ))
207- goto out ;
213+ return err ;
208214
209215 /*
210216 * High capacity cards should have this "magic" size
@@ -222,9 +228,23 @@ static int mmc_read_ext_csd(struct mmc_card *card)
222228 mmc_hostname (card -> host ));
223229 err = 0 ;
224230 }
231+ } else
232+ * new_ext_csd = ext_csd ;
225233
226- goto out ;
227- }
234+ return err ;
235+ }
236+
237+ /*
238+ * Decode extended CSD.
239+ */
240+ static int mmc_read_ext_csd (struct mmc_card * card , u8 * ext_csd )
241+ {
242+ int err = 0 ;
243+
244+ BUG_ON (!card );
245+
246+ if (!ext_csd )
247+ return 0 ;
228248
229249 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
230250 if (card -> csd .structure == 3 ) {
@@ -372,8 +392,69 @@ static int mmc_read_ext_csd(struct mmc_card *card)
372392 card -> erased_byte = 0x0 ;
373393
374394out :
395+ return err ;
396+ }
397+
398+ static inline void mmc_free_ext_csd (u8 * ext_csd )
399+ {
375400 kfree (ext_csd );
401+ }
402+
376403
404+ static int mmc_compare_ext_csds (struct mmc_card * card , u8 * ext_csd ,
405+ unsigned bus_width )
406+ {
407+ u8 * bw_ext_csd ;
408+ int err ;
409+
410+ err = mmc_get_ext_csd (card , & bw_ext_csd );
411+ if (err )
412+ return err ;
413+
414+ if ((ext_csd == NULL || bw_ext_csd == NULL )) {
415+ if (bus_width != MMC_BUS_WIDTH_1 )
416+ err = - EINVAL ;
417+ goto out ;
418+ }
419+
420+ if (bus_width == MMC_BUS_WIDTH_1 )
421+ goto out ;
422+
423+ /* only compare read only fields */
424+ err = (!(ext_csd [EXT_CSD_PARTITION_SUPPORT ] ==
425+ bw_ext_csd [EXT_CSD_PARTITION_SUPPORT ]) &&
426+ (ext_csd [EXT_CSD_ERASED_MEM_CONT ] ==
427+ bw_ext_csd [EXT_CSD_ERASED_MEM_CONT ]) &&
428+ (ext_csd [EXT_CSD_REV ] ==
429+ bw_ext_csd [EXT_CSD_REV ]) &&
430+ (ext_csd [EXT_CSD_STRUCTURE ] ==
431+ bw_ext_csd [EXT_CSD_STRUCTURE ]) &&
432+ (ext_csd [EXT_CSD_CARD_TYPE ] ==
433+ bw_ext_csd [EXT_CSD_CARD_TYPE ]) &&
434+ (ext_csd [EXT_CSD_S_A_TIMEOUT ] ==
435+ bw_ext_csd [EXT_CSD_S_A_TIMEOUT ]) &&
436+ (ext_csd [EXT_CSD_HC_WP_GRP_SIZE ] ==
437+ bw_ext_csd [EXT_CSD_HC_WP_GRP_SIZE ]) &&
438+ (ext_csd [EXT_CSD_ERASE_TIMEOUT_MULT ] ==
439+ bw_ext_csd [EXT_CSD_ERASE_TIMEOUT_MULT ]) &&
440+ (ext_csd [EXT_CSD_HC_ERASE_GRP_SIZE ] ==
441+ bw_ext_csd [EXT_CSD_HC_ERASE_GRP_SIZE ]) &&
442+ (ext_csd [EXT_CSD_SEC_TRIM_MULT ] ==
443+ bw_ext_csd [EXT_CSD_SEC_TRIM_MULT ]) &&
444+ (ext_csd [EXT_CSD_SEC_ERASE_MULT ] ==
445+ bw_ext_csd [EXT_CSD_SEC_ERASE_MULT ]) &&
446+ (ext_csd [EXT_CSD_SEC_FEATURE_SUPPORT ] ==
447+ bw_ext_csd [EXT_CSD_SEC_FEATURE_SUPPORT ]) &&
448+ (ext_csd [EXT_CSD_TRIM_MULT ] ==
449+ bw_ext_csd [EXT_CSD_TRIM_MULT ]) &&
450+ memcmp (& ext_csd [EXT_CSD_SEC_CNT ],
451+ & bw_ext_csd [EXT_CSD_SEC_CNT ],
452+ 4 ) != 0 );
453+ if (err )
454+ err = - EINVAL ;
455+
456+ out :
457+ mmc_free_ext_csd (bw_ext_csd );
377458 return err ;
378459}
379460
@@ -438,6 +519,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
438519 u32 cid [4 ];
439520 unsigned int max_dtr ;
440521 u32 rocr ;
522+ u8 * ext_csd = NULL ;
441523
442524 BUG_ON (!host );
443525 WARN_ON (!host -> claimed );
@@ -536,7 +618,11 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
536618 /*
537619 * Fetch and process extended CSD.
538620 */
539- err = mmc_read_ext_csd (card );
621+
622+ err = mmc_get_ext_csd (card , & ext_csd );
623+ if (err )
624+ goto free_card ;
625+ err = mmc_read_ext_csd (card , ext_csd );
540626 if (err )
541627 goto free_card ;
542628
@@ -676,14 +762,18 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
676762 0 );
677763 if (!err ) {
678764 mmc_set_bus_width (card -> host , bus_width );
765+
679766 /*
680767 * If controller can't handle bus width test,
681- * use the highest bus width to maintain
682- * compatibility with previous MMC behavior.
768+ * compare ext_csd previously read in 1 bit mode
769+ * against ext_csd at new bus width
683770 */
684771 if (!(host -> caps & MMC_CAP_BUS_WIDTH_TEST ))
685- break ;
686- err = mmc_bus_test (card , bus_width );
772+ err = mmc_compare_ext_csds (card ,
773+ ext_csd ,
774+ bus_width );
775+ else
776+ err = mmc_bus_test (card , bus_width );
687777 if (!err )
688778 break ;
689779 }
@@ -730,12 +820,14 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
730820 if (!oldcard )
731821 host -> card = card ;
732822
823+ mmc_free_ext_csd (ext_csd );
733824 return 0 ;
734825
735826free_card :
736827 if (!oldcard )
737828 mmc_remove_card (card );
738829err :
830+ mmc_free_ext_csd (ext_csd );
739831
740832 return err ;
741833}
0 commit comments