//------------------------------------------------------------------------- // commit_entry() // Thu Jan 11 13:28:35 EST 2001 //------------------------------------------------------------------------- // Update memory mapped file with new data // // input: db file type // output: status //------------------------------------------------------------------------- int commit_entry( int dbType ) { void *dbptr; // generic pointer to dbs int status = SUCCESS; // assume the best extern struct MODULE *CTSdb; // pointer to in-memory copy of data file extern struct CRATE *CRATEdb; // " " " " " " " if( MSGLVL(FUNCTION_NAME) ) printf( "commit_entry()\n" ); switch( dbType ) { case CTS_DB: dbptr = (void *)CTSdb; break; case CRATE_DB: dbptr = (void *)CRATEdb; break; } // commit changes to file // NB! a len of 0 flushes all memory pages mapped if( msync(dbptr, 0, MS_SYNC|MS_INVALIDATE) == ERROR ) status = COMMIT_ERROR; if( MSGLVL(DETAILS) ) { printf("commit_entry(): "); ShowStatus(status); } return status; }
//------------------------------------------------------------------------- // Delete a crate from the crate db //------------------------------------------------------------------------- int DelCrate() { char crateName[CRATE_NAME_SIZE + 1]; int index, numOfEntries; int status = SUCCESS; static DESCRIPTOR( phy_name_p, "PHY_NAME" ); static DYNAMIC_DESCRIPTOR( phy_name ); // get user input str_free1_dx(&phy_name); // per twf -- clear out field cli_get_value( &phy_name_p, &phy_name ); str_upcase( &phy_name, &phy_name ); // trim it... sprintf(crateName, "%.6s", phy_name.pointer); // check to see if db file memory mapped if( CRATEdbFileIsMapped == FALSE ) { // is not, so try if( map_data_file(CRATE_DB) != SUCCESS ) { // we're dead in the water status = FAILURE; // MAP_ERROR; [2001.07.12] goto DelCrate_Exit; } } // get number of current entries if( (numOfEntries = get_file_count(CRATE_DB)) == 0 ) { // no entries in crate db file if( MSGLVL(IMPORTANT) ) fprintf( stderr, "db file empty, no entries to remove\n" ); status = FAILURE; // DELCRATE_ERROR; [2001.07.12] goto DelCrate_Exit; } // try to remove from crate.db if( (index = lookup_entry(CRATE_DB, crateName)) >= 0 ) { // module does exist if( remove_entry(CRATE_DB, index) != SUCCESS ) { // removal failed status = FAILURE; // DELCRATE_ERROR; [2001.07.12] goto DelCrate_Exit; } } else { // no such module if( MSGLVL(IMPORTANT) ) fprintf( stderr, "delcrate(): entry '%s' not found\n", crateName); status = FAILURE; // DELCRATE_ERROR; [2001.07.12] goto DelCrate_Exit; } DelCrate_Exit: if( MSGLVL(DETAILS) ) { printf("DelCrate() status = %d\n", status); } return SUCCESS; }
//------------------------------------------------------------------------- // parse_cts_db() //------------------------------------------------------------------------- // Fri Jan 5 12:00:48 EST 2001 // Tue Mar 20 15:50:36 EST 2001 -- conversion fixup // Wed Jan 16 10:29:30 EST 2002 -- " " // Fri Jan 18 11:09:18 EST 2002 -- fixed comment field // Fri Jan 25 12:52:12 EST 2002 -- 'really' fixed it this time! // Thu Feb 7 13:29:05 EST 2002 -- fix from 'module.h', ie comment field //------------------------------------------------------------------------- // Parse a database entry from memory mapped file // // input: pointer to cts.db data (in memory) // output: pointer to struct of db data //------------------------------------------------------------------------- void parse_cts_db( struct MODULE *in, struct Module_ *out ) { char adpt, fmt[6], line[MODULE_ENTRY+1]; char comm[40]; int i; if( MSGLVL(FUNCTION_NAME) ) printf( "parse_cts_db()\n" ); sprintf(fmt, "%%.%ds", MODULE_ENTRY - 1); // create format string memset(line, ' ', MODULE_ENTRY + 1); // 2002.02.06 sprintf(line, fmt, (char*)in); // extract first (single) line // parse ... sscanf( line, "%32s GK%c%1d%02d:N%2d %40c", out->name, &adpt, // temporary &out->id, &out->crate, &out->slot, comm // temporary ); out->adapter = adpt - 'A'; // adjustment ... // find end of comment for( i = sizeof(comm) - 1; i >= 0; --i ) { if( comm[i] != ' ' ) break; } comm[i + 1] = '\0'; // 'early' termination strncpy(out->comment, comm, i); // 'copy' it over [2002.02.07] }
//------------------------------------------------------------------------- // get_file_name() //------------------------------------------------------------------------- // Prepends path information for files, specifically db files, using // information passed in the ENVIRONMENT. User can append a '/' char, // or not. // Eg. use: CTS_DB_DIR=/my_directory; export CTS_DB_DIR // or: CTS_DB_DIR=/my_directory/; export CTS_DB_DIR // trailing '/' --^ //------------------------------------------------------------------------- // Fri Apr 13 12:29:02 EDT 2001 // Tue Apr 17 11:42:55 EDT 2001 -- more concice processing of '/' character //------------------------------------------------------------------------- char *get_file_name( char *filename ) { char *pEnv; // pointer to environment var static char db_dir[256], *pChar; // needs to be persistant // get environment variable pEnv = getenv(DB_DIR); // variable specified in ".../tunables.h" // adjust filename if( pEnv == NULL ) sprintf(db_dir, "%s", filename); // ENV variable is NULL -- no change to filename else sprintf(db_dir, "%s%s%s", // ENV variable non-NULL -- prepend to filename pEnv, (*(pEnv+strlen(pEnv)-1) != '/') ? "/" : "", // add '/' if necessary filename ); pChar = &db_dir[0]; if( MSGLVL(FUNCTION_NAME) ) printf( "get_file_name('%s')\n", pChar ); // send it back ... return pChar; }
int ScsiSystemStatus( void ) { char line[80], *pline; int scsiSystemStatus = 0; // assume the worst :( FILE *fp, *fopen(); if( MSGLVL(FUNCTION_NAME) ) printf( "ScsiSystemStatus()\n" ); if( (fp = fopen(PROC_FILE, "r")) == NULL ) { fprintf( stderr, "can't open '%s' for read\n", PROC_FILE); scsiSystemStatus = 0; goto ScsiSystemStatus_Exit; } pline = &line[0]; if( (pline = fgets(line, sizeof(line), fp)) != NULL ) { if( strncmp(pline, scsimsg, strlen(scsimsg)) ) scsiSystemStatus = 1; // something is attached } ScsiSystemStatus_Exit: if( fp ) // still open fclose(fp); return scsiSystemStatus; }
//------------------------------------------------------------------------- //------------------------------------------------------------------------- int Noop() { if( MSGLVL(DETAILS) ) printf("Noop() invoked -- not implemented\n"); return SUCCESS; }
//------------------------------------------------------------------------- // create_sema4() // Mon Apr 9 15:04:34 EDT 2001 -- changed file name for 'ftok()' call //------------------------------------------------------------------------- // Create a semaphore for use in file locking // // input: none // output: SUCCESS, otherwise fatal error //------------------------------------------------------------------------- int create_sema4() { key_t key; union semun arg; sema4Exists = FALSE; // at least at this point if( MSGLVL(FUNCTION_NAME) ) printf( "create_sema4()\n" ); // obtain a key if( (key = ftok(SEMAPHORE_KEY_SEED, 'A')) == ERROR ) { perror("ftok(create_sema4)"); exit(-1); // fatal error !!! } // get a semaphore id if( (semid = semget(key, 1, 0666 | IPC_CREAT)) == ERROR ) { perror("semget(create_sema4)"); exit(-2); // fatal error !!! } // create semaphore and initialize it arg.val = 1; if( semctl(semid, 0, SETVAL, arg) == ERROR ) { perror("semctl(create_asema4)"); exit(-3); // fatal error !!! } sema4Exists = TRUE; // if we get here, all's OK return SUCCESS; }
//------------------------------------------------------------------------- // translate a logical module name to a physical representation (ie 'Key' format) //------------------------------------------------------------------------- // Fri May 25 12:23:23 EDT 2001 // Fri Feb 8 10:42:57 EST 2002 -- fixed type of 'key->scsi_port' //------------------------------------------------------------------------- // input: logical module name // output: status, and modified data referenced by pointer //------------------------------------------------------------------------- int xlate_logicalname( char *Name, CamKey *key ) { int i; int status = SUCCESS; // otpimistic struct Module_ Mod; extern struct MODULE *CTSdb; extern int CTSdbFileIsMapped; if( MSGLVL(FUNCTION_NAME) ) printf( "xlate_logicalname()\n" ); if( strchr(Name, ':') != NULL ) { // invalid logical name ... status = ERROR; // ... was passed a physical name goto Xlate_LogicalName_Exit; } // check to see if CTS db is memory mapped if( CTSdbFileIsMapped == FALSE ) { if( map_data_file( CTS_DB ) != SUCCESS ) { status = MAP_ERROR; // not mapped, we're done :< goto Xlate_LogicalName_Exit; } } // look up entry in db file if( (i = lookup_entry( CTS_DB, Name )) < 0 ) { status = NO_DEVICE; goto Xlate_LogicalName_Exit; } parse_cts_db( CTSdb+i, &Mod ); // get info ... // load up struct with vital info key->scsi_port = Mod.adapter +'A'; // SCSI host adapter [2002.02.08] key->scsi_address = Mod.id; // SCSI id key->crate = Mod.crate; // CAMAC crate number key->slot = Mod.slot; // CAMAC slot (ie station) Xlate_LogicalName_Exit: if( MSGLVL(DETAILS) ) { printf( "xlate(): name['%s'] ==>> HA[%c] id[%d] crate[%02d] slot[%d]\n", Name, key->scsi_port, key->scsi_address, key->crate, key->slot ); } return status; }
//----------------------------------------------------------- int CamError( int xexp, int qexp, TranslatedIosb *iosb ) { int xexp_use; int qexp_use; TranslatedIosb *iosb_use; if( MSGLVL(DETAILS) ) printf( "CamError(): xexp:%d qexp:%d\n", xexp, qexp ); xexp_use = xexp ? xexp : 0; qexp_use = qexp ? qexp : 0; iosb_use = iosb ? iosb : &LastIosb; iosb_use->err = !iosb_use->x && !iosb_use->q; if( MSGLVL(DETAILS) ) printf( "CamError(): x:%d q:%d iosb->err %d\n", xexp_use, qexp_use, iosb_use->err ); return iosb_use->err; }
//------------------------------------------------------------------------- // unlock_file() //------------------------------------------------------------------------- // Thu Jan 11 13:52:17 EST 2001 //------------------------------------------------------------------------- // Remove file lock provided by a sempahore. // // input: none // output: status //------------------------------------------------------------------------- int unlock_file() { int status = SUCCESS; // assume the best case struct sembuf sb = { 0, P_SEMA4, 0}; extern int semid; if( MSGLVL(FUNCTION_NAME) ) printf( "unlock_file()\n" ); sb.sem_op = V_SEMA4; // prepare to 'vend' a semaphore if( semop(semid, &sb, 1) == ERROR ) { if( MSGLVL(ALWAYS) ) perror("semop()"); status = ERROR; } if( MSGLVL(DETAILS) ) { printf( "unlock_file(): " ); ShowStatus( status ); } return status; }
//------------------------------------------------------------------------- // check_sema4() // Thu Jan 11 13:34:22 EST 2001 //------------------------------------------------------------------------- // Check to see if we can get the semaphore // // input: none // output: semaphore count // NB! count should be zero for success (ie binary semaphore) //------------------------------------------------------------------------- int check_sema4() { int count; union semun u; extern int semid; if( MSGLVL(FUNCTION_NAME) ) printf( "check_sema4()\n" ); // check to see if sema4 is in use count = semctl(semid, 0, GETVAL, u); return count; }
//------------------------------------------------------------------------- //------------------------------------------------------------------------- int ResetHighway() { int status; static DESCRIPTOR( crate_p, "MODULE" ); static DYNAMIC_DESCRIPTOR( wild ); static DYNAMIC_DESCRIPTOR( crate ); cli_get_value( &crate_p, &wild ); if( MSGLVL(DETAILS) ) printf("ResetHighway() invoked -- not implemented\n"); return SUCCESS; }
int CamQ( TranslatedIosb *iosb ) // CAM$Q_SCSI() { TranslatedIosb *iosb_use; iosb_use = (iosb) ? iosb : &LastIosb; if( MSGLVL(DETAILS) ) printf( "CamQ(): using %s %p CamQ()=%d\n", (iosb) ? "iosb" : "&LastIosb", iosb, iosb_use->q ); //printf("CamQ(iosb) ::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] //printf("CamQ(iosb_use)::->> bytecount= %d\n", iosb_use->bytcnt); // [2002.12.13] //printf("CamQ(LastIosb)::->> bytecount= %d\n", LastIosb.bytcnt); // [2002.12.13] return iosb_use->q; }
//----------------------------------------------------------- int CamBytcnt( TranslatedIosb *iosb ) // CAM$BYTCNT_SCSI() { TranslatedIosb *iosb_use; iosb_use = (iosb) ? iosb : &LastIosb; if( MSGLVL(DETAILS) ) printf( "CamBytcnt(): using %s %p CamBytcnt()=%d\n", (iosb) ? "iosb" : "&LastIosb", iosb, iosb_use->bytcnt ); //printf("CamBytcnt(iosb) ::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] //printf("CamBytcnt(iosb_use)::->> bytecount= %d\n", iosb_use->bytcnt); // [2002.12.13] //printf("CamBytcnt(LastIosb)::->> bytecount= %d\n", LastIosb.bytcnt); // [2002.12.13] return ((int)iosb_use->bytcnt) | (((int)iosb_use->lbytcnt) << 16); }
//------------------------------------------------------------------------- // set a crate on-line or off-line //------------------------------------------------------------------------- int SetCrate() { int status; char *cratename; void *ctx=0; static DESCRIPTOR( crate_p, "CRATE" ); static DESCRIPTOR( quietq, "QUIET" ); static DYNAMIC_DESCRIPTOR( wild ); static DYNAMIC_DESCRIPTOR( crate ); static DESCRIPTOR( offq, "OFFLINE" ); static DESCRIPTOR( onq, "ONLINE" ); int off = cli_present(&offq) & 1; int on = cli_present(&onq ) & 1; int quiet = cli_present(&quietq) & 1; cli_get_value( &crate_p, &wild ); str_upcase( &wild, &wild ); // check to see if db file memory mapped if( CRATEdbFileIsMapped == FALSE ) { // is not, so try if( map_data_file(CRATE_DB) != SUCCESS ) { // we're dead in the water if( MSGLVL(IMPORTANT) ) fprintf( stderr, "error memory mapping crate db file\n" ); status = FAILURE; // MAP_ERROR; [2001.07.12] goto SetCrate_Exit; } } while (find_crate(wild.pointer,&cratename,&ctx)) { status = turn_crate_on_off_line( cratename , (on) ? ON : OFF ); if (!(status & 1) && !quiet) printf("Error turning crate %s %s\n",cratename,on ? "online" : "offline"); free(cratename); } find_crate_end(&ctx); SetCrate_Exit: return SUCCESS; }
//------------------------------------------------------------------------- // parse_crate_db() //------------------------------------------------------------------------- // Fri Jan 12 09:55:17 EST 2001 // Wed Mar 7 14:31:29 EST 2001 -- fixed conversion of DSFname to device // Thu Mar 15 12:00:33 EST 2001 -- changed format of crate.db // Tue Mar 20 12:26:15 EST 2001 -- return -1 for non assigned /dev/sg# // Mon Apr 30 13:15:27 EDT 2001 -- added highway type member //------------------------------------------------------------------------- // Parse a crate database entry from memory mapped file // // input: pointer to crate.db data (in memory), and // pointer to struct of crate data // output: none //------------------------------------------------------------------------- void parse_crate_db( struct CRATE *in, struct Crate_ *out ) { if( MSGLVL(FUNCTION_NAME) ) printf( "parse_crate_db()\n" ); // build full physical crate name, eg. 'GKB509' sprintf( out->name, "%.2s%c%c%.2s", in->Phys_Name.prefix, // 'GK' in->Phys_Name.Adapter, // SCSI host adapter in->Phys_Name.Id, // SCSI id number in->Phys_Name.Crate // CAMAC crate number ); out->device = (in->DSFname[0] != '.') ? atoi(in->DSFname) // valid /dev/sg# : -1; // in-valid out->type = in->HwyType; // highway type out->enhanced = in->enhanced == '1'; out->online = in->online == '1'; }
//----------------------------------------------------------- // turn_crate_on_off_line() //----------------------------------------------------------- // Fri Jul 27 11:56:57 EDT 2001 -- changed calling sequence for CamPiow() // Tue Jul 31 11:55:21 EDT 2001 -- added 'offline' support //----------------------------------------------------------- // Tue Apr 10 11:11:48 EDT 2001 // eg. *crate_name == "GKB509" //----------------------------------------------------------- int turn_crate_on_off_line( char *crate_name, int state ) { char controller[12], *pController; short SCCdata; int i, status = SUCCESS; // optimistic ... int idx; TranslatedIosb iosb; extern struct CRATE *CRATEdb; int online; int enhanced; int crateStatus; if( MSGLVL(FUNCTION_NAME) ) printf( "turn_crate_on_off_line('%s'=>%s)\n", crate_name, (state == ON) ? "ON" : "off" ); // convert to all UPPER CASE for( i = 0; i < strlen(crate_name); ++i ) toupper(*crate_name); // create full crate controller designation // NB! all crate controllers reside in slot 30 sprintf( &controller[0], "%.6s:N30", crate_name ); // lookup name -- make sure a valid device if( (idx = lookup_entry( CRATE_DB, crate_name )) < 0 ) { // lookup actual device num if( MSGLVL(IMPORTANT) ) fprintf( stderr, "no such crate in 'crate.db'\n" ); status = NO_DEVICE; // doesn't exist goto TurnCrateOnOffLine_Exit; } if( MSGLVL(DETAILS) ) printf( "lookup OK -- found '%s'\n", crate_name ); pController = &controller[0]; if (CRATEdb[idx].HwyType != ('0'+JORWAY_73A)) { SCCdata = 1; // initiates Dataway Z status = CamPiow( pController, // serial crate controller name 0, // A --\__ write status register 17, // F --/ &SCCdata, // data value 16, // mem == 16-bit data &iosb // *iosb ); SCCdata = (state == ON) ? 0 : 0x1000; // clear status register status = CamPiow( pController, // serial crate controller name 0, // A --\__ write status register 17, // F --/ &SCCdata, // data value 16, // mem == 16-bit data &iosb // *iosb ); if (status & 1) { status = get_crate_status(pController, &crateStatus); online = ((crateStatus & 0x1000) != 0x1000) ? TRUE : FALSE; if( !crateStatus || crateStatus == 0x3 ) online = FALSE; CRATEdb[idx].online = online ? '1' : '0'; enhanced = (online && (crateStatus & 0x4000)) ? TRUE : FALSE; CRATEdb[idx].enhanced = enhanced ? '1' : '0'; } } else { CRATEdb[idx].online = (state == ON) ? '1' : '0'; CRATEdb[idx].enhanced = '0'; status = 1; } //----------------------------------------------------------- TurnCrateOnOffLine_Exit: if( MSGLVL(DETAILS) ) printf( "tcool(): status %d\n", status ); return status; }
//------------------------------------------------------------------------- // get_file_count() // Thu Jan 4 12:06:05 EST 2001 // Wed Apr 18 11:22:47 EDT 2001 -- use macro support, ie 'Open()' // Thu Sep 6 13:43:00 EDT 2001 -- fixed debug printout // Wed Sep 19 12:19:00 EDT 2001 -- switched to memory mapped file use; faster // Tue Jan 8 17:16:18 EST 2002 -- fixed end of dbfile problem //------------------------------------------------------------------------- // Get number of entries in database file. Unused records have a leading // space character. Uses memory mapped 'version' of db file. // // input: db type // output: number of entries [0..n]; FILE_ERROR [-2 * 2] if error //------------------------------------------------------------------------- int get_file_count( int dbType ) { void *dbptr; // generic pointer to struct's char dbFileName[16]; int dbFileSize, entrySize, i, numOfEntries; int *FileIsMapped; extern struct MODULE *CTSdb; extern struct CRATE *CRATEdb; extern int CTSdbFileIsMapped; extern int CRATEdbFileIsMapped; switch( dbType ) { case CTS_DB: dbptr = (void *)CTSdb; entrySize = MODULE_ENTRY; FileIsMapped = &CTSdbFileIsMapped; sprintf(dbFileName, "%s", CTS_DB_FILE); break; case CRATE_DB: dbptr = (void *)CRATEdb; entrySize = CRATE_ENTRY; FileIsMapped = &CRATEdbFileIsMapped; sprintf(dbFileName, "%s", CRATE_DB_FILE); break; } if( MSGLVL(FUNCTION_NAME) ) printf( "get_file_count()\n" ); // check for memory mapped file if( *FileIsMapped == FALSE ) { if( map_data_file(dbType) != SUCCESS ) { numOfEntries = MAP_ERROR; goto GetFileCount_Exit; } } // get total db file size in bytes if(( dbFileSize = get_db_file_size(dbFileName)) < 0 ) { numOfEntries = FAILURE; goto GetFileCount_Exit; } // get the appropriate count numOfEntries = 0; for( i = 0; ; i += entrySize ) { if( (i + entrySize) > dbFileSize ) // make sure we don't fall off the end ... break; // sprintf(&ch, "%.1s", (char *)(dbptr+i)); if( *(char *)(dbptr+i) == ' ' ) // we're done, so out'a here break; ++numOfEntries; } GetFileCount_Exit: if( MSGLVL(DETAILS) ) printf( "get_file_count(%d):\n", numOfEntries ); return numOfEntries; }
//------------------------------------------------------------------------- //------------------------------------------------------------------------- int ShowModule() { char db_tmp[64]; // enough space for a logical name and a cstring terminator [2002.02.20] int i, numOfEntries, status = SUCCESS; static DESCRIPTOR( physical, "PHYSICAL" ); static DESCRIPTOR( module_p, "MODULE" ); static DESCRIPTOR( format_p, "FORMAT" ); static DESCRIPTOR( blank, " " ); static DESCRIPTOR( heading1, " # Logical Name Physical Comment" ); // header static DESCRIPTOR( heading2, "==== ================================ ========== ========================================" ); static DYNAMIC_DESCRIPTOR( wild ); struct Module_ Mod, *pMod; struct descriptor pattern; int format = cli_present(&format_p) & 1; int physical_name = cli_present(&physical) & 1; // 2002.01.16 cli_get_value( &module_p, &wild ); StrUpcase( &wild, &wild ); // convert to upper case if( MSGLVL(DETAILS) ) printf("ShowModule(): in %s order\n", (physical_name) ? "physical" : "logical" ); // check to see if db file memory mapped if( CTSdbFileIsMapped == FALSE ) { // is not, so try if( map_data_file(CTS_DB) != SUCCESS ) { // we're dead in the water if( MSGLVL(IMPORTANT) ) fprintf( stderr, "error memory mapping database file\n" ); status = FAILURE; // MAP_ERROR; [2001.07.12] goto Showmodule_Exit; } } // db file now mapped, continue pMod = &Mod; // point to some actual storage if( (numOfEntries = get_file_count(CTS_DB)) > 0 ) { // something to show printf( "%s\n", heading1.pointer ); printf( "%s%d\n", heading2.pointer, numOfEntries ); for( i = 0; i < numOfEntries; i++ ) { parse_cts_db( CTSdb+i, pMod ); // extract info from db memset(db_tmp, ' ', 32); db_tmp[33] = '\0'; if( physical_name ) // duh, physical name sprintf( db_tmp, "GK%c%d%02d:N%d", pMod->adapter + 'A', pMod->id, pMod->crate, pMod->slot ); else sprintf( db_tmp, "%s", pMod->name ); pattern.pointer = db_tmp; pattern.length = strlen(db_tmp); if( StrMatchWild( &pattern, &wild ) & 1 ) { // printf( "%s%3d: %.84s<%s\n", CYAN, i+1, (char *)CTSdb+(i * MODULE_ENTRY), NORMAL ); // fancy, with color printf( "%3d: %.84s<\n", i+1, (char *)CTSdb+(i * MODULE_ENTRY) ); } } // end of for() loop printf( "%s\n", heading2.pointer ); } else { if( MSGLVL(IMPORTANT) ) fprintf( stderr, "db file is empty, no modules to show\n" ); status = SUCCESS; // Not necessarily an ERROR; [2002.02.19] goto Showmodule_Exit; } Showmodule_Exit: if( MSGLVL(DETAILS) ) { printf("ShowModule(): "); ShowStatus(status); } return status; }
//------------------------------------------------------------------------- // add_entry() // Thu Jan 11 10:41:13 EST 2001 // Tue Apr 3 16:28:20 EDT 2001 //------------------------------------------------------------------------- // Add a cts or crate entry to the appropriate db. The new entry is inserted // into the list alphabetically. // // input: db type, // pointer to a c-string containing a complete entry // output: status //------------------------------------------------------------------------- int add_entry( int dbType, char *newEntry ) { void *dbptr; // re-usable pointer for dbs int entrySize, i, numOfEntries; int status = SUCCESS; // assume the best extern struct MODULE *CTSdb; // pointer to in-memory copy of data file extern struct CRATE *CRATEdb; // pointer to in-memory copy of data file if( MSGLVL(FUNCTION_NAME) ) printf( "add_entry()\n" ); //---------------------------- // adding an entry //---------------------------- //-- 'critical section' start //---------------------------- // 'lock' with semaphore if( lock_file() != SUCCESS ) { status = LOCK_ERROR; goto AddEntry_Exit; } // get current number of entries if( (numOfEntries = get_file_count(dbType)) < 0 ) { status = FILE_ERROR; goto AddEntry_Exit; } // cull db specific info switch( dbType ) { case CTS_DB: dbptr = (void *)CTSdb; entrySize = MODULE_ENTRY; break; case CRATE_DB: dbptr = (void *)CRATEdb; entrySize = CRATE_ENTRY; break; } // shift current entries by one if( numOfEntries ) // ... only if any entries exist for( i = numOfEntries - 1; i >= 0; --i ) memcpy( (char *)dbptr+((i + 1) * entrySize), (char *)dbptr+( i * entrySize), entrySize ); // put new entry at head of list memcpy( (char *)dbptr, newEntry, entrySize ); // insertion sort if( numOfEntries > 0 ) // only insert if more than one entry exists already if( issort(dbptr, numOfEntries + 1, entrySize, compare_str) != 0 ) { status = ERROR; goto AddEntry_Exit; } // commit change to file if( commit_entry(dbType) != SUCCESS ) { status = COMMIT_ERROR; goto AddEntry_Exit; } // release semaphore if( unlock_file() != SUCCESS ) status = UNLOCK_ERROR; //---------------------------- //-- 'critical section' finish //---------------------------- AddEntry_Exit: if( MSGLVL(DETAILS) ) { printf( "add_entry(): " ); ShowStatus( status ); } return status; }
//------------------------------------------------------------------------- // Fri Mar 9 15:12:40 EST 2001 // Thu Mar 15 13:40:01 EST 2001 // Mon Mar 26 13:49:08 EST 2001 // Tue Apr 3 16:45:23 EDT 2001 // Mon Apr 30 16:37:43 EDT 2001 -- added support for highway type // Fri Jul 6 11:37:19 EDT 2001 -- 'tighten-up' search loop //------------------------------------------------------------------------- // lookup a CAMAC device (either a serial highway or a module) and // return o/s specific device number, eg '/dev/sg#' // puts value into crate.db; leaves unchanged if not found // NB! called by 'autoconfig()' in cts::verbs //------------------------------------------------------------------------- int map_scsi_device( char *highway_name ) { char line[80], *pline, tmp[7]; char dsf[3], hwytype; int adapter, i, numOfEntries, scsi_id, sg_number; int status = SUCCESS; // optimistic int found = FALSE; FILE *fp, *fopen(); extern struct CRATE *CRATEdb; // pointer to in-memory copy of data file if( MSGLVL(FUNCTION_NAME) ) printf( "map_scsi_device('%s')\n", highway_name ); // open '/proc' filesystem scsi info if( (fp = fopen(PROC_FILE, "r")) == NULL ) { if( MSGLVL(ALWAYS) ) fprintf( stderr, "failure to open '%s'\n", PROC_FILE ); status = FILE_ERROR; // serious error !!! no scsi devices to check goto MapScsiDevice_Exit; } // get current db file count if( (numOfEntries = get_file_count( CRATE_DB )) <= 0 ) { status = FILE_ERROR; goto MapScsiDevice_Exit; // we're done :< } if( MSGLVL(DETAILS) ) printf( "crate.db count= %d\n", numOfEntries ); // lookup highway name if( MSGLVL(DETAILS) ) printf("msd() looking up '%s'\n", highway_name); if( (i = lookup_entry( CRATE_DB, highway_name )) < 0 ) { status = NO_DEVICE; // no such device in db file goto MapScsiDevice_Exit; } if( MSGLVL(DETAILS) ) printf( "msd(): lookup index [%d]:%s\n", i, highway_name ); // point to actual memory pline = &line[0]; // scan all scsi devices sg_number = 0; // start at the beginning while( !found && (pline = fgets(line, sizeof(line), fp)) != NULL ) { if( strncmp(pline, "Host:", 5) == EQUAL ) { sscanf(line, "Host: scsi%d Channel: %*2c Id: %d %*s", &adapter, &scsi_id); sprintf(tmp, "GK%c%d", 'A' + adapter, scsi_id); if( strncmp(tmp, highway_name, 4) == EQUAL ) { // found it if( QueryHighwayType( tmp ) == SUCCESS ) // determine highway type hwytype = tmp[5]; // we're done, so exit found = TRUE; } else sg_number++; } // end of if() .... } // end of while() ... // 'lock' file with semaphore if( lock_file() != SUCCESS ) { status = FAILURE; // LOCK_ERROR; [2001.07.12] goto MapScsiDevice_Exit; } // update memory mapped version if( found ) { sprintf(dsf, "%03d", sg_number); // format conversion strncpy((CRATEdb+i)->DSFname, dsf, 3); // real device number (CRATEdb+i)->HwyType = hwytype; // highway type } else { strncpy((CRATEdb+i)->DSFname, "...", 3); // place-holder device number (CRATEdb+i)->HwyType = '.'; } // commit changes to file if( commit_entry( CRATE_DB ) != SUCCESS ) { status = FAILURE; // COMMIT_ERROR; [2001.07.12] goto MapScsiDevice_Exit; } if( MSGLVL(DETAILS) ) printf( "'%.4s+%.3s+%c'\n", highway_name, (CRATEdb+i)->DSFname, (CRATEdb+i)->HwyType ); // unlock file if( unlock_file() != SUCCESS ) { status = FAILURE; // UNLOCK_ERROR; [2001.07.12] goto MapScsiDevice_Exit; } MapScsiDevice_Exit: // cleanup if( fp ) fclose(fp); return status; }
//----------------------------------------------------------- static int JorwayDoIo( CamKey Key, BYTE A, BYTE F, int Count, BYTE *Data, BYTE Mem, TranslatedIosb *iosb, int dmode, int Enhanced ) { char dev_name[7]; int IsDataCommand, scsiDevice; int xfer_data_length; int status; unsigned char *cmd; unsigned char cmdlen; int direction; unsigned int bytcnt; int reqbytcnt = 0; SenseData sense; char sensretlen; int online; int enhanced; CDBCamacDataCommand( DATAcommand ); CDBCamacNonDataCommand( NONDATAcommand ); if( MSGLVL(FUNCTION_NAME) ) printf( "%s()\n", J_ROUTINE_NAME ); //printf( "%s(iosb is %sNULL)\n", J_ROUTINE_NAME, (iosb)?"NOT ":"" ); // [2002.12.13] sprintf(dev_name, "GK%c%d%02d", Key.scsi_port, Key.scsi_address, Key.crate); if( (scsiDevice = get_scsi_device_number( dev_name, &enhanced, &online )) < 0 ) { if( MSGLVL(IMPORTANT) ) fprintf( stderr, "%s(): error -- no scsi device found for '%s'\n", J_ROUTINE_NAME, dev_name ); status = NO_DEVICE; goto JorwayDoIo_Exit; } if (!online && Key.slot != 30) return CamOFFLINE; if (!Enhanced) enhanced = 0; if( MSGLVL(DETAILS) ) printf( "%s(): device '%s' = '/dev/sg%d'\n", J_ROUTINE_NAME, dev_name, scsiDevice ); IsDataCommand = (F & 0x08) ? FALSE : TRUE; if( IsDataCommand ) { union { BYTE b[4]; unsigned int l; } transfer_len; DATAcommand.f = F; DATAcommand.bs = Mem == 24; DATAcommand.n = Key.slot; DATAcommand.m = JORWAYMODE(dmode, enhanced, Count > 1); DATAcommand.a = A; DATAcommand.sncx = 0; DATAcommand.scs = 0; reqbytcnt = transfer_len.l = Count * ((Mem == 24) ? 4 : 2); # if JORWAY_DISCONNECT # ifdef SG_BIG_BUFF DATAcommand.dd = transfer_len.l > SG_BIG_BUFF; # else DATAcommand.dd = transfer_len.l > 4096; # endif # else DATAcommand.dd = 0; # endif DATAcommand.crate = Key.crate; DATAcommand.sp = HIGHWAY_SERIAL; DATAcommand.transfer_len[0] = transfer_len.b[2]; // NB! order reversal DATAcommand.transfer_len[1] = transfer_len.b[1]; DATAcommand.transfer_len[2] = transfer_len.b[0]; cmd = (unsigned char *)&DATAcommand; cmdlen = sizeof(DATAcommand); direction = (F < 8) ? 1 : 2; } else { NONDATAcommand.bytes[1] = F; NONDATAcommand.bytes[2] = Key.slot; NONDATAcommand.bytes[3] = A; NONDATAcommand.bytes[4] = (HIGHWAY_SERIAL << 7) | Key.crate; cmd = (unsigned char *)&NONDATAcommand; cmdlen = sizeof(NONDATAcommand); direction = 0; } memset(&sense,0,sizeof(sense)); sensretlen=0; bytcnt=0; scsi_lock(scsiDevice,1); status = scsi_io( scsiDevice, direction, cmd, cmdlen, Data, reqbytcnt, (unsigned char *)&sense, sizeof(sense), &sensretlen, &bytcnt); scsi_lock(scsiDevice,0); status = JorwayTranslateIosb(reqbytcnt,&sense,sensretlen,bytcnt,status); if ( iosb ) *iosb = LastIosb; // [2002.12.11] JorwayDoIo_Exit: if( MSGLVL(DETAILS) ) { printf( "%s(): iosb->status [0x%x]\n", J_ROUTINE_NAME, iosb->status ); printf( "%s(): iosb->x [0x%x]\n", J_ROUTINE_NAME, iosb->x ); printf( "%s(): iosb->q [0x%x]\n", J_ROUTINE_NAME, iosb->q ); //printf( "%s(): iosb->bytcnt [%d]\n", J_ROUTINE_NAME, iosb->bytcnt); // [2002.12.11] } //printf("JorwayDoIo(iosb)::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] return status; }
//----------------------------------------------------------- static int MultiIo( CamKey Key, BYTE A, BYTE F, int Count, BYTE *Data, BYTE Mem, TranslatedIosb *iosb, int dmode, int Enhanced ) { char tmp[7]; int highwayType, mode, status; if( MSGLVL(FUNCTION_NAME) ) printf( "\033[01;31mMultiIo(F=%d, count=%d)-->>\033[0m\n", F, Count ); sprintf(tmp, "GK%c%d", Key.scsi_port, Key.scsi_address); if( (status = QueryHighwayType( tmp )) == SUCCESS ) { highwayType = NUMERIC(tmp[5]); // extract type switch( highwayType ) { // check highway type case JORWAY: case JORWAY_OLD: if( MSGLVL(DETAILS) ) printf( "-->>JorwayDoIo()\n" ); mode = JORWAYMODE(dmode, (Enhanced && highwayType == JORWAY), Count > 1); if( mode != NO_MODE ) status = JorwayDoIo( Key, // module info A, // module sub address F, // module function Count, // data count int bytes Data, // data Mem, // 16 or 24 bit data iosb, // status struct dmode, // mode Enhanced // highway mode ); else status = NOT_SUPPORTED(); break; case KINSYSCO: if( MSGLVL(DETAILS) ) printf( "-->>KsMultiIo()\n" ); status = KsMultiIo( Key, // module info A, // module sub-address F, // module function Count, // data count in bytes Data, // data Mem, // 16 or 24 bit data iosb, // status struct KSMODE(dmode), // mode Enhanced // enhanced ); break; case JORWAY_73A: mode = JORWAYMODE(dmode, (Enhanced && highwayType == JORWAY), Count > 1); if( mode != NO_MODE ) status = Jorway73ADoIo( Key, // module info A, // module sub address F, // module function Count, // data count int bytes Data, // data Mem, // 16 or 24 bit data iosb, // status struct dmode, // mode Enhanced // highway mode ); else status = NOT_SUPPORTED(); break; default: if( MSGLVL(IMPORTANT) ) fprintf( stderr, "highway type(%d) not supported\n", highwayType ); status = FAILURE; break; } // end of switch() } // end of if() if( MSGLVL(DETAILS) ) { if (!iosb) printf("MultiIo null iosb ptr"); else printf("MultiIo(iosb)::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] } return status; }
//----------------------------------------------------------- static int SingleIo( CamKey Key, BYTE A, BYTE F, BYTE *Data, BYTE Mem, TranslatedIosb *iosb, int dmode ) { char tmp[7]; int highwayType, status; if( MSGLVL(FUNCTION_NAME) ) printf( "\033[01;31mSingleIo(F=%d)-->\033[0m", F ); sprintf(tmp, "GK%c%d", Key.scsi_port, Key.scsi_address); if( (status = QueryHighwayType( tmp )) == SUCCESS ) { highwayType = NUMERIC(tmp[5]); // extract type switch( highwayType ) { // check highway type case JORWAY: case JORWAY_OLD: if( MSGLVL(DETAILS) ) printf( "-->>JorwayDoIo()\n" ); status = JorwayDoIo( Key, // module info A, // module sub address F, // module function 1, // implied count of 1 Data, // data Mem, // 16 or 24 bit data iosb, // status struct dmode, // mode 0 // non-enhanced ); break; case KINSYSCO: if( MSGLVL(DETAILS) ) printf( "-->>KsSingleIo()\n" ); status = KsSingleIo( Key, // module info A, // module sub-address F, // module function Data, // data Mem, // 16 or 24 bit data iosb, // status struct KSMODE(dmode) // mode ); break; case JORWAY_73A: if( MSGLVL(DETAILS) ) printf( "-->>JorwayDoIo()\n" ); status = Jorway73ADoIo( Key, // module info A, // module sub address F, // module function 1, // implied count of 1 Data, // data Mem, // 16 or 24 bit data iosb, // status struct dmode, // mode 0 // non-enhanced ); break; default: if( MSGLVL(IMPORTANT) ) fprintf( stderr, "highway type(%d) not supported\n", highwayType ); status = FAILURE; break; } // end of switch() if( MSGLVL(FUNCTION_NAME) ) // show data, if there is some if (Data) { if (Mem==16) printf( "\033[01;31mSingleIo(F=%d)-->>%d\033[0m\n", F, *(short *) Data ); else printf( "\033[01;31mSingleIo(F=%d)-->>%d\033[0m\n", F, *(int *) Data ); } } // end of if() //printf("SingleIo(iosb)::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] return status; }
//------------------------------------------------------------------------- // Add a crate to the crate db //------------------------------------------------------------------------- int AddCrate() { char line[CRATE_ENTRY + 1]; int dbFileSize, fd, numOfEntries; int status = SUCCESS; // assume the best static DESCRIPTOR( phy_name_p, "PHY_NAME" ); static DYNAMIC_DESCRIPTOR( phy_name ); // get user input str_free1_dx(&phy_name); // per twf -- clear out field cli_get_value( &phy_name_p, &phy_name ); str_upcase( &phy_name, &phy_name ); // [2002.01.08] if( CRATEdbFileIsMapped == FALSE ) { // ... no if( check_for_file(CRATE_DB_FILE) != SUCCESS ) { // ... no if( (fd = Creat(CRATE_DB_FILE, 0666)) == ERROR ) { // no status = FAILURE; goto AddCrate_Exit; } else close(fd); // yes } if( map_data_file(CRATE_DB) != SUCCESS ) { // failure :( status = MAP_ERROR; goto AddCrate_Exit; } // else OK :) } // get current db file count if( (numOfEntries = get_file_count(CRATE_DB)) == FILE_ERROR ) { status = FAILURE; // FILE_ERROR; [2001.07.12] goto AddCrate_Exit; } if( numOfEntries ) { // 1 or more char pname[7]; sprintf(pname,"%.6s",phy_name.pointer); if( lookup_entry(CRATE_DB, pname) >= 0 ) { // duplicate ! if( MSGLVL(IMPORTANT) ) fprintf( stderr, "duplicate crate name '%.6s' -- not allowed\n", phy_name.pointer ); status = FAILURE; // DUPLICATE; [2001.07.12] goto AddCrate_Exit; } } // get db file size if( (dbFileSize = get_db_file_size(CRATE_DB_FILE)) == ERROR ) { status = FAILURE; // FILE_ERROR; [2001.07.12] goto AddCrate_Exit; } dbFileSize /= CRATE_ENTRY; // .. current maximum number of possible crate entries // do we need to expand db file? if( (dbFileSize == 0) || (numOfEntries == dbFileSize) ) { // ... yes if( (status = expand_db(CRATE_DB, numOfEntries)) != SUCCESS ) { // expand status = FAILURE; // EXPAND_ERROR; [2001.07.12] // failure goto AddCrate_Exit; } } // else OK // make an entry line, with online and enhanced set as undefined sprintf( line, "%-.6s:...:.:.:.\n", phy_name.pointer ); // add it ... if( (status = add_entry(CRATE_DB, line)) != SUCCESS ) { status = FAILURE; // ASSIGN_ERROR; [2001.07.12] goto AddCrate_Exit; } AddCrate_Exit: if( MSGLVL(DETAILS) ) { printf("AddCrate(): "); ShowStatus(status); } if( status == SUCCESS ) // if everything is OK ... Autoconfig(); // ... map crate to /dev/sg# //ShowCrate(); return status; }
//----------------------------------------------------------- void Blank( UserParams *user ) { if( MSGLVL(FUNCTION_NAME) ) printf( "Blank()\n" ); }
//----------------------------------------------------------- static int Jorway73ADoIo( CamKey Key, BYTE A, BYTE F, int Count, BYTE *Data, BYTE Mem, TranslatedIosb *iosb, int dmode, int Enhanced ) { char dev_name[7]; int IsDataCommand, scsiDevice; int xfer_data_length; int status; unsigned char *cmd; unsigned char cmdlen; int direction; unsigned int bytcnt; int reqbytcnt = 0; J73ASenseData sense; char sensretlen; int online; int enhanced; struct { __u8 opcode; __u8 f : 5; __u8 lu : 3; __u8 n : 5; __u8 bs : 1; __u8 m : 2; __u8 a : 4; __u8 zero1 : 4; __u8 transfer_len; __u8 zero2; } NONDATAcommand = {1,0,0,0,0,0,0,0,0,0}; struct { __u8 opcode; __u8 f : 5; __u8 lu : 3; __u8 n : 5; __u8 bs : 1; __u8 m : 2; __u8 a : 4; __u8 zero1 : 4; __u8 transfer_len; __u8 zero2; } ShortDATAcommand = {1,0,0,0,0,0,0,0,0,0}; struct { __u8 opcode; __u8 zero1 : 5; __u8 lu : 3; __u8 f : 5; __u8 zero2 : 3; __u8 n : 5; __u8 bs : 1; __u8 m : 2; __u8 a : 4; __u8 zero3 : 4; __u8 zero4; __u8 transfer_len[3]; __u8 zero5; } LongDATAcommand = {0x21,0,0,0,0,0,0,0,0,0}; static char modes[4] = {2,2,3,1}; /* QStop, QIgnore, QRep, QScan */ static char singlemodes[4] = {0,2,3,1}; if( MSGLVL(FUNCTION_NAME) ) printf( "%s()\n", J_ROUTINE_NAME ); //printf( "%s(iosb is %sNULL)\n", J_ROUTINE_NAME, (iosb)?"NOT ":"" ); // [2002.12.13] sprintf(dev_name, "GK%c%d%02d", Key.scsi_port, Key.scsi_address, Key.crate); if( (scsiDevice = get_scsi_device_number( dev_name, &enhanced, &online )) < 0 ) { if( MSGLVL(IMPORTANT) ) fprintf( stderr, "%s(): error -- no scsi device found for '%s'\n", J_ROUTINE_NAME, dev_name ); status = NO_DEVICE; goto Jorway73ADoIo_Exit; } if (!online && Key.slot != 30) return CamOFFLINE; if (online && Key.slot == 30 && F == 1 && A == 0) { *(short *)Data = 0x30; return CamDONE_Q; } if( MSGLVL(DETAILS) ) printf( "%s(): device '%s' = '/dev/sg%d'\n", J_ROUTINE_NAME, dev_name, scsiDevice ); IsDataCommand = (F & 0x08) ? FALSE : TRUE; if( IsDataCommand ) { union { BYTE b[4]; unsigned int l; } transfer_len; reqbytcnt = transfer_len.l = Count * ((Mem == 24) ? 4 : 2); direction = (F < 8) ? 1 : 2; if (reqbytcnt < 256) { cmd = (char *)&ShortDATAcommand; cmdlen = sizeof(ShortDATAcommand); ShortDATAcommand.f = F; ShortDATAcommand.bs = Mem == 24; ShortDATAcommand.n = Key.slot; ShortDATAcommand.m = Count > 1 ? modes[dmode] : singlemodes[dmode]; ShortDATAcommand.a = A; ShortDATAcommand.transfer_len = transfer_len.l; } else { cmd = (char *)&LongDATAcommand; cmdlen = sizeof(LongDATAcommand); LongDATAcommand.f = F; LongDATAcommand.bs = Mem == 24; LongDATAcommand.n = Key.slot; LongDATAcommand.m = modes[dmode]; LongDATAcommand.a = A; LongDATAcommand.transfer_len[0] = transfer_len.b[2]; // NB! order reversal LongDATAcommand.transfer_len[1] = transfer_len.b[1]; LongDATAcommand.transfer_len[2] = transfer_len.b[0]; } } else { NONDATAcommand.f = F; NONDATAcommand.n = Key.slot; NONDATAcommand.a = A; cmd = (unsigned char *)&NONDATAcommand; cmdlen = sizeof(NONDATAcommand); direction = 0; } scsi_lock(scsiDevice,1); status = scsi_io( scsiDevice, direction, cmd, cmdlen, Data, reqbytcnt, (unsigned char *)&sense, sizeof(sense), &sensretlen, &bytcnt); scsi_lock(scsiDevice,0); status = Jorway73ATranslateIosb(IsDataCommand, reqbytcnt,&sense,status); if ( iosb ) *iosb = LastIosb; // [2002.12.11] Jorway73ADoIo_Exit: if( MSGLVL(FUNCTION_NAME+1) ) { // This is only rough - depends on the nature of the "overloaded" vars printf("scsi_mode opcode=%d, dmode=%d, modes[dmode]=%d, [1]=%d, [3]=%d, [5]=%d [7]=%d\n", cmd[0], dmode, modes[dmode], cmd[1], cmd[3], cmd[5], cmd[7]); if (!iosb) {printf("Jorway73ADoIo_Exit: Null pointer to iosb\n"); } else { printf( "%s(): iosb->status [0x%x]\n", J_ROUTINE_NAME, iosb->status ); printf( "%s(): iosb->x [0x%x]\n", J_ROUTINE_NAME, iosb->x ); printf( "%s(): iosb->q [0x%x]\n", J_ROUTINE_NAME, iosb->q ); //printf( "%s(): iosb->bytcnt [%d]\n", J_ROUTINE_NAME, iosb->bytcnt); // [2002.12.11] } } //printf("Jorway73ADoIo(iosb)::->> bytecount= %d\n", iosb->bytcnt); // [2002.12.13] return status; }
// extract CAMAC status info for Jorway highways //----------------------------------------------------------- static int JorwayTranslateIosb( int reqbytcnt, SenseData *sense, char sensretlen, unsigned int ret_bytcnt, int scsi_status ) { int status; int bytcnt = reqbytcnt - ((int)sense->word_count_defect[2])+ (((int)sense->word_count_defect[1])<<8)+ (((int)sense->word_count_defect[0])<<16); LastIosb.bytcnt = (unsigned short)(bytcnt & 0xffff); LastIosb.lbytcnt = (unsigned short)(bytcnt >> 16); LastIosb.x=0; LastIosb.q=0; LastIosb.err=0; LastIosb.lpe=0; LastIosb.tpe=0; LastIosb.no_sync=0; LastIosb.tmo=0; LastIosb.adnr=0; LastIosb.list=0; if( MSGLVL(FUNCTION_NAME) ) printf( "%s()\n", JT_ROUTINE_NAME ); if( MSGLVL(DETAILS) ) { printf( "%s(): scsi status 0x%x\n", JT_ROUTINE_NAME, scsi_status ); } LastIosb.q = !sense->main_status_reg.no_q; LastIosb.x = !sense->main_status_reg.no_x; status = CamSERTRAERR; switch (scsi_status) { case 0: status = 1; break; case 1: { switch(sense->sense_key) { case SENSE_HARDWARE_ERROR: if (sense->additional_sense_code == SENSE2_NOX) { LastIosb.q = 0; LastIosb.x = 0; if (sense->main_status_reg.snex) status = CamSCCFAIL; else status = CamDONE_NOX; } else if (sense->additional_sense_code == SENSE2_NOQ) { LastIosb.q = 0; status = CamDONE_NOQ; } else { LastIosb.err = 1; LastIosb.tmo = sense->main_status_reg.to | sense->serial_status_reg.timos; LastIosb.no_sync = sense->serial_status_reg.losyn | sense->serial_status_reg.sync; LastIosb.lpe = LastIosb.tpe = sense->serial_status_reg.rpe; } break; case SENSE_SHORT_TRANSFER: LastIosb.q = 0; status = CamDONE_NOQ; break; case SENSE_UNIT_ATTENTION: LastIosb.q = !sense->main_status_reg.no_q; LastIosb.x = !sense->main_status_reg.no_x; LastIosb.tmo = sense->main_status_reg.to | sense->serial_status_reg.timos; LastIosb.no_sync = sense->serial_status_reg.losyn | sense->serial_status_reg.sync; LastIosb.lpe = LastIosb.tpe = sense->serial_status_reg.rpe; LastIosb.err = LastIosb.lpe || LastIosb.no_sync || LastIosb.tmo; if (LastIosb.err) break; if (!LastIosb.x) status = CamDONE_NOX; else if (!LastIosb.q) status = CamDONE_NOQ; else status = CamDONE_Q; break; } } break; case 2: if (!LastIosb.x) status = CamDONE_NOX; else if (!LastIosb.q) status = CamDONE_NOQ; else status = CamDONE_Q; break; } LastIosb.status = (unsigned short)status&0xffff; if (Verbose || status == CamSERTRAERR) { if (status == CamSERTRAERR) { printf("Serial Transmission Error detected, debug information follows \n\n" "******************************************************************\n"); } printf("Sense return length: %d, bytcnt: %d, scsi_status: %d, reqbytcnt: %d\n",sensretlen,ret_bytcnt,scsi_status,reqbytcnt); printf("SCSI Sense data: code=%d,valid=%d,sense_key=%d,word count deficit=%d\n\n",sense->code,sense->valid, sense->sense_key, ((int)sense->word_count_defect[2])+ (((int)sense->word_count_defect[1])<<8)+ (((int)sense->word_count_defect[0])<<16)); printf(" Main status register:\n\n"); printf(" bdmd=%d,dsne=%d,bdsq=%d,snex=%d,crto=%d,to=%d,no_x=%d,no_q=%d\n\n", sense->main_status_reg.bdmd,sense->main_status_reg.dsne,sense->main_status_reg.bdsq, sense->main_status_reg.snex,sense->main_status_reg.crto,sense->main_status_reg.to, sense->main_status_reg.no_x,sense->main_status_reg.no_q); printf(" Serial status register:\n\n"); printf(" cret=%d,timos=%d,rpe=%d,hdrrec=%d,cmdfor=%d,rnre1=%d,rnrg1=%d,snex=%d,hngd=%d\n", sense->serial_status_reg.cret,sense->serial_status_reg.timos,sense->serial_status_reg.rpe, sense->serial_status_reg.hdrrec,sense->serial_status_reg.cmdfor, sense->serial_status_reg.rnre1,sense->serial_status_reg.rnrg1, sense->serial_status_reg.snex,sense->serial_status_reg.hngd); printf(" sync=%d,losyn=%d,rerr=%d,derr=%d\n\n",sense->serial_status_reg.sync, sense->serial_status_reg.losyn,sense->serial_status_reg.rerr, sense->serial_status_reg.derr); printf(" Additional Sense Code=%d,slot=%d,crate=%d\n\n",sense->additional_sense_code, sense->slot_high_bit * 16 + sense->slot,sense->crate); if (status == CamSERTRAERR) { printf("******************************************************************\n\n"); } } return status; }
//------------------------------------------------------------------------- // contract_db() // Wed Jan 30 10:44:01 EST 2002 //------------------------------------------------------------------------- // Contract the CTS or CRATE db files // // input: db type, // current number of entries in CTS data file // output: status //------------------------------------------------------------------------- int contract_db( int dbType, int numOfEntries ) { char *FileName; int FileIncr, newCount; int status = SUCCESS; // optimistic, aren't we ... :> char tmpfile[1024]; strcpy(tmpfile,get_file_name("mdscts_temp_file_XXXXXX")); if( MSGLVL(FUNCTION_NAME) ) printf( "contract_db()\n" ); // assimilate db specific information ... switch( dbType ) { case CTS_DB: FileName = CTS_DB_FILE; FileIncr = CTS_DB_INCREMENT; break; case CRATE_DB: FileName = CRATE_DB_FILE; FileIncr = CRATE_DB_INCREMENT; break; } // make db file smaller .... [2002.01.30] // // // calculate new db file size newCount = (((int)numOfEntries / FileIncr) + 1) * FileIncr; // create a TMP file if( (status = create_tmp_file( dbType, newCount, tmpfile )) != SUCCESS ) { if( MSGLVL(ALWAYS) ) fprintf( stderr, "error creating TMP file\n" ); goto ContractDB_Exit; } // only need to copy old data if there is any if( numOfEntries ) { // copy current db file to TMP file if( (status = copy( dbType, FileName, tmpfile, numOfEntries )) != SUCCESS ) { if( MSGLVL(ALWAYS) ) fprintf( stderr, "error copying db to TMP file\n" ); goto ContractDB_Exit; } // remove old db file if( Remove( FileName ) ) { // non-zero is an error if( MSGLVL(ALWAYS) ) { fprintf( stderr, "error removing old db file\n" ); perror("remove()"); } status = CONTRACT_ERROR; goto ContractDB_Exit; } } if( rename(tmpfile, get_file_name( FileName )) ) { // non-zero is an error if( MSGLVL(ALWAYS) ) { fprintf( stderr, "error renaming temp db file\n" ); perror("rename()"); } status = CONTRACT_ERROR; goto ContractDB_Exit; } chmod(get_file_name( FileName ), O666); // re-map file if( map_data_file( dbType ) != SUCCESS ) { if( MSGLVL(ALWAYS) ) fprintf( stderr, "unable to map contracted file\n" ); status = MAP_ERROR; goto ContractDB_Exit; } ContractDB_Exit: if( MSGLVL(DETAILS) ) { printf( "contract_db('%s'): ", get_file_name( FileName )); ShowStatus( status ); } return status; }
//------------------------------------------------------------------------- // assign a new module to CTS database //------------------------------------------------------------------------- int Assign() { char line[MODULE_ENTRY+1]; int dbFileSize, fd, i, nullMask, numOfEntries, rc; int status = SUCCESS; // assume the best static DESCRIPTOR( phy_name_p, "PHY_NAME" ); static DYNAMIC_DESCRIPTOR( phy_name ); static DESCRIPTOR( log_name_p, "LOG_NAME" ); static DYNAMIC_DESCRIPTOR( log_name ); static DESCRIPTOR( comment_p, "COMMENT" ); static DYNAMIC_DESCRIPTOR( comment ); // get user input cli_get_value( &phy_name_p, &phy_name ); str_upcase( &phy_name, &phy_name ); cli_get_value( &log_name_p, &log_name ); str_upcase( &log_name, &log_name ); str_free1_dx(&comment); // per twf -- clear out comment field cli_get_value( &comment_p, &comment ); // check to see if db file exists if( check_for_file(CTS_DB_FILE) != SUCCESS ) { // does not exist, yet // try to creat (sic) it if( (fd = Creat(CTS_DB_FILE, 0666)) == ERROR ) { status = FAILURE; // FILE_ERROR; [2001.07.12] goto Assign_Exit; // we're done :< } else close(fd); } // check to see if db file is memory mapped if( CTSdbFileIsMapped == FALSE ) { // is not, so try if( map_data_file(CTS_DB) != SUCCESS ) { // we're dead in the water status = FAILURE; // MAP_ERROR; [2001.07.12] goto Assign_Exit; } } // get current db file count if( (numOfEntries = get_file_count(CTS_DB)) < 0 ) { status = FAILURE; // FILE_ERROR; [2001.07.12] goto Assign_Exit; } if( numOfEntries ) { // 1 or more if( lookup_entry(CTS_DB, log_name.pointer) >= 0 ) { // duplicate ! if( MSGLVL(IMPORTANT) ) fprintf( stderr, "duplicate module name '%s' -- not allowed\n", log_name.pointer ); status = FAILURE; // DUPLICATE; [2001.07.12] goto Assign_Exit; } } // get db file size if( (dbFileSize = get_db_file_size(CTS_DB_FILE)) == ERROR ) { status = FAILURE; // FILE_ERROR; [2001.07.12] goto Assign_Exit; } dbFileSize /= MODULE_ENTRY; // .. current maximum number of possible module entries // do we need to expand db file? if( (dbFileSize == 0) || (numOfEntries == dbFileSize) ) { // ... yes if( expand_db(CTS_DB, numOfEntries) != SUCCESS ) { // expand ... status = FAILURE; // EXPAND_ERROR; [2001.07.12] // ... failure goto Assign_Exit; } } // else OK // create a temporary string sprintf( line, "%-32s %-10s %-40s\n", log_name.pointer, // these were entered by the user phy_name.pointer, comment.pointer ? comment.pointer : "" ); // check comment field for null string, ie "(null)" nullMask = (1 << strlen(nullStr)) - 1; // set all mask bits for( i = COMMENT_INDEX; i < COMMENT_INDEX + strlen(nullStr); ++i ) if( line[i] == nullStr[i - COMMENT_INDEX] ) nullMask &= ~(1 << (i - COMMENT_INDEX)); // clear a bit in mask if( nullMask == 0 ) // all mask bit have been reset, ie matched for( i = COMMENT_INDEX; i < COMMENT_INDEX + strlen(nullStr); ++i ) line[i] = ' '; // make it spaces // add it ... if( add_entry(CTS_DB, line) != SUCCESS ) { status = FAILURE; // ASSIGN_ERROR; [2001.07.12] goto Assign_Exit; } #if NEED_WARM_N_FUZZY // write to a buffer file for a warm fuzzy ... if( (fd = Creat("buffer.db", 0666)) == ERROR ) { if( MSGLVL(ALWAYS) ) perror("creat()"); status = FAILURE; // FILE_ERROR; [2001.07.12] goto Assign_Exit; } rc = write(fd, line, sizeof(line)); close(fd); if( rc != sizeof(line) ) { if( MSGLVL(ALWAYS) ) perror("write()"); status = FAILURE; // FILE_ERROR; [2001.07.12] } #endif Assign_Exit: // we're done, so out'a here! if( MSGLVL(DETAILS) ) { printf("Assign(): "); ShowStatus(status); } return status; }