4747#include <asm/uaccess.h>
4848#include <asm/of_device.h>
4949
50- #define VERSION "0.6 "
50+ #define VERSION "0.7 "
5151#define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
5252
5353#undef DEBUG_SMU
5454
5555#ifdef DEBUG_SMU
56- #define DPRINTK (fmt , args ...) do { printk (KERN_DEBUG fmt , ##args); } while (0)
56+ #define DPRINTK (fmt , args ...) do { udbg_printf (KERN_DEBUG fmt , ##args); } while (0)
5757#else
5858#define DPRINTK (fmt , args ...) do { } while (0)
5959#endif
@@ -92,7 +92,7 @@ struct smu_device {
9292 * for now, just hard code that
9393 */
9494static struct smu_device * smu ;
95-
95+ static DECLARE_MUTEX ( smu_part_access );
9696
9797/*
9898 * SMU driver low level stuff
@@ -113,9 +113,11 @@ static void smu_start_cmd(void)
113113
114114 DPRINTK ("SMU: starting cmd %x, %d bytes data\n" , cmd -> cmd ,
115115 cmd -> data_len );
116- DPRINTK ("SMU: data buffer: %02x %02x %02x %02x ... \n" ,
116+ DPRINTK ("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x \n" ,
117117 ((u8 * )cmd -> data_buf )[0 ], ((u8 * )cmd -> data_buf )[1 ],
118- ((u8 * )cmd -> data_buf )[2 ], ((u8 * )cmd -> data_buf )[3 ]);
118+ ((u8 * )cmd -> data_buf )[2 ], ((u8 * )cmd -> data_buf )[3 ],
119+ ((u8 * )cmd -> data_buf )[4 ], ((u8 * )cmd -> data_buf )[5 ],
120+ ((u8 * )cmd -> data_buf )[6 ], ((u8 * )cmd -> data_buf )[7 ]);
119121
120122 /* Fill the SMU command buffer */
121123 smu -> cmd_buf -> cmd = cmd -> cmd ;
@@ -440,7 +442,7 @@ int smu_present(void)
440442EXPORT_SYMBOL (smu_present );
441443
442444
443- int smu_init (void )
445+ int __init smu_init (void )
444446{
445447 struct device_node * np ;
446448 u32 * data ;
@@ -845,16 +847,154 @@ int smu_queue_i2c(struct smu_i2c_cmd *cmd)
845847 return 0 ;
846848}
847849
848- struct smu_sdbp_header * smu_get_sdb_partition (int id , unsigned int * size )
850+ /*
851+ * Handling of "partitions"
852+ */
853+
854+ static int smu_read_datablock (u8 * dest , unsigned int addr , unsigned int len )
855+ {
856+ DECLARE_COMPLETION (comp );
857+ unsigned int chunk ;
858+ struct smu_cmd cmd ;
859+ int rc ;
860+ u8 params [8 ];
861+
862+ /* We currently use a chunk size of 0xe. We could check the
863+ * SMU firmware version and use bigger sizes though
864+ */
865+ chunk = 0xe ;
866+
867+ while (len ) {
868+ unsigned int clen = min (len , chunk );
869+
870+ cmd .cmd = SMU_CMD_MISC_ee_COMMAND ;
871+ cmd .data_len = 7 ;
872+ cmd .data_buf = params ;
873+ cmd .reply_len = chunk ;
874+ cmd .reply_buf = dest ;
875+ cmd .done = smu_done_complete ;
876+ cmd .misc = & comp ;
877+ params [0 ] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC ;
878+ params [1 ] = 0x4 ;
879+ * ((u32 * )& params [2 ]) = addr ;
880+ params [6 ] = clen ;
881+
882+ rc = smu_queue_cmd (& cmd );
883+ if (rc )
884+ return rc ;
885+ wait_for_completion (& comp );
886+ if (cmd .status != 0 )
887+ return rc ;
888+ if (cmd .reply_len != clen ) {
889+ printk (KERN_DEBUG "SMU: short read in "
890+ "smu_read_datablock, got: %d, want: %d\n" ,
891+ cmd .reply_len , clen );
892+ return - EIO ;
893+ }
894+ len -= clen ;
895+ addr += clen ;
896+ dest += clen ;
897+ }
898+ return 0 ;
899+ }
900+
901+ static struct smu_sdbp_header * smu_create_sdb_partition (int id )
902+ {
903+ DECLARE_COMPLETION (comp );
904+ struct smu_simple_cmd cmd ;
905+ unsigned int addr , len , tlen ;
906+ struct smu_sdbp_header * hdr ;
907+ struct property * prop ;
908+
909+ /* First query the partition info */
910+ smu_queue_simple (& cmd , SMU_CMD_PARTITION_COMMAND , 2 ,
911+ smu_done_complete , & comp ,
912+ SMU_CMD_PARTITION_LATEST , id );
913+ wait_for_completion (& comp );
914+
915+ /* Partition doesn't exist (or other error) */
916+ if (cmd .cmd .status != 0 || cmd .cmd .reply_len != 6 )
917+ return NULL ;
918+
919+ /* Fetch address and length from reply */
920+ addr = * ((u16 * )cmd .buffer );
921+ len = cmd .buffer [3 ] << 2 ;
922+ /* Calucluate total length to allocate, including the 17 bytes
923+ * for "sdb-partition-XX" that we append at the end of the buffer
924+ */
925+ tlen = sizeof (struct property ) + len + 18 ;
926+
927+ prop = kcalloc (tlen , 1 , GFP_KERNEL );
928+ if (prop == NULL )
929+ return NULL ;
930+ hdr = (struct smu_sdbp_header * )(prop + 1 );
931+ prop -> name = ((char * )prop ) + tlen - 18 ;
932+ sprintf (prop -> name , "sdb-partition-%02x" , id );
933+ prop -> length = len ;
934+ prop -> value = (unsigned char * )hdr ;
935+ prop -> next = NULL ;
936+
937+ /* Read the datablock */
938+ if (smu_read_datablock ((u8 * )hdr , addr , len )) {
939+ printk (KERN_DEBUG "SMU: datablock read failed while reading "
940+ "partition %02x !\n" , id );
941+ goto failure ;
942+ }
943+
944+ /* Got it, check a few things and create the property */
945+ if (hdr -> id != id ) {
946+ printk (KERN_DEBUG "SMU: Reading partition %02x and got "
947+ "%02x !\n" , id , hdr -> id );
948+ goto failure ;
949+ }
950+ if (prom_add_property (smu -> of_node , prop )) {
951+ printk (KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
952+ "property !\n" , id );
953+ goto failure ;
954+ }
955+
956+ return hdr ;
957+ failure :
958+ kfree (prop );
959+ return NULL ;
960+ }
961+
962+ /* Note: Only allowed to return error code in pointers (using ERR_PTR)
963+ * when interruptible is 1
964+ */
965+ struct smu_sdbp_header * __smu_get_sdb_partition (int id , unsigned int * size ,
966+ int interruptible )
849967{
850968 char pname [32 ];
969+ struct smu_sdbp_header * part ;
851970
852971 if (!smu )
853972 return NULL ;
854973
855974 sprintf (pname , "sdb-partition-%02x" , id );
856- return (struct smu_sdbp_header * )get_property (smu -> of_node ,
975+
976+ if (interruptible ) {
977+ int rc ;
978+ rc = down_interruptible (& smu_part_access );
979+ if (rc )
980+ return ERR_PTR (rc );
981+ } else
982+ down (& smu_part_access );
983+
984+ part = (struct smu_sdbp_header * )get_property (smu -> of_node ,
857985 pname , size );
986+ if (part == NULL ) {
987+ part = smu_create_sdb_partition (id );
988+ if (part != NULL && size )
989+ * size = part -> len << 2 ;
990+ }
991+ up (& smu_part_access );
992+ return part ;
993+ }
994+
995+ struct smu_sdbp_header * smu_get_sdb_partition (int id , unsigned int * size )
996+ {
997+ return __smu_get_sdb_partition (id , size , 0 );
858998}
859999EXPORT_SYMBOL (smu_get_sdb_partition );
8601000
@@ -930,6 +1070,14 @@ static ssize_t smu_write(struct file *file, const char __user *buf,
9301070 else if (hdr .cmdtype == SMU_CMDTYPE_WANTS_EVENTS ) {
9311071 pp -> mode = smu_file_events ;
9321072 return 0 ;
1073+ } else if (hdr .cmdtype == SMU_CMDTYPE_GET_PARTITION ) {
1074+ struct smu_sdbp_header * part ;
1075+ part = __smu_get_sdb_partition (hdr .cmd , NULL , 1 );
1076+ if (part == NULL )
1077+ return - EINVAL ;
1078+ else if (IS_ERR (part ))
1079+ return PTR_ERR (part );
1080+ return 0 ;
9331081 } else if (hdr .cmdtype != SMU_CMDTYPE_SMU )
9341082 return - EINVAL ;
9351083 else if (pp -> mode != smu_file_commands )
0 commit comments