6 #include <iba/ib_types.h>
\r
7 #include <iba/ib_al.h>
\r
18 /* common query parameters */
\r
19 typedef struct _REQUEST_IN
\r
26 unsigned short pkey_num;
\r
27 unsigned __int16 pkeys[MAX_NUM_PKEY];
\r
33 #define DEFAULT_BUFER_SIZE 1024
\r
34 static const char IBBUS_SERV_KEY[] = {"SYSTEM\\CurrentControlSet\\Services\\ibbus\\Parameters"};
\r
38 printf("Usage : part_man.exe <show|add|rem> <port_guid> <pkey1, pkey2, ...>\n");
\r
41 /********************************************************************
\r
42 * name : reg_ibbus_pkey_show
\r
43 * read registry pkey and optionally prints it
\r
44 * input : show - whether to print
\r
45 * output: partKey - contains read pkeys, reg_handle
\r
46 * return: number of characters read
\r
47 ********************************************************************/
\r
48 static int reg_ibbus_pkey_show(IN BOOLEAN show,OUT char *partKey, OUT HKEY *reg_handle)
\r
52 DWORD read_length = DEFAULT_BUFER_SIZE;
\r
54 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,IBBUS_SERV_KEY,0,KEY_SET_VALUE | KEY_QUERY_VALUE ,reg_handle);
\r
55 if (ERROR_SUCCESS != ret)
\r
57 printf("reg_ibbus_pkey_show error = %d\n",GetLastError());
\r
63 ret = RegQueryValueEx(*reg_handle,"PartitionKey",NULL,NULL,(LPBYTE)partKey,&read_length);
\r
64 if (ERROR_MORE_DATA == ret)
\r
66 printf("reg_ibbus_pkey_show : more buffer space required\n");
\r
71 if (ERROR_SUCCESS != ret)
\r
73 printf("RegQueryValueEx : Error %d\n",ret);
\r
77 retval = (int)read_length;
\r
81 printf("Existing Pkey(s): %s\n",partKey);
\r
90 static int reg_ibbus_print_pkey()
\r
93 char pkeyBuf[DEFAULT_BUFER_SIZE];
\r
96 result = reg_ibbus_pkey_show(TRUE,(char*)pkeyBuf,&hreg);
\r
98 RegCloseKey( hreg );
\r
102 printf("No configured pkey found\n");
\r
108 static int reg_ibbus_pkey_add(const uint16_t *pkeys, uint16_t pkey_num,OUT pkey_array_t *pkey, OUT char **final_reg_string, OUT DWORD *reg_length)
\r
110 static char partKey[DEFAULT_BUFER_SIZE];
\r
112 char *guid_string, *p;
\r
115 char *tmpbuff = NULL;
\r
121 *final_reg_string = NULL;
\r
122 read_length = reg_ibbus_pkey_show(FALSE,(char*)partKey,®_handle);
\r
124 guid_string = NULL;
\r
125 if (read_length < 4)
\r
127 /* empty string read, simply write to registry */
\r
128 cnt = sprintf(partKey,"0x%I64X:",pkey->port_guid);
\r
132 /* update the existed registry list */
\r
133 sprintf(tmp,"0x%I64X",pkey->port_guid);
\r
134 guid_string = strstr(partKey,tmp);
\r
137 p = strstr(guid_string,";");
\r
138 tmpbuff = (char*)malloc(strlen(p) + 1);
\r
141 printf("Failed memory allocation\n");
\r
144 /* save the rest of the string */
\r
146 cnt = (int)(p - partKey);
\r
150 cnt = strlen(partKey) + sprintf(partKey + strlen(partKey),"%s:",tmp);
\r
154 for (i = 0 ;i < pkey_num; i++)
\r
157 sprintf(tmp,"0x%04X",pkeys[i]);
\r
160 same_pkey = strstr(guid_string,tmp);
\r
161 if( same_pkey && (same_pkey < p) )
\r
164 pkey->pkey_array[pkey->pkey_num] = pkeys[i];
\r
166 if( (i == 0) && (!guid_string))
\r
167 cnt += sprintf(partKey + cnt,"0x%04X",pkeys[i]);
\r
169 cnt += sprintf(partKey + cnt,",0x%04X",pkeys[i]);
\r
173 cnt += sprintf(partKey + cnt,"%s",tmpbuff);
\r
177 cnt += sprintf(partKey + cnt,";\0");
\r
181 *final_reg_string = partKey;
\r
182 *reg_length = (DWORD)cnt;
\r
186 printf("No pkey to add\n");
\r
189 RegCloseKey( reg_handle );
\r
193 static int reg_ibbus_pkey_rem(const unsigned __int16 *pkeys, unsigned short pkey_num,OUT pkey_array_t *pkey)
\r
195 static char partKey[DEFAULT_BUFER_SIZE];
\r
196 static char newKey[DEFAULT_BUFER_SIZE] = {'\0'};
\r
202 unsigned __int16 cur_pkey;
\r
204 unsigned short i = 0;
\r
205 char pkey_sep[] = ",";
\r
210 char *pafter = NULL;
\r
211 boolean_t found2remove;
\r
212 boolean_t pkey_not_written = TRUE;
\r
214 read_length = reg_ibbus_pkey_show(FALSE,(char*)partKey,®_handle);
\r
217 if (read_length < 4)
\r
219 /* empty string read, nothing to delete */
\r
220 printf("No pkey configured - nothing to remove\n");
\r
225 sprintf(tmp,"0x%I64X\0",pkey->port_guid);
\r
226 guid_string = strstr(partKey,tmp);
\r
229 printf("No guid configured - nothing to remove\n");
\r
233 pfrom = strstr(guid_string,":");
\r
234 pto = strstr(guid_string,";");
\r
235 if ( (!pfrom) || (!pto))
\r
237 printf("Error configuration\n");
\r
243 pafter = (char*)malloc(strlen(pto) + 1);
\r
247 printf("Allocation failed\n");
\r
251 _snprintf(newKey,(int)(pfrom - partKey),"%s",partKey);
\r
252 cnt = (int)(pfrom - partKey);
\r
253 strcpy(pafter,pto);
\r
255 strcpy(partKey,pfrom);
\r
256 token = strtok(partKey,pkey_sep);
\r
259 found2remove = FALSE;
\r
260 converted = sscanf(token,"0x%X",&cur_pkey);
\r
261 if(!converted || (converted == EOF))
\r
263 printf("invalid registry format\n");
\r
268 for (i = 0; i < pkey_num; i++)
\r
270 found2remove = (boolean_t)(cur_pkey == pkeys[i]);
\r
273 pkey->pkey_array[pkey->pkey_num] = pkeys[i];
\r
284 if(pkey_not_written)
\r
286 cnt += sprintf(newKey + cnt,"0x%04X",cur_pkey);
\r
287 pkey_not_written = FALSE;
\r
290 cnt += sprintf(newKey + cnt,",0x%04X",cur_pkey);
\r
292 token = strtok(NULL,pkey_sep);
\r
295 if(! pkey->pkey_num)
\r
297 /* nothing to delete */
\r
298 printf("No pkey found to remove\n");
\r
303 if(pkey_not_written)
\r
304 cnt -= (2 + strlen(tmp));
\r
306 strcpy(newKey + cnt,pafter);
\r
307 ret = RegSetValueEx(reg_handle,"PartitionKey",0,REG_SZ,(BYTE*)newKey, (DWORD)strlen(newKey));
\r
308 if (ERROR_SUCCESS != ret)
\r
310 printf("reg_ibbus_pkey_add RegSetValueEx error = %d\n",GetLastError());
\r
319 RegCloseKey( reg_handle );
\r
323 static int send_pdo_req(pkey_array_t *pkeys,DWORD iocode)
\r
326 DWORD bytesReturned;
\r
331 GENERIC_READ | GENERIC_WRITE,
\r
332 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode none
\r
333 NULL, // no security
\r
335 FILE_ATTRIBUTE_NORMAL,
\r
336 NULL // no template
\r
339 if (hKernelLib == INVALID_HANDLE_VALUE)
\r
341 printf("failed to open the kernel device : error = %d\n",GetLastError());
\r
345 if (! DeviceIoControl(hKernelLib,
\r
347 pkeys,sizeof(pkey_array_t),
\r
352 DWORD err = GetLastError();
\r
354 printf("No matched port guid (0x%I64X) found\n",pkeys->port_guid);
\r
355 else if (err == 1117)
\r
356 printf("operation failed - internal driver error\n");
\r
358 printf("operation failed - invalid input to driver\n");
\r
360 printf("operation failed with error %d\n",err);
\r
362 CloseHandle(hKernelLib);
\r
365 CloseHandle(hKernelLib);
\r
370 boolean_t reg_pkey_operation(const REQUEST_IN *input)
\r
372 pkey_array_t pkeys;
\r
374 char *p_reg_string;
\r
375 DWORD reg_length = 0;
\r
381 printf("create_ipoib_pdo : invalid input parameter\n");
\r
385 RtlZeroMemory(&pkeys,sizeof(pkeys));
\r
386 pkeys.port_guid = input->u.guid_pkey.port_guid;
\r
388 if(input->u.guid_pkey.action == pkey_add)
\r
389 result = reg_ibbus_pkey_add((unsigned __int16*)input->u.guid_pkey.pkeys, input->u.guid_pkey.pkey_num, &pkeys,&p_reg_string,®_length);
\r
390 else if(input->u.guid_pkey.action == pkey_rem)
\r
391 result = reg_ibbus_pkey_rem((unsigned __int16*)input->u.guid_pkey.pkeys, input->u.guid_pkey.pkey_num, &pkeys);
\r
392 else if(input->u.guid_pkey.action == pkey_show)
\r
393 result = reg_ibbus_print_pkey();
\r
395 printf("Invalid command to part_man.exe\n");
\r
402 if(input->u.guid_pkey.action == pkey_add)
\r
404 if( 0 == send_pdo_req(&pkeys,UAL_REQ_CREATE_PDO))
\r
406 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,IBBUS_SERV_KEY,0,KEY_SET_VALUE | KEY_QUERY_VALUE ,®_handle);
\r
408 ret = RegSetValueEx(reg_handle,"PartitionKey",0,REG_SZ,(BYTE*)p_reg_string,reg_length);
\r
409 RegCloseKey( reg_handle );
\r
410 if (ERROR_SUCCESS == ret)
\r
416 printf("reg_ibbus_pkey_add RegSetValueEx error = %d\n",GetLastError());
\r
420 else if(input->u.guid_pkey.action == pkey_rem)
\r
422 return (boolean_t)( 0 == send_pdo_req(&pkeys,UAL_REQ_REMOVE_PDO));
\r
428 int prepare_reg_pkey_input(OUT REQUEST_IN *input,char* cmd[],int num)
\r
431 input->u.guid_pkey.pkey_num = 0;
\r
433 if(strstr(cmd[1],"add"))
\r
434 input->u.guid_pkey.action = pkey_add;
\r
435 else if(strstr(cmd[1],"rem"))
\r
436 input->u.guid_pkey.action = pkey_rem;
\r
437 else if(strstr(cmd[1],"show"))
\r
439 input->u.guid_pkey.action = pkey_show;
\r
444 printf("invalid command %s\n",cmd[1]);
\r
450 printf("invalid command %s\n",cmd[1]);
\r
454 /* vstat output format 0008:f104:0397:7ccc
\r
455 For port guid add 1 for each port
\r
457 if (strstr(cmd[2],":"))
\r
460 unsigned short *guid_vstat;
\r
461 guid_vstat = (unsigned short*)&input->u.guid_pkey.port_guid;
\r
462 sscanf(cmd[2],"%x:%x:%x:%x",&guid_vstat[0],&guid_vstat[1],&guid_vstat[2],&guid_vstat[3]);
\r
463 for( i = 0; i < 4; i++)
\r
464 guid_vstat[i] = (guid_vstat[i] << 8) | (guid_vstat[i] >> 8);
\r
468 printf("port guid %s - illegal string format\n",cmd[2]);
\r
472 for( i = 3; i < num; i++)
\r
474 if((strstr(cmd[i],"ffff")) || (strstr(cmd[i],"FFFF")))
\r
476 if (strstr(cmd[i],"0x") || strstr(cmd[i],"0X"))
\r
477 sscanf(cmd[i],"0x%x",&input->u.guid_pkey.pkeys[input->u.guid_pkey.pkey_num]);
\r
479 sscanf(cmd[i],"%x",&input->u.guid_pkey.pkeys[input->u.guid_pkey.pkey_num]);
\r
480 input->u.guid_pkey.pkey_num++;
\r
485 void partition_operation(char* cmd[],int num)
\r
489 if (! prepare_reg_pkey_input(&input, cmd, num))
\r
492 if(! reg_pkey_operation(&input))
\r
493 printf("Pkey operation failed\n");
\r
495 printf("Done...\n");
\r
503 BOOLEAN showHelp = FALSE;
\r
510 if(!_stricmp(argv[1], "-h") || !_stricmp(argv[1], "-help"))
\r
515 partition_operation(argv,argc);
\r