dpt_i2o.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:85k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /***************************************************************************
  2.                           dpti.c  -  description
  3.                              -------------------
  4.     begin                : Thu Sep 7 2000
  5.     copyright            : (C) 2000 by Adaptec
  6.     email                : deanna_bonds@adaptec.com
  7.         July 30, 2001 First version being submitted
  8.    for inclusion in the kernel.  V2.4
  9.     See README.dpti for history, notes, license info, and credits
  10.  ***************************************************************************/
  11. /***************************************************************************
  12.  *                                                                         *
  13.  *   This program is free software; you can redistribute it and/or modify  *
  14.  *   it under the terms of the GNU General Public License as published by  *
  15.  *   the Free Software Foundation; either version 2 of the License, or     *
  16.  *   (at your option) any later version.                                   *
  17.  *                                                                         *
  18.  ***************************************************************************/
  19. //#define DEBUG 1
  20. //#define UARTDELAY 1
  21. // On the real kernel ADDR32 should always be zero for 2.4. GFP_HIGH allocates
  22. // high pages. Keep the macro around because of the broken unmerged ia64 tree
  23. #define ADDR32 (0)
  24. #include <linux/version.h>
  25. #include <linux/module.h>
  26. MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
  27. MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
  28. ////////////////////////////////////////////////////////////////
  29. #include <linux/ioctl.h> /* For SCSI-Passthrough */
  30. #include <asm/uaccess.h>
  31. #include <linux/stat.h>
  32. #include <linux/slab.h> /* for kmalloc() */
  33. #include <linux/config.h> /* for CONFIG_PCI */
  34. #include <linux/pci.h> /* for PCI support */
  35. #include <linux/proc_fs.h>
  36. #include <linux/blk.h>
  37. #include <linux/delay.h> /* for udelay */
  38. #include <linux/tqueue.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/kernel.h> /* for printk */
  41. #include <linux/sched.h>
  42. #include <linux/reboot.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/timer.h>
  45. #include <linux/string.h>
  46. #include <linux/ioport.h>
  47. #include <linux/stat.h>
  48. #include <asm/processor.h> /* for boot_cpu_data */
  49. #include <asm/pgtable.h>
  50. #include <asm/io.h> /* for virt_to_bus, etc. */
  51. #include "scsi.h"
  52. #include "hosts.h"
  53. #include "sd.h"
  54. #include "dpt/dptsig.h"
  55. #include "dpti.h"
  56. /*============================================================================
  57.  * Create a binary signature - this is read by dptsig
  58.  * Needed for our management apps
  59.  *============================================================================
  60.  */
  61. static dpt_sig_S DPTI_sig = {
  62. {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
  63. #ifdef __i386__
  64. PROC_INTEL, PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
  65. #elif defined(__ia64__)
  66. PROC_INTEL, PROC_IA64,
  67. #elif defined(__sparc__)
  68. PROC_ULTRASPARC,
  69. #elif defined(__alpha__)
  70. PROC_ALPHA ,
  71. #else
  72. (-1),(-1)
  73. #endif
  74.  FT_HBADRVR, 0, OEM_DPT, OS_LINUX, CAP_OVERLAP, DEV_ALL,
  75. ADF_ALL_SC5, 0, 0, DPT_VERSION, DPT_REVISION, DPT_SUBREVISION,
  76. DPT_MONTH, DPT_DAY, DPT_YEAR, "Adaptec Linux I2O RAID Driver"
  77. };
  78. /*============================================================================
  79.  * Globals
  80.  *============================================================================
  81.  */
  82. DECLARE_MUTEX(adpt_configuration_lock);
  83. static struct i2o_sys_tbl *sys_tbl = NULL;
  84. static int sys_tbl_ind = 0;
  85. static int sys_tbl_len = 0;
  86. static adpt_hba* hbas[DPTI_MAX_HBA];
  87. static adpt_hba* hba_chain = NULL;
  88. static int hba_count = 0;
  89. static struct file_operations adpt_fops = {
  90. ioctl: adpt_ioctl,
  91. open: adpt_open,
  92. release: adpt_close
  93. };
  94. #ifdef REBOOT_NOTIFIER
  95. static struct notifier_block adpt_reboot_notifier =
  96. {
  97.  adpt_reboot_event,
  98.  NULL,
  99.  0
  100. };
  101. #endif
  102. /* Structures and definitions for synchronous message posting.
  103.  * See adpt_i2o_post_wait() for description
  104.  * */
  105. struct adpt_i2o_post_wait_data
  106. {
  107. int status;
  108. u32 id;
  109. adpt_wait_queue_head_t *wq;
  110. struct adpt_i2o_post_wait_data *next;
  111. };
  112. static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
  113. static u32 adpt_post_wait_id = 0;
  114. static spinlock_t adpt_post_wait_lock = SPIN_LOCK_UNLOCKED;
  115. /*============================================================================
  116.  *  Functions
  117.  *============================================================================
  118.  */
  119. static u8 adpt_read_blink_led(adpt_hba* host)
  120. {
  121. if(host->FwDebugBLEDflag_P != 0) {
  122. if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
  123. return readb(host->FwDebugBLEDvalue_P);
  124. }
  125. }
  126. return 0;
  127. }
  128. /*============================================================================
  129.  * Scsi host template interface functions
  130.  *============================================================================
  131.  */
  132. static struct pci_device_id dptids[] = {
  133. { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  134. { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  135. { 0, }
  136. };
  137. MODULE_DEVICE_TABLE(pci,dptids);
  138. static int adpt_detect(Scsi_Host_Template* sht)
  139. {
  140. struct pci_dev *pDev = NULL;
  141. adpt_hba* pHba;
  142. adpt_init();
  143. sht->use_new_eh_code = 1;
  144. PINFO("Detecting Adaptec I2O RAID controllers...n");
  145.         /* search for all Adatpec I2O RAID cards */
  146. while ((pDev = pci_find_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
  147. if(pDev->device == PCI_DPT_DEVICE_ID ||
  148.    pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
  149. if(adpt_install_hba(sht, pDev) ){
  150. PERROR("Could not Init an I2O RAID devicen");
  151. PERROR("Will not try to detect others.n");
  152. return hba_count-1;
  153. }
  154. }
  155. }
  156. /* In INIT state, Activate IOPs */
  157. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  158. // Activate does get status , init outbound, and get hrt
  159. if (adpt_i2o_activate_hba(pHba) < 0) {
  160. adpt_i2o_delete_hba(pHba);
  161. }
  162. }
  163. /* Active IOPs in HOLD state */
  164. rebuild_sys_tab:
  165. if (hba_chain == NULL) 
  166. return 0;
  167. /*
  168.  * If build_sys_table fails, we kill everything and bail
  169.  * as we can't init the IOPs w/o a system table
  170.  */
  171. if (adpt_i2o_build_sys_table() < 0) {
  172. adpt_i2o_sys_shutdown();
  173. return 0;
  174. }
  175. PDEBUG("HBA's in HOLD staten");
  176. /* If IOP don't get online, we need to rebuild the System table */
  177. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  178. if (adpt_i2o_online_hba(pHba) < 0) {
  179. adpt_i2o_delete_hba(pHba);
  180. goto rebuild_sys_tab;
  181. }
  182. }
  183. /* Active IOPs now in OPERATIONAL state */
  184. PDEBUG("HBA's in OPERATIONAL staten");
  185. printk(KERN_INFO"dpti: If you have a lot of devices this could take a few minutes.n");
  186. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  187. printk(KERN_INFO"%s: Reading the hardware resource table.n", pHba->name);
  188. if (adpt_i2o_lct_get(pHba) < 0){
  189. adpt_i2o_delete_hba(pHba);
  190. continue;
  191. }
  192. if (adpt_i2o_parse_lct(pHba) < 0){
  193. adpt_i2o_delete_hba(pHba);
  194. continue;
  195. }
  196. adpt_inquiry(pHba);
  197. }
  198. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  199. if( adpt_scsi_register(pHba,sht) < 0){
  200. adpt_i2o_delete_hba(pHba);
  201. continue;
  202. }
  203. pHba->initialized = TRUE;
  204. pHba->state &= ~DPTI_STATE_RESET;
  205. }
  206. // Register our control device node
  207. // nodes will need to be created in /dev to access this
  208. // the nodes can not be created from within the driver
  209. if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
  210. adpt_i2o_sys_shutdown();
  211. return 0;
  212. }
  213. return hba_count;
  214. }
  215. /*
  216.  * scsi_unregister will be called AFTER we return. 
  217.  */
  218. static int adpt_release(struct Scsi_Host *host)
  219. {
  220. adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
  221. // adpt_i2o_quiesce_hba(pHba);
  222. adpt_i2o_delete_hba(pHba);
  223. return 0;
  224. }
  225. static void adpt_inquiry(adpt_hba* pHba)
  226. {
  227. u32 msg[14]; 
  228. u32 *mptr;
  229. u32 *lenptr;
  230. int direction;
  231. int scsidir;
  232. u32 len;
  233. u32 reqlen;
  234. u8* buf;
  235. u8  scb[16];
  236. s32 rcode;
  237. memset(msg, 0, sizeof(msg));
  238. buf = (u8*)kmalloc(80,GFP_KERNEL|ADDR32);
  239. if(!buf){
  240. printk(KERN_ERR"%s: Could not allocate buffern",pHba->name);
  241. return;
  242. }
  243. memset((void*)buf, 0, 36);
  244. len = 36;
  245. direction = 0x00000000;
  246. scsidir  =0x40000000; // DATA IN  (iop<--dev)
  247. reqlen = 14; // SINGLE SGE
  248. /* Stick the headers on */
  249. msg[0] = reqlen<<16 | SGL_OFFSET_12;
  250. msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
  251. msg[2] = 0;
  252. msg[3]  = 0;
  253. // Adaptec/DPT Private stuff 
  254. msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
  255. msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
  256. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  257. // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
  258. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
  259. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  260. msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
  261. mptr=msg+7;
  262. memset(scb, 0, sizeof(scb));
  263. // Write SCSI command into the message - always 16 byte block 
  264. scb[0] = INQUIRY;
  265. scb[1] = 0;
  266. scb[2] = 0;
  267. scb[3] = 0;
  268. scb[4] = 36;
  269. scb[5] = 0;
  270. // Don't care about the rest of scb
  271. memcpy(mptr, scb, sizeof(scb));
  272. mptr+=4;
  273. lenptr=mptr++; /* Remember me - fill in when we know */
  274. /* Now fill in the SGList and command */
  275. *lenptr = len;
  276. *mptr++ = 0xD0000000|direction|len;
  277. *mptr++ = virt_to_bus(buf);
  278. // Send it on it's way
  279. rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
  280. if (rcode != 0) {
  281. sprintf(pHba->detail, "Adaptec I2O RAID");
  282. printk(KERN_INFO "%s: Inquiry Error (%d)n",pHba->name,rcode);
  283. } else {
  284. memset(pHba->detail, 0, sizeof(pHba->detail));
  285. memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
  286. memcpy(&(pHba->detail[16]), " Model: ", 8);
  287. memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
  288. memcpy(&(pHba->detail[40]), " FW: ", 4);
  289. memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
  290. pHba->detail[48] = ''; /* precautionary */
  291. }
  292. kfree(buf);
  293. adpt_i2o_status_get(pHba);
  294. return ;
  295. }
  296. static void adpt_select_queue_depths(struct Scsi_Host *host, Scsi_Device * devicelist)
  297. {
  298. Scsi_Device *device; /* scsi layer per device information */
  299. adpt_hba* pHba;
  300. pHba = (adpt_hba *) host->hostdata[0];
  301. for (device = devicelist; device != NULL; device = device->next) {
  302. if (device->host != host) {
  303. continue;
  304. }
  305. if (host->can_queue) {
  306. device->queue_depth =  host->can_queue - 1;
  307. } else {
  308. device->queue_depth = 1;
  309. }
  310. }
  311. }
  312. static int adpt_queue(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  313. {
  314. adpt_hba* pHba = NULL;
  315. struct adpt_device* pDev = NULL; /* dpt per device information */
  316. ulong timeout = jiffies + (TMOUT_SCSI*HZ);
  317. cmd->scsi_done = done;
  318. /*
  319.  * SCSI REQUEST_SENSE commands will be executed automatically by the 
  320.  * Host Adapter for any errors, so they should not be executed 
  321.  * explicitly unless the Sense Data is zero indicating that no error 
  322.  * occurred.
  323.  */
  324. if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
  325. cmd->result = (DID_OK << 16);
  326. cmd->scsi_done(cmd);
  327. return 0;
  328. }
  329. pHba = (adpt_hba*)cmd->host->hostdata[0];
  330. if (!pHba) {
  331. return FAILED;
  332. }
  333. rmb();
  334. /*
  335.  * TODO: I need to block here if I am processing ioctl cmds
  336.  * but if the outstanding cmds all finish before the ioctl,
  337.  * the scsi-core will not know to start sending cmds to me again.
  338.  * I need to a way to restart the scsi-cores queues or should I block
  339.  * calling scsi_done on the outstanding cmds instead
  340.  * for now we don't set the IOCTL state
  341.  */
  342. if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
  343. pHba->host->last_reset = jiffies;
  344. pHba->host->resetting = 1;
  345. return 1;
  346. }
  347. if(cmd->eh_state != SCSI_STATE_QUEUED){
  348. // If we are not doing error recovery
  349. mod_timer(&cmd->eh_timeout, timeout);
  350. }
  351. // TODO if the cmd->device if offline then I may need to issue a bus rescan
  352. // followed by a get_lct to see if the device is there anymore
  353. if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
  354. /*
  355.  * First command request for this device.  Set up a pointer
  356.  * to the device structure.  This should be a TEST_UNIT_READY
  357.  * command from scan_scsis_single.
  358.  */
  359. if ((pDev = adpt_find_device(pHba, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun)) == NULL) {
  360. // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response 
  361. // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
  362. cmd->result = (DID_NO_CONNECT << 16);
  363. cmd->scsi_done(cmd);
  364. return 0;
  365. }
  366. (struct adpt_device*)(cmd->device->hostdata) = pDev;
  367. }
  368. pDev->pScsi_dev = cmd->device;
  369. /*
  370.  * If we are being called from when the device is being reset, 
  371.  * delay processing of the command until later.
  372.  */
  373. if (pDev->state & DPTI_DEV_RESET ) {
  374. return FAILED;
  375. }
  376. return adpt_scsi_to_i2o(pHba, cmd, pDev);
  377. }
  378. static int adpt_bios_param(Disk* disk, kdev_t dev, int geom[])
  379. {
  380. int heads=-1;
  381. int sectors=-1;
  382. int cylinders=-1;
  383. // *** First lets set the default geometry ****
  384. // If the capacity is less than ox2000
  385. if (disk->capacity < 0x2000 ) { // floppy
  386. heads = 18;
  387. sectors = 2;
  388. // else if between 0x2000 and 0x20000
  389. else if (disk->capacity < 0x20000) {
  390. heads = 64;
  391. sectors = 32;
  392. }
  393. // else if between 0x20000 and 0x40000
  394. else if (disk->capacity < 0x40000) {
  395. heads = 65;
  396. sectors = 63;
  397. }
  398. // else if between 0x4000 and 0x80000
  399. else if (disk->capacity < 0x80000) {
  400. heads = 128;
  401. sectors = 63;
  402. }
  403. // else if greater than 0x80000
  404. else {
  405. heads = 255;
  406. sectors = 63;
  407. }
  408. cylinders = disk->capacity / (heads * sectors);
  409. // Special case if CDROM
  410. if(disk->device->type == 5) {  // CDROM
  411. heads = 252;
  412. sectors = 63;
  413. cylinders = 1111;
  414. }
  415. geom[0] = heads;
  416. geom[1] = sectors;
  417. geom[2] = cylinders;
  418. PDEBUG("adpt_bios_param: exitn");
  419. return 0;
  420. }
  421. static const char *adpt_info(struct Scsi_Host *host)
  422. {
  423. adpt_hba* pHba;
  424. pHba = (adpt_hba *) host->hostdata[0];
  425. return (char *) (pHba->detail);
  426. }
  427. static int adpt_proc_info(char *buffer, char **start, off_t offset,
  428.   int length, int hostno, int inout)
  429. {
  430. struct adpt_device* d;
  431. int id;
  432. int chan;
  433. int len = 0;
  434. int begin = 0;
  435. int pos = 0;
  436. adpt_hba* pHba;
  437. struct Scsi_Host *host;
  438. int unit;
  439. *start = buffer;
  440. if (inout == TRUE) {
  441. /*
  442.  * The user has done a write and wants us to take the
  443.  * data in the buffer and do something with it.
  444.  * proc_scsiwrite calls us with inout = 1
  445.  *
  446.  * Read data from buffer (writing to us) - NOT SUPPORTED
  447.  */
  448. return -EINVAL;
  449. }
  450. /*
  451.  * inout = 0 means the user has done a read and wants information
  452.  * returned, so we write information about the cards into the buffer
  453.  * proc_scsiread() calls us with inout = 0
  454.  */
  455. // Find HBA (host bus adapter) we are looking for
  456. down(&adpt_configuration_lock);
  457. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  458. if (pHba->host->host_no == hostno) {
  459. break; /* found adapter */
  460. }
  461. }
  462. up(&adpt_configuration_lock);
  463. if (pHba == NULL) {
  464. return 0;
  465. }
  466. host = pHba->host;
  467. len  = sprintf(buffer    , "Adaptec I2O RAID Driver Version: %snn", DPT_I2O_VERSION);
  468. len += sprintf(buffer+len, "%sn", pHba->detail);
  469. len += sprintf(buffer+len, "SCSI Host=scsi%d  Control Node=/dev/%s  irq=%dn", 
  470. pHba->host->host_no, pHba->name, host->irq);
  471. len += sprintf(buffer+len, "tpost fifo size  = %dntreply fifo size = %dntsg table size   = %dnn",
  472. host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
  473. pos = begin + len;
  474. /* CHECKPOINT */
  475. if(pos > offset + length) {
  476. goto stop_output;
  477. }
  478. if(pos <= offset) {
  479. /*
  480.  * If we haven't even written to where we last left
  481.  * off (the last time we were called), reset the 
  482.  * beginning pointer.
  483.  */
  484. len = 0;
  485. begin = pos;
  486. }
  487. len +=  sprintf(buffer+len, "Devices:n");
  488. for(chan = 0; chan < MAX_CHANNEL; chan++) {
  489. for(id = 0; id < MAX_ID; id++) {
  490. d = pHba->channel[chan].device[id];
  491. while(d){
  492. len += sprintf(buffer+len,"t%-24.24s", d->pScsi_dev->vendor);
  493. len += sprintf(buffer+len," Rev: %-8.8sn", d->pScsi_dev->rev);
  494. pos = begin + len;
  495. /* CHECKPOINT */
  496. if(pos > offset + length) {
  497. goto stop_output;
  498. }
  499. if(pos <= offset) {
  500. len = 0;
  501. begin = pos;
  502. }
  503. unit = d->pI2o_dev->lct_data.tid;
  504. len += sprintf(buffer+len, "tTID=%d, (Channel=%d, Target=%d, Lun=%d)  (%s)nn",
  505.        unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
  506.        d->pScsi_dev->online? "online":"offline"); 
  507. pos = begin + len;
  508. /* CHECKPOINT */
  509. if(pos > offset + length) {
  510. goto stop_output;
  511. }
  512. if(pos <= offset) {
  513. len = 0;
  514. begin = pos;
  515. }
  516. d = d->next_lun;
  517. }
  518. }
  519. }
  520. /*
  521.  * begin is where we last checked our position with regards to offset
  522.  * begin is always less than offset.  len is relative to begin.  It
  523.  * is the number of bytes written past begin
  524.  *
  525.  */
  526. stop_output:
  527. /* stop the output and calculate the correct length */
  528. *(buffer + len) = '';
  529. *start = buffer + (offset - begin); /* Start of wanted data */
  530. len -= (offset - begin);
  531. if(len > length) {
  532. len = length;
  533. } else if(len < 0){
  534. len = 0;
  535. **start = '';
  536. }
  537. return len;
  538. }
  539. /*===========================================================================
  540.  * Error Handling routines
  541.  *===========================================================================
  542.  */
  543. static int adpt_abort(Scsi_Cmnd * cmd)
  544. {
  545. adpt_hba* pHba = NULL; /* host bus adapter structure */
  546. struct adpt_device* dptdevice; /* dpt per device information */
  547. u32 msg[5];
  548. int rcode;
  549. if(cmd->serial_number == 0){
  550. return FAILED;
  551. }
  552. pHba = (adpt_hba*) cmd->host->hostdata[0];
  553. printk(KERN_INFO"%s: Trying to Abort cmd=%ldn",pHba->name, cmd->serial_number);
  554. if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
  555. printk(KERN_ERR "%s: Unable to abort: No device in cmndn",pHba->name);
  556. return FAILED;
  557. }
  558. memset(msg, 0, sizeof(msg));
  559. msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
  560. msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
  561. msg[2] = 0;
  562. msg[3]= 0; 
  563. msg[4] = (u32)cmd;
  564. if( (rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER)) != 0){
  565. if(rcode == -EOPNOTSUPP ){
  566. printk(KERN_INFO"%s: Abort cmd not supportedn",pHba->name);
  567. return FAILED;
  568. }
  569. printk(KERN_INFO"%s: Abort cmd=%ld failed.n",pHba->name, cmd->serial_number);
  570. return FAILED;
  571. printk(KERN_INFO"%s: Abort cmd=%ld complete.n",pHba->name, cmd->serial_number);
  572. return SUCCESS;
  573. }
  574. #define I2O_DEVICE_RESET 0x27
  575. // This is the same for BLK and SCSI devices
  576. // NOTE this is wrong in the i2o.h definitions
  577. // This is not currently supported by our adapter but we issue it anyway
  578. static int adpt_device_reset(Scsi_Cmnd* cmd)
  579. {
  580. adpt_hba* pHba;
  581. u32 msg[4];
  582. u32 rcode;
  583. int old_state;
  584. struct adpt_device* d = (void*) cmd->device->hostdata;
  585. pHba = (void*) cmd->host->hostdata[0];
  586. printk(KERN_INFO"%s: Trying to reset devicen",pHba->name);
  587. if (!d) {
  588. printk(KERN_INFO"%s: Reset Device: Device Not foundn",pHba->name);
  589. return FAILED;
  590. }
  591. memset(msg, 0, sizeof(msg));
  592. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  593. msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
  594. msg[2] = 0;
  595. msg[3] = 0;
  596. old_state = d->state;
  597. d->state |= DPTI_DEV_RESET;
  598. if( (rcode = adpt_i2o_post_wait(pHba, (void*)msg,sizeof(msg), FOREVER)) ){
  599. d->state = old_state;
  600. if(rcode == -EOPNOTSUPP ){
  601. printk(KERN_INFO"%s: Device reset not supportedn",pHba->name);
  602. return FAILED;
  603. }
  604. printk(KERN_INFO"%s: Device reset failedn",pHba->name);
  605. return FAILED;
  606. } else {
  607. d->state = old_state;
  608. printk(KERN_INFO"%s: Device reset successfuln",pHba->name);
  609. return SUCCESS;
  610. }
  611. }
  612. #define I2O_HBA_BUS_RESET 0x87
  613. // This version of bus reset is called by the eh_error handler
  614. static int adpt_bus_reset(Scsi_Cmnd* cmd)
  615. {
  616. adpt_hba* pHba;
  617. u32 msg[4];
  618. pHba = (adpt_hba*)cmd->host->hostdata[0];
  619. memset(msg, 0, sizeof(msg));
  620. printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %dn",pHba->name, cmd->channel,pHba->channel[cmd->channel].tid );
  621. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  622. msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->channel].tid);
  623. msg[2] = 0;
  624. msg[3] = 0;
  625. if(adpt_i2o_post_wait(pHba, (void*)msg,sizeof(msg), FOREVER) ){
  626. printk(KERN_WARNING"%s: Bus reset failed.n",pHba->name);
  627. return FAILED;
  628. } else {
  629. printk(KERN_WARNING"%s: Bus reset success.n",pHba->name);
  630. return SUCCESS;
  631. }
  632. }
  633. // This version of reset is called by the eh_error_handler
  634. static int adpt_reset(Scsi_Cmnd* cmd)
  635. {
  636. adpt_hba* pHba;
  637. int rcode;
  638. pHba = (adpt_hba*)cmd->host->hostdata[0];
  639. printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %dn",pHba->name,cmd->channel,pHba->channel[cmd->channel].tid );
  640. rcode =  adpt_hba_reset(pHba);
  641. if(rcode == 0){
  642. printk(KERN_WARNING"%s: HBA reset completen",pHba->name);
  643. return SUCCESS;
  644. } else {
  645. printk(KERN_WARNING"%s: HBA reset failed (%x)n",pHba->name, rcode);
  646. return FAILED;
  647. }
  648. }
  649. // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
  650. static int adpt_hba_reset(adpt_hba* pHba)
  651. {
  652. int rcode;
  653. pHba->state |= DPTI_STATE_RESET;
  654. // Activate does get status , init outbound, and get hrt
  655. if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
  656. printk(KERN_ERR "%s: Could not activaten", pHba->name);
  657. adpt_i2o_delete_hba(pHba);
  658. return rcode;
  659. }
  660. if ((rcode=adpt_i2o_build_sys_table()) < 0) {
  661. adpt_i2o_delete_hba(pHba);
  662. return rcode;
  663. }
  664. PDEBUG("%s: in HOLD staten",pHba->name);
  665. if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
  666. adpt_i2o_delete_hba(pHba);
  667. return rcode;
  668. }
  669. PDEBUG("%s: in OPERATIONAL staten",pHba->name);
  670. if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
  671. adpt_i2o_delete_hba(pHba);
  672. return rcode;
  673. }
  674. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
  675. adpt_i2o_delete_hba(pHba);
  676. return rcode;
  677. }
  678. pHba->state &= ~DPTI_STATE_RESET;
  679. adpt_fail_posted_scbs(pHba);
  680. return 0; /* return success */
  681. }
  682. /*===========================================================================
  683.  * 
  684.  *===========================================================================
  685.  */
  686. static void adpt_i2o_sys_shutdown(void)
  687. {
  688. adpt_hba *pHba, *pNext;
  689. struct adpt_i2o_post_wait_data *p1, *p2;
  690.  printk(KERN_INFO"Shutting down Adaptec I2O controllers.n");
  691.  printk(KERN_INFO"   This could take a few minutes if there are many devices attachedn");
  692. /* Delete all IOPs from the controller chain */
  693. /* They should have already been released by the
  694.  * scsi-core
  695.  */
  696. for (pHba = hba_chain; pHba; pHba = pNext) {
  697. pNext = pHba->next;
  698. adpt_i2o_delete_hba(pHba);
  699. }
  700. /* Remove any timedout entries from the wait queue.  */
  701. p2 = NULL;
  702. // spin_lock_irqsave(&adpt_post_wait_lock, flags);
  703. /* Nothing should be outstanding at this point so just
  704.  * free them 
  705.  */
  706. for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p2->next) {
  707. kfree(p1);
  708. }
  709. // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  710. adpt_post_wait_queue = 0;
  711.  printk(KERN_INFO "Adaptec I2O controllers down.n");
  712. }
  713. /*
  714.  * reboot/shutdown notification.
  715.  *
  716.  * - Quiesce each IOP in the system
  717.  *
  718.  */
  719. #ifdef REBOOT_NOTIFIER
  720. static int adpt_reboot_event(struct notifier_block *n, ulong code, void *p)
  721. {
  722.  if(code != SYS_RESTART && code != SYS_HALT && code != SYS_POWER_OFF)
  723.   return NOTIFY_DONE;
  724.  adpt_i2o_sys_shutdown();
  725.  return NOTIFY_DONE;
  726. }
  727. #endif
  728. static int adpt_install_hba(Scsi_Host_Template* sht, struct pci_dev* pDev) 
  729. {
  730. adpt_hba* pHba = NULL;
  731. adpt_hba* p = NULL;
  732. ulong base_addr0_phys = 0;
  733. ulong base_addr1_phys = 0;
  734. u32 hba_map0_area_size = 0;
  735. u32 hba_map1_area_size = 0;
  736. ulong base_addr_virt = 0;
  737. ulong msg_addr_virt = 0;
  738. int raptorFlag = FALSE;
  739. int i;
  740. if(pci_enable_device(pDev)) {
  741. return -EINVAL;
  742. }
  743. pci_set_master(pDev);
  744. base_addr0_phys = pci_resource_start(pDev,0);
  745. hba_map0_area_size = pci_resource_len(pDev,0);
  746. // Check if standard PCI card or single BAR Raptor
  747. if(pDev->device == PCI_DPT_DEVICE_ID){
  748. if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
  749. // Raptor card with this device id needs 4M
  750. hba_map0_area_size = 0x400000;
  751. } else { // Not Raptor - it is a PCI card
  752. if(hba_map0_area_size > 0x100000 ){ 
  753. hba_map0_area_size = 0x100000;
  754. }
  755. }
  756. } else {// Raptor split BAR config
  757. // Use BAR1 in this configuration
  758. base_addr1_phys = pci_resource_start(pDev,1);
  759. hba_map1_area_size = pci_resource_len(pDev,1);
  760. raptorFlag = TRUE;
  761. }
  762. base_addr_virt = (ulong)ioremap(base_addr0_phys,hba_map0_area_size);
  763. if(base_addr_virt == 0) {
  764. PERROR("dpti: adpt_config_hba: io remap failedn");
  765. return -EINVAL;
  766. }
  767.         if(raptorFlag == TRUE) {
  768. msg_addr_virt = (ulong)ioremap(base_addr1_phys, hba_map1_area_size );
  769. if(msg_addr_virt == 0) {
  770. PERROR("dpti: adpt_config_hba: io remap failed on BAR1n");
  771. iounmap((void*)base_addr_virt);
  772. return -EINVAL;
  773. }
  774. } else {
  775. msg_addr_virt = base_addr_virt;
  776. }
  777. // Allocate and zero the data structure
  778. pHba = kmalloc(sizeof(adpt_hba), GFP_KERNEL);
  779. if( pHba == NULL) {
  780. if(msg_addr_virt != base_addr_virt){
  781. iounmap((void*)msg_addr_virt);
  782. }
  783. iounmap((void*)base_addr_virt);
  784. return -ENOMEM;
  785. }
  786. memset(pHba, 0, sizeof(adpt_hba));
  787. down(&adpt_configuration_lock);
  788. for(i=0;i<DPTI_MAX_HBA;i++) {
  789. if(hbas[i]==NULL) {
  790. hbas[i]=pHba;
  791. break;
  792. }
  793. }
  794. if(hba_chain != NULL){
  795. for(p = hba_chain; p->next; p = p->next);
  796. p->next = pHba;
  797. } else {
  798. hba_chain = pHba;
  799. }
  800. pHba->next = NULL;
  801. pHba->unit = hba_count;
  802. sprintf(pHba->name, "dpti%d", i);
  803. hba_count++;
  804. up(&adpt_configuration_lock);
  805. pHba->pDev = pDev;
  806. pHba->base_addr_phys = base_addr0_phys;
  807. // Set up the Virtual Base Address of the I2O Device
  808. pHba->base_addr_virt = base_addr_virt;
  809. pHba->msg_addr_virt = msg_addr_virt;  
  810. pHba->irq_mask = (ulong)(base_addr_virt+0x30);
  811. pHba->post_port = (ulong)(base_addr_virt+0x40);
  812. pHba->reply_port = (ulong)(base_addr_virt+0x44);
  813. pHba->hrt = NULL;
  814. pHba->lct = NULL;
  815. pHba->lct_size = 0;
  816. pHba->status_block = NULL;
  817. pHba->post_count = 0;
  818. pHba->state = DPTI_STATE_RESET;
  819. pHba->pDev = pDev;
  820. pHba->devices = NULL;
  821. // Initializing the spinlocks
  822. spin_lock_init(&pHba->state_lock);
  823. if(raptorFlag == 0){
  824. printk(KERN_INFO"Adaptec I2O RAID controller %d at %lx size=%x irq=%dn", 
  825. hba_count-1, base_addr_virt, hba_map0_area_size, pDev->irq);
  826. } else {
  827. printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%dn",hba_count-1, pDev->irq);
  828. printk(KERN_INFO"     BAR0 %lx - size= %xn",base_addr_virt,hba_map0_area_size);
  829. printk(KERN_INFO"     BAR1 %lx - size= %xn",msg_addr_virt,hba_map1_area_size);
  830. }
  831. if (request_irq (pDev->irq, adpt_isr, SA_SHIRQ, pHba->name, pHba)) {
  832. printk(KERN_ERR"%s: Couldn't register IRQ %dn", pHba->name, pDev->irq);
  833. adpt_i2o_delete_hba(pHba);
  834. return -EINVAL;
  835. }
  836. return 0;
  837. }
  838. static void adpt_i2o_delete_hba(adpt_hba* pHba)
  839. {
  840. adpt_hba* p1;
  841. adpt_hba* p2;
  842. struct i2o_device* d;
  843. struct i2o_device* next;
  844. int i;
  845. int j;
  846. struct adpt_device* pDev;
  847. struct adpt_device* pNext;
  848. down(&adpt_configuration_lock);
  849. // scsi_unregister calls our adpt_release which
  850. // does a quiese
  851. if(pHba->host){
  852. free_irq(pHba->host->irq, pHba);
  853. }
  854. for(i=0;i<DPTI_MAX_HBA;i++) {
  855. if(hbas[i]==pHba) {
  856. hbas[i] = NULL;
  857. }
  858. }
  859. p2 = NULL;
  860. for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
  861. if(p1 == pHba) {
  862. if(p2) {
  863. p2->next = p1->next;
  864. } else {
  865. hba_chain = p1->next;
  866. }
  867. break;
  868. }
  869. }
  870. hba_count--;
  871. up(&adpt_configuration_lock);
  872. iounmap((void*)pHba->base_addr_virt);
  873. if(pHba->msg_addr_virt != pHba->base_addr_virt){
  874. iounmap((void*)pHba->msg_addr_virt);
  875. }
  876. if(pHba->hrt) {
  877. kfree(pHba->hrt);
  878. }
  879. if(pHba->lct){
  880. kfree(pHba->lct);
  881. }
  882. if(pHba->status_block) {
  883. kfree(pHba->status_block);
  884. }
  885. if(pHba->reply_pool){
  886. kfree(pHba->reply_pool);
  887. }
  888. for(d = pHba->devices; d ; d = next){
  889. next = d->next;
  890. kfree(d);
  891. }
  892. for(i = 0 ; i < pHba->top_scsi_channel ; i++){
  893. for(j = 0; j < MAX_ID; j++){
  894. if(pHba->channel[i].device[j] != NULL){
  895. for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
  896. pNext = pDev->next_lun;
  897. kfree(pDev);
  898. }
  899. }
  900. }
  901. }
  902. kfree(pHba);
  903. if(hba_count <= 0){
  904. unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);   
  905. }
  906. }
  907. static int adpt_init(void)
  908. {
  909. int i;
  910. printk(KERN_INFO"Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "n");
  911. for (i = 0; i < DPTI_MAX_HBA; i++) {
  912. hbas[i] = NULL;
  913. }
  914. #ifdef REBOOT_NOTIFIER
  915. register_reboot_notifier(&adpt_reboot_notifier);
  916. #endif
  917. return 0;
  918. }
  919. static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
  920. {
  921. struct adpt_device* d;
  922. if(chan < 0 || chan >= MAX_CHANNEL)
  923. return NULL;
  924. if( pHba->channel[chan].device == NULL){
  925. printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocatedn");
  926. return NULL;
  927. }
  928. d = pHba->channel[chan].device[id];
  929. if(!d || d->tid == 0) {
  930. return NULL;
  931. }
  932. /* If it is the only lun at that address then this should match*/
  933. if(d->scsi_lun == lun){
  934. return d;
  935. }
  936. /* else we need to look through all the luns */
  937. for(d=d->next_lun ; d ; d = d->next_lun){
  938. if(d->scsi_lun == lun){
  939. return d;
  940. }
  941. }
  942. return NULL;
  943. }
  944. static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
  945. {
  946. // I used my own version of the WAIT_QUEUE_HEAD
  947. // to handle some version differences
  948. // When embedded in the kernel this could go back to the vanilla one
  949. ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
  950. int status = 0;
  951. ulong flags = 0;
  952. struct adpt_i2o_post_wait_data *p1, *p2;
  953. struct adpt_i2o_post_wait_data *wait_data =
  954. kmalloc(sizeof(struct adpt_i2o_post_wait_data),GFP_KERNEL);
  955. adpt_wait_queue_t wait;
  956. if(!wait_data){
  957. return -ENOMEM;
  958. }
  959. /*
  960.  * The spin locking is needed to keep anyone from playing
  961.  * with the queue pointers and id while we do the same
  962.  */
  963. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  964.        // TODO we need a MORE unique way of getting ids
  965.        // to support async LCT get
  966. wait_data->next = adpt_post_wait_queue;
  967. adpt_post_wait_queue = wait_data;
  968. adpt_post_wait_id++;
  969. adpt_post_wait_id = (adpt_post_wait_id & 0x7fff);
  970. wait_data->id =  adpt_post_wait_id;
  971. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  972. wait_data->wq = &adpt_wq_i2o_post;
  973. wait_data->status = -ETIMEDOUT;
  974. // this code is taken from kernel/sched.c:interruptible_sleep_on_timeout
  975. wait.task = current;
  976. init_waitqueue_entry(&wait, current);
  977. wq_write_lock_irqsave(&adpt_wq_i2o_post.lock,flags);
  978. __add_wait_queue(&adpt_wq_i2o_post, &wait);
  979. wq_write_unlock(&adpt_wq_i2o_post.lock);
  980. msg[2] |= 0x80000000 | ((u32)wait_data->id);
  981. timeout *= HZ;
  982. if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
  983. if(!timeout){
  984. set_current_state(TASK_INTERRUPTIBLE);
  985. spin_unlock_irq(&io_request_lock);
  986. schedule();
  987. spin_lock_irq(&io_request_lock);
  988. } else {
  989. set_current_state(TASK_INTERRUPTIBLE);
  990. spin_unlock_irq(&io_request_lock);
  991. schedule_timeout(timeout*HZ);
  992. spin_lock_irq(&io_request_lock);
  993. }
  994. }
  995. wq_write_lock_irq(&adpt_wq_i2o_post.lock);
  996. __remove_wait_queue(&adpt_wq_i2o_post, &wait);
  997. wq_write_unlock_irqrestore(&adpt_wq_i2o_post.lock,flags);
  998. if(status == -ETIMEDOUT){
  999. printk(KERN_INFO"dpti%d: POST WAIT TIMEOUTn",pHba->unit);
  1000. // We will have to free the wait_data memory during shutdown
  1001. return status;
  1002. }
  1003. /* Remove the entry from the queue.  */
  1004. p2 = NULL;
  1005. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  1006. for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
  1007. if(p1 == wait_data) {
  1008. if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
  1009. status = -EOPNOTSUPP;
  1010. }
  1011. if(p2) {
  1012. p2->next = p1->next;
  1013. } else {
  1014. adpt_post_wait_queue = p1->next;
  1015. }
  1016. break;
  1017. }
  1018. }
  1019. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  1020. kfree(wait_data);
  1021. return status;
  1022. }
  1023. static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
  1024. {
  1025. u32 m = EMPTY_QUEUE;
  1026. u32 *msg;
  1027. ulong timeout = jiffies + 30*HZ;
  1028. do {
  1029. rmb();
  1030. m = readl(pHba->post_port);
  1031. if (m != EMPTY_QUEUE) {
  1032. break;
  1033. }
  1034. if(time_after(jiffies,timeout)){
  1035. printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!n", pHba->unit);
  1036. return -ETIMEDOUT;
  1037. }
  1038. } while(m == EMPTY_QUEUE);
  1039. msg = (u32*) (pHba->msg_addr_virt + m);
  1040. memcpy_toio(msg, data, len);
  1041. wmb();
  1042. //post message
  1043. writel(m, pHba->post_port);
  1044. wmb();
  1045. return 0;
  1046. }
  1047. static void adpt_i2o_post_wait_complete(u32 context, int status)
  1048. {
  1049. struct adpt_i2o_post_wait_data *p1 = NULL;
  1050. /*
  1051.  * We need to search through the adpt_post_wait
  1052.  * queue to see if the given message is still
  1053.  * outstanding.  If not, it means that the IOP
  1054.  * took longer to respond to the message than we
  1055.  * had allowed and timer has already expired.
  1056.  * Not much we can do about that except log
  1057.  * it for debug purposes, increase timeout, and recompile
  1058.  *
  1059.  * Lock needed to keep anyone from moving queue pointers
  1060.  * around while we're looking through them.
  1061.  */
  1062. context &= 0x7fff;
  1063. spin_lock(&adpt_post_wait_lock);
  1064. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1065. if(p1->id == context) {
  1066. p1->status = status;
  1067. spin_unlock(&adpt_post_wait_lock);
  1068. wake_up_interruptible(p1->wq);
  1069. return;
  1070. }
  1071. }
  1072. spin_unlock(&adpt_post_wait_lock);
  1073.         // If this happens we loose commands that probably really completed
  1074. printk(KERN_DEBUG"dpti: Could Not find task %d in wait queuen",context);
  1075. printk(KERN_DEBUG"      Tasks in wait queue:n");
  1076. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1077. printk(KERN_DEBUG"           %dn",p1->id);
  1078. }
  1079. return;
  1080. }
  1081. static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
  1082. {
  1083. u32 msg[8];
  1084. u8* status;
  1085. u32 m = EMPTY_QUEUE ;
  1086. ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
  1087. if(pHba->initialized  == FALSE) { // First time reset should be quick
  1088. timeout = jiffies + (25*HZ);
  1089. } else {
  1090. adpt_i2o_quiesce_hba(pHba);
  1091. }
  1092. do {
  1093. rmb();
  1094. m = readl(pHba->post_port);
  1095. if (m != EMPTY_QUEUE) {
  1096. break;
  1097. }
  1098. if(time_after(jiffies,timeout)){
  1099. printk(KERN_WARNING"Timeout waiting for message!n");
  1100. return -ETIMEDOUT;
  1101. }
  1102. } while (m == EMPTY_QUEUE);
  1103. status = (u8*)kmalloc(4, GFP_KERNEL|ADDR32);
  1104. if(status == NULL) {
  1105. adpt_send_nop(pHba, m);
  1106. printk(KERN_ERR"IOP reset failed - no free memory.n");
  1107. return -ENOMEM;
  1108. }
  1109. memset(status,0,4);
  1110. msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
  1111. msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
  1112. msg[2]=0;
  1113. msg[3]=0;
  1114. msg[4]=0;
  1115. msg[5]=0;
  1116. msg[6]=virt_to_bus(status);
  1117. msg[7]=0;     
  1118. memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
  1119. wmb();
  1120. writel(m, pHba->post_port);
  1121. wmb();
  1122. while(*status == 0){
  1123. if(time_after(jiffies,timeout)){
  1124. printk(KERN_WARNING"%s: IOP Reset Timeoutn",pHba->name);
  1125. kfree(status);
  1126. return -ETIMEDOUT;
  1127. }
  1128. rmb();
  1129. }
  1130. if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
  1131. PDEBUG("%s: Reset in progress...n", pHba->name);
  1132. // Here we wait for message frame to become available
  1133. // indicated that reset has finished
  1134. do {
  1135. rmb();
  1136. m = readl(pHba->post_port);
  1137. if (m != EMPTY_QUEUE) {
  1138. break;
  1139. }
  1140. if(time_after(jiffies,timeout)){
  1141. printk(KERN_ERR "%s:Timeout waiting for IOP Reset.n",pHba->name);
  1142. return -ETIMEDOUT;
  1143. }
  1144. } while (m == EMPTY_QUEUE);
  1145. // Flush the offset
  1146. adpt_send_nop(pHba, m);
  1147. }
  1148. adpt_i2o_status_get(pHba);
  1149. if(*status == 0x02 ||
  1150. pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  1151. printk(KERN_WARNING"%s: Reset reject, trying to clearn",
  1152. pHba->name);
  1153. } else {
  1154. PDEBUG("%s: Reset completed.n", pHba->name);
  1155. }
  1156. kfree(status);
  1157. #ifdef UARTDELAY
  1158. // This delay is to allow someone attached to the card through the debug UART to 
  1159. // set up the dump levels that they want before the rest of the initialization sequence
  1160. adpt_delay(20000);
  1161. #endif
  1162. return 0;
  1163. }
  1164. static int adpt_i2o_parse_lct(adpt_hba* pHba)
  1165. {
  1166. int i;
  1167. int max;
  1168. int tid;
  1169. struct i2o_device *d;
  1170. i2o_lct *lct = pHba->lct;
  1171. u8 bus_no = 0;
  1172. s16 scsi_id;
  1173. s16 scsi_lun;
  1174. u32 buf[10]; // larger than 7, or 8 ...
  1175. struct adpt_device* pDev; 
  1176. if (lct == NULL) {
  1177. printk(KERN_ERR "%s: LCT is empty???n",pHba->name);
  1178. return -1;
  1179. }
  1180. max = lct->table_size;
  1181. max -= 3;
  1182. max /= 9;
  1183. for(i=0;i<max;i++) {
  1184. if( lct->lct_entry[i].user_tid != 0xfff){
  1185. /*
  1186.  * If we have hidden devices, we need to inform the upper layers about
  1187.  * the possible maximum id reference to handle device access when
  1188.  * an array is disassembled. This code has no other purpose but to
  1189.  * allow us future access to devices that are currently hidden
  1190.  * behind arrays, hotspares or have not been configured (JBOD mode).
  1191.  */
  1192. if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
  1193.     lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
  1194.     lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1195.      continue;
  1196. }
  1197. tid = lct->lct_entry[i].tid;
  1198. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1199. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  1200. continue;
  1201. }
  1202. bus_no = buf[0]>>16;
  1203. scsi_id = buf[1];
  1204. scsi_lun = (buf[2]>>8 )&0xff;
  1205. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1206. printk(KERN_WARNING"%s: Channel number %d out of range n", pHba->name, bus_no);
  1207. continue;
  1208. }
  1209. if(scsi_id > MAX_ID){
  1210. printk(KERN_WARNING"%s: SCSI ID %d out of range n", pHba->name, bus_no);
  1211. continue;
  1212. }
  1213. if(bus_no > pHba->top_scsi_channel){
  1214. pHba->top_scsi_channel = bus_no;
  1215. }
  1216. if(scsi_id > pHba->top_scsi_id){
  1217. pHba->top_scsi_id = scsi_id;
  1218. }
  1219. if(scsi_lun > pHba->top_scsi_lun){
  1220. pHba->top_scsi_lun = scsi_lun;
  1221. }
  1222. continue;
  1223. }
  1224. d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
  1225. if(d==NULL)
  1226. {
  1227. printk(KERN_CRIT"%s: Out of memory for I2O device data.n",pHba->name);
  1228. return -ENOMEM;
  1229. }
  1230. d->controller = (void*)pHba;
  1231. d->next = NULL;
  1232. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  1233. d->flags = 0;
  1234. tid = d->lct_data.tid;
  1235. adpt_i2o_report_hba_unit(pHba, d);
  1236. adpt_i2o_install_device(pHba, d);
  1237. }
  1238. bus_no = 0;
  1239. for(d = pHba->devices; d ; d = d->next) {
  1240. if(d->lct_data.class_id  == I2O_CLASS_BUS_ADAPTER_PORT ||
  1241.    d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PORT){
  1242. tid = d->lct_data.tid;
  1243. // TODO get the bus_no from hrt-but for now they are in order
  1244. //bus_no = 
  1245. if(bus_no > pHba->top_scsi_channel){
  1246. pHba->top_scsi_channel = bus_no;
  1247. }
  1248. pHba->channel[bus_no].type = d->lct_data.class_id;
  1249. pHba->channel[bus_no].tid = tid;
  1250. if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
  1251. {
  1252. pHba->channel[bus_no].scsi_id = buf[1];
  1253. PDEBUG("Bus %d - SCSI ID %d.n", bus_no, buf[1]);
  1254. }
  1255. // TODO remove - this is just until we get from hrt
  1256. bus_no++;
  1257. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1258. printk(KERN_WARNING"%s: Channel number %d out of range - LCTn", pHba->name, bus_no);
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. // Setup adpt_device table
  1264. for(d = pHba->devices; d ; d = d->next) {
  1265. if(d->lct_data.class_id  == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  1266.    d->lct_data.class_id  == I2O_CLASS_SCSI_PERIPHERAL ||
  1267.    d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1268. tid = d->lct_data.tid;
  1269. scsi_id = -1;
  1270. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1271. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
  1272. bus_no = buf[0]>>16;
  1273. scsi_id = buf[1];
  1274. scsi_lun = (buf[2]>>8 )&0xff;
  1275. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1276. continue;
  1277. }
  1278. if(scsi_id > MAX_ID){
  1279. continue;
  1280. }
  1281. if( pHba->channel[bus_no].device[scsi_id] == NULL){
  1282. pDev =  kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1283. if(pDev == NULL) {
  1284. return -ENOMEM;
  1285. }
  1286. pHba->channel[bus_no].device[scsi_id] = pDev;
  1287. memset(pDev,0,sizeof(struct adpt_device));
  1288. } else {
  1289. for( pDev = pHba->channel[bus_no].device[scsi_id];
  1290. pDev->next_lun; pDev = pDev->next_lun){
  1291. }
  1292. pDev->next_lun = kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1293. if(pDev == NULL) {
  1294. return -ENOMEM;
  1295. }
  1296. memset(pDev->next_lun,0,sizeof(struct adpt_device));
  1297. pDev = pDev->next_lun;
  1298. }
  1299. pDev->tid = tid;
  1300. pDev->scsi_channel = bus_no;
  1301. pDev->scsi_id = scsi_id;
  1302. pDev->scsi_lun = scsi_lun;
  1303. pDev->pI2o_dev = d;
  1304. d->owner = pDev;
  1305. pDev->type = (buf[0])&0xff;
  1306. pDev->flags = (buf[0]>>8)&0xff;
  1307. if(scsi_id > pHba->top_scsi_id){
  1308. pHba->top_scsi_id = scsi_id;
  1309. }
  1310. if(scsi_lun > pHba->top_scsi_lun){
  1311. pHba->top_scsi_lun = scsi_lun;
  1312. }
  1313. }
  1314. if(scsi_id == -1){
  1315. printk(KERN_WARNING"Could not find SCSI ID for %sn",
  1316. d->lct_data.identity_tag);
  1317. }
  1318. }
  1319. }
  1320. return 0;
  1321. }
  1322. /*
  1323.  * Each I2O controller has a chain of devices on it - these match
  1324.  * the useful parts of the LCT of the board.
  1325.  */
  1326.  
  1327. static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
  1328. {
  1329. down(&adpt_configuration_lock);
  1330. d->controller=pHba;
  1331. d->owner=NULL;
  1332. d->next=pHba->devices;
  1333. d->prev=NULL;
  1334. if (pHba->devices != NULL){
  1335. pHba->devices->prev=d;
  1336. }
  1337. pHba->devices=d;
  1338. *d->dev_name = 0;
  1339. up(&adpt_configuration_lock);
  1340. return 0;
  1341. }
  1342. static int adpt_open(struct inode *inode, struct file *file)
  1343. {
  1344. int minor;
  1345. adpt_hba* pHba;
  1346. //TODO check for root access
  1347. //
  1348. minor = MINOR(inode->i_rdev);
  1349. if (minor >= hba_count) {
  1350. return -ENXIO;
  1351. }
  1352. down(&adpt_configuration_lock);
  1353. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1354. if (pHba->unit == minor) {
  1355. break; /* found adapter */
  1356. }
  1357. }
  1358. if (pHba == NULL) {
  1359. up(&adpt_configuration_lock);
  1360. return -ENXIO;
  1361. }
  1362. // if(pHba->in_use){
  1363. // up(&adpt_configuration_lock);
  1364. // return -EBUSY;
  1365. // }
  1366. pHba->in_use = 1;
  1367. up(&adpt_configuration_lock);
  1368. return 0;
  1369. }
  1370. static int adpt_close(struct inode *inode, struct file *file)
  1371. {
  1372. int minor;
  1373. adpt_hba* pHba;
  1374. minor = MINOR(inode->i_rdev);
  1375. if (minor >= hba_count) {
  1376. return -ENXIO;
  1377. }
  1378. down(&adpt_configuration_lock);
  1379. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1380. if (pHba->unit == minor) {
  1381. break; /* found adapter */
  1382. }
  1383. }
  1384. up(&adpt_configuration_lock);
  1385. if (pHba == NULL) {
  1386. return -ENXIO;
  1387. }
  1388. pHba->in_use = 0;
  1389. return 0;
  1390. }
  1391. static int adpt_i2o_passthru(adpt_hba* pHba, u32* arg)
  1392. {
  1393. u32 msg[MAX_MESSAGE_SIZE];
  1394. u32* reply = NULL;
  1395. u32 size = 0;
  1396. u32 reply_size = 0;
  1397. u32* user_msg = (u32*)arg;
  1398. u32* user_reply = NULL;
  1399. ulong sg_list[pHba->sg_tablesize];
  1400. u32 sg_offset = 0;
  1401. u32 sg_count = 0;
  1402. int sg_index = 0;
  1403. u32 i = 0;
  1404. u32 rcode = 0;
  1405. ulong p = 0;
  1406. ulong flags = 0;
  1407. memset(&msg, 0, MAX_MESSAGE_SIZE*4);
  1408. // get user msg size in u32s 
  1409. if(get_user(size, &user_msg[0])){
  1410. return -EFAULT;
  1411. }
  1412. size = size>>16;
  1413. user_reply = &user_msg[size];
  1414. if(size > MAX_MESSAGE_SIZE){
  1415. return -EFAULT;
  1416. }
  1417. size *= 4; // Convert to bytes
  1418. /* Copy in the user's I2O command */
  1419. if(copy_from_user((void*)msg, (void*)user_msg, size)) {
  1420. return -EFAULT;
  1421. }
  1422. get_user(reply_size, &user_reply[0]);
  1423. reply_size = reply_size>>16;
  1424. if(reply_size > REPLY_FRAME_SIZE){
  1425. reply_size = REPLY_FRAME_SIZE;
  1426. }
  1427. reply_size *= 4;
  1428. reply = kmalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
  1429. if(reply == NULL) {
  1430. printk(KERN_WARNING"%s: Could not allocate reply buffern",pHba->name);
  1431. return -ENOMEM;
  1432. }
  1433. memset(reply,0,REPLY_FRAME_SIZE*4);
  1434. sg_offset = (msg[0]>>4)&0xf;
  1435. msg[2] = 0x40000000; // IOCTL context
  1436. msg[3] = (u32)reply;
  1437. memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
  1438. if(sg_offset) {
  1439. // TODO 64bit fix
  1440. struct sg_simple_element *sg =  (struct sg_simple_element*) (msg+sg_offset);
  1441. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1442. if (sg_count > pHba->sg_tablesize){
  1443. printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)n", pHba->name,sg_count);
  1444. kfree (reply);
  1445. return -EINVAL;
  1446. }
  1447. for(i = 0; i < sg_count; i++) {
  1448. int sg_size;
  1449. if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
  1450. printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)n",pHba->name,i,  sg[i].flag_count);
  1451. rcode = -EINVAL;
  1452. goto cleanup;
  1453. }
  1454. sg_size = sg[i].flag_count & 0xffffff;      
  1455. /* Allocate memory for the transfer */
  1456. p = (ulong)kmalloc(sg_size, GFP_KERNEL|ADDR32);
  1457. if(p == 0) {
  1458. printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %dn",
  1459. pHba->name,sg_size,i,sg_count);
  1460. rcode = -ENOMEM;
  1461. goto cleanup;
  1462. }
  1463. sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
  1464. /* Copy in the user's SG buffer if necessary */
  1465. if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
  1466. // TODO 64bit fix
  1467. if (copy_from_user((void*)p,(void*)sg[i].addr_bus, sg_size)) {
  1468. printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM usern",pHba->name,i);
  1469. rcode = -EFAULT;
  1470. goto cleanup;
  1471. }
  1472. }
  1473. //TODO 64bit fix
  1474. sg[i].addr_bus = (u32)virt_to_bus((void*)p);
  1475. }
  1476. }
  1477. do {
  1478. spin_lock_irqsave(&io_request_lock, flags);
  1479. // This state stops any new commands from enterring the
  1480. // controller while processing the ioctl
  1481. // pHba->state |= DPTI_STATE_IOCTL;
  1482. // We can't set this now - The scsi subsystem sets host_blocked and
  1483. // the queue empties and stops.  We need a way to restart the queue
  1484. rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
  1485. // pHba->state &= ~DPTI_STATE_IOCTL;
  1486. spin_unlock_irqrestore(&io_request_lock, flags);
  1487. } while(rcode == -ETIMEDOUT);  
  1488. if(rcode){
  1489. goto cleanup;
  1490. }
  1491. if(sg_offset) {
  1492. /* Copy back the Scatter Gather buffers back to user space */
  1493. u32 j;
  1494. // TODO 64bit fix
  1495. struct sg_simple_element* sg;
  1496. int sg_size;
  1497. // re-acquire the original message to handle correctly the sg copy operation
  1498. memset(&msg, 0, MAX_MESSAGE_SIZE*4); 
  1499. // get user msg size in u32s 
  1500. if(get_user(size, &user_msg[0])){
  1501. rcode = -EFAULT; 
  1502. goto cleanup; 
  1503. }
  1504. size = size>>16;
  1505. size *= 4;
  1506. /* Copy in the user's I2O command */
  1507. if (copy_from_user ((void*)msg, (void*)user_msg, size)) {
  1508. rcode = -EFAULT;
  1509. goto cleanup;
  1510. }
  1511. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1512. // TODO 64bit fix
  1513. sg   = (struct sg_simple_element*)(msg + sg_offset);
  1514. for (j = 0; j < sg_count; j++) {
  1515. /* Copy out the SG list to user's buffer if necessary */
  1516. if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
  1517. sg_size = sg[j].flag_count & 0xffffff; 
  1518. // TODO 64bit fix
  1519. if (copy_to_user((void*)sg[j].addr_bus,(void*)sg_list[j], sg_size)) {
  1520. printk(KERN_WARNING"%s: Could not copy %lx TO user %xn",pHba->name, sg_list[j], sg[j].addr_bus);
  1521. rcode = -EFAULT;
  1522. goto cleanup;
  1523. }
  1524. }
  1525. }
  1526. /* Copy back the reply to user space */
  1527. if (reply_size) {
  1528. // we wrote our own values for context - now restore the user supplied ones
  1529. if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
  1530. printk(KERN_WARNING"%s: Could not copy message context FROM usern",pHba->name);
  1531. rcode = -EFAULT;
  1532. }
  1533. if(copy_to_user(user_reply, reply, reply_size)) {
  1534. printk(KERN_WARNING"%s: Could not copy reply TO usern",pHba->name);
  1535. rcode = -EFAULT;
  1536. }
  1537. }
  1538. cleanup:
  1539. kfree (reply);
  1540. while(sg_index) {
  1541. if(sg_list[--sg_index]) {
  1542. kfree((void*)(sg_list[sg_index]));
  1543. }
  1544. }
  1545. return rcode;
  1546. }
  1547. /*
  1548.  * This routine returns information about the system.  This does not effect
  1549.  * any logic and if the info is wrong - it doesn't matter.
  1550.  */
  1551. /* Get all the info we can not get from kernel services */
  1552. static int adpt_system_info(void *buffer)
  1553. {
  1554. sysInfo_S si;
  1555. memset(&si, 0, sizeof(si));
  1556. si.osType = OS_LINUX;
  1557. si.osMajorVersion = (u8) (LINUX_VERSION_CODE >> 16);
  1558. si.osMinorVersion = (u8) (LINUX_VERSION_CODE >> 8 & 0x0ff);
  1559. si.osRevision =     (u8) (LINUX_VERSION_CODE & 0x0ff);
  1560. si.busType = SI_PCI_BUS;
  1561. si.processorFamily = DPTI_sig.dsProcessorFamily;
  1562. #if defined __i386__ 
  1563. adpt_i386_info(&si);
  1564. #elif defined (__ia64__)
  1565. adpt_ia64_info(&si);
  1566. #elif defined(__sparc__)
  1567. adpt_sparc_info(&si);
  1568. #elif defined (__alpha__)
  1569. adpt_alpha_info(&si);
  1570. #else
  1571. si.processorType = 0xff ;
  1572. #endif
  1573. if(copy_to_user(buffer, &si, sizeof(si))){
  1574. printk(KERN_WARNING"dpti: Could not copy buffer TO usern");
  1575. return -EFAULT;
  1576. }
  1577. return 0;
  1578. }
  1579. #if defined __ia64__ 
  1580. static void adpt_ia64_info(sysInfo_S* si)
  1581. {
  1582. // This is all the info we need for now
  1583. // We will add more info as our new
  1584. // managmenent utility requires it
  1585. si->processorType = PROC_IA64;
  1586. }
  1587. #endif
  1588. #if defined __sparc__ 
  1589. static void adpt_sparc_info(sysInfo_S* si)
  1590. {
  1591. // This is all the info we need for now
  1592. // We will add more info as our new
  1593. // managmenent utility requires it
  1594. si->processorType = PROC_ULTRASPARC;
  1595. }
  1596. #endif
  1597. #if defined __alpha__ 
  1598. static void adpt_alpha_info(sysInfo_S* si)
  1599. {
  1600. // This is all the info we need for now
  1601. // We will add more info as our new
  1602. // managmenent utility requires it
  1603. si->processorType = PROC_ALPHA;
  1604. }
  1605. #endif
  1606. #if defined __i386__
  1607. static void adpt_i386_info(sysInfo_S* si)
  1608. {
  1609. // This is all the info we need for now
  1610. // We will add more info as our new
  1611. // managmenent utility requires it
  1612. switch (boot_cpu_data.x86) {
  1613. case CPU_386:
  1614. si->processorType = PROC_386;
  1615. break;
  1616. case CPU_486:
  1617. si->processorType = PROC_486;
  1618. break;
  1619. case CPU_586:
  1620. si->processorType = PROC_PENTIUM;
  1621. break;
  1622. default:  // Just in case 
  1623. si->processorType = PROC_PENTIUM;
  1624. break;
  1625. }
  1626. }
  1627. #endif
  1628. static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd,
  1629.       ulong arg)
  1630. {
  1631. int minor;
  1632. int error = 0;
  1633. adpt_hba* pHba;
  1634. ulong flags;
  1635. minor = MINOR(inode->i_rdev);
  1636. if (minor >= DPTI_MAX_HBA){
  1637. return -ENXIO;
  1638. }
  1639. down(&adpt_configuration_lock);
  1640. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1641. if (pHba->unit == minor) {
  1642. break; /* found adapter */
  1643. }
  1644. }
  1645. up(&adpt_configuration_lock);
  1646. if(pHba == NULL){
  1647. return -ENXIO;
  1648. }
  1649. while((volatile u32) pHba->state & DPTI_STATE_RESET ) {
  1650. set_task_state(current,TASK_UNINTERRUPTIBLE);
  1651. schedule_timeout(2);
  1652. }
  1653. switch (cmd) {
  1654. // TODO: handle 3 cases
  1655. case DPT_SIGNATURE:
  1656. if (copy_to_user((char*)arg, &DPTI_sig, sizeof(DPTI_sig))) {
  1657. return -EFAULT;
  1658. }
  1659. break;
  1660. case I2OUSRCMD:
  1661. return adpt_i2o_passthru(pHba,(u32*)arg);
  1662. break;
  1663. case DPT_CTRLINFO:{
  1664. drvrHBAinfo_S HbaInfo;
  1665. #define FLG_OSD_PCI_VALID 0x0001
  1666. #define FLG_OSD_DMA   0x0002
  1667. #define FLG_OSD_I2O   0x0004
  1668. memset(&HbaInfo, 0, sizeof(HbaInfo));
  1669. HbaInfo.drvrHBAnum = pHba->unit;
  1670. HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
  1671. HbaInfo.blinkState = adpt_read_blink_led(pHba);
  1672. HbaInfo.pciBusNum =  pHba->pDev->bus->number;
  1673. HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn); 
  1674. HbaInfo.Interrupt = pHba->pDev->irq; 
  1675. HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
  1676. if(copy_to_user((void *) arg, &HbaInfo, sizeof(HbaInfo))){
  1677. printk(KERN_WARNING"%s: Could not copy HbaInfo TO usern",pHba->name);
  1678. return -EFAULT;
  1679. }
  1680. break;
  1681. }
  1682. case DPT_SYSINFO:
  1683. return adpt_system_info((void*)arg);
  1684. break;
  1685. case DPT_BLINKLED:{
  1686. u32 value;
  1687. value = (u32)adpt_read_blink_led(pHba);
  1688. if (copy_to_user((char*)arg, &value, sizeof(value))) {
  1689. return -EFAULT;
  1690. }
  1691. break;
  1692. }
  1693. case I2ORESETCMD:
  1694. spin_lock_irqsave(&io_request_lock, flags);
  1695. adpt_hba_reset(pHba);
  1696. spin_unlock_irqrestore(&io_request_lock, flags);
  1697. break;
  1698. case I2ORESCANCMD:
  1699. adpt_rescan(pHba);
  1700. break;
  1701. case DPT_TARGET_BUSY & 0xFFFF:
  1702. case DPT_TARGET_BUSY:
  1703. {
  1704. TARGET_BUSY_T busy;
  1705. struct adpt_device* d;
  1706. if (copy_from_user((void*)&busy, (void*)arg, sizeof(TARGET_BUSY_T))) {
  1707. return -EFAULT;
  1708. }
  1709. d = adpt_find_device(pHba, busy.channel, busy.id, busy.lun);
  1710. if(d == NULL){
  1711. return -ENODEV;
  1712. }
  1713. busy.isBusy = ((d->pScsi_dev) && (0 != d->pScsi_dev->access_count)) ? 1 : 0;
  1714. if (copy_to_user ((char*)arg, &busy, sizeof(busy))) {
  1715. return -EFAULT;
  1716. }
  1717. break;
  1718. }
  1719. default:
  1720. return -EINVAL;
  1721. }
  1722. return error;
  1723. }
  1724. static void adpt_isr(int irq, void *dev_id, struct pt_regs *regs)
  1725. {
  1726. Scsi_Cmnd* cmd;
  1727. adpt_hba* pHba=NULL;
  1728. u32 m;
  1729. ulong reply;
  1730. u32 status=0;
  1731. u32 context;
  1732. ulong flags = 0;
  1733. pHba = dev_id;
  1734. if (pHba == NULL ){
  1735. printk(KERN_WARNING"adpt_isr: NULL dev_idn");
  1736. return;
  1737. }
  1738. spin_lock_irqsave(&io_request_lock, flags);
  1739. while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
  1740. m = readl(pHba->reply_port);
  1741. if(m == EMPTY_QUEUE){
  1742. // Try twice then give up
  1743. rmb();
  1744. m = readl(pHba->reply_port);
  1745. if(m == EMPTY_QUEUE){ 
  1746. // This really should not happen
  1747. printk(KERN_ERR"dpti: Could not get reply framen");
  1748. spin_unlock_irqrestore(&io_request_lock,flags);
  1749. return;
  1750. }
  1751. }
  1752. reply = (ulong)bus_to_virt(m);
  1753. if (readl(reply) & MSG_FAIL) {
  1754. u32 old_m = readl(reply+28); 
  1755. ulong msg;
  1756. u32 old_context;
  1757. PDEBUG("%s: Failed messagen",pHba->name);
  1758. if(old_m >= 0x100000){
  1759. printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping framen",pHba->name,old_m);
  1760. writel(m,pHba->reply_port);
  1761. continue;
  1762. }
  1763. // Transaction context is 0 in failed reply frame
  1764. msg = (ulong)(pHba->msg_addr_virt + old_m);
  1765. old_context = readl(msg+12);
  1766. writel(old_context, reply+12);
  1767. adpt_send_nop(pHba, old_m);
  1768. context = readl(reply+8);
  1769. if(context & 0x40000000){ // IOCTL
  1770. ulong p = (ulong)(readl(reply+12));
  1771. if( p != 0) {
  1772. memcpy((void*)p, (void*)reply, REPLY_FRAME_SIZE * 4);
  1773. }
  1774. // All IOCTLs will also be post wait
  1775. }
  1776. if(context & 0x80000000){ // Post wait message
  1777. status = readl(reply+16);
  1778. if(status  >> 24){
  1779. status &=  0xffff; /* Get detail status */
  1780. } else {
  1781. status = I2O_POST_WAIT_OK;
  1782. }
  1783. if(!(context & 0x40000000)) {
  1784. cmd = (Scsi_Cmnd*) readl(reply+12); 
  1785. if(cmd != NULL) {
  1786. printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%xn", pHba->name, cmd, context);
  1787. }
  1788. }
  1789. adpt_i2o_post_wait_complete(context, status);
  1790. } else { // SCSI message
  1791. cmd = (Scsi_Cmnd*) readl(reply+12); 
  1792. if(cmd != NULL){
  1793. if(cmd->serial_number != 0) { // If not timedout
  1794. adpt_i2o_to_scsi(reply, cmd);
  1795. }
  1796. }
  1797. }
  1798. writel(m, pHba->reply_port);
  1799. wmb();
  1800. rmb();
  1801. }
  1802. spin_unlock_irqrestore(&io_request_lock, flags);
  1803. return;
  1804. }
  1805. static s32 adpt_scsi_to_i2o(adpt_hba* pHba, Scsi_Cmnd* cmd, struct adpt_device* d)
  1806. {
  1807. int i;
  1808. u32 msg[MAX_MESSAGE_SIZE];
  1809. u32* mptr;
  1810. u32 *lenptr;
  1811. int direction;
  1812. int scsidir;
  1813. u32 len;
  1814. u32 reqlen;
  1815. s32 rcode;
  1816. memset(msg, 0 , sizeof(msg));
  1817. len = cmd->request_bufflen;
  1818. direction = 0x00000000;
  1819. scsidir = 0x00000000; // DATA NO XFER
  1820. if(len) {
  1821. /*
  1822.  * Set SCBFlags to indicate if data is being transferred
  1823.  * in or out, or no data transfer
  1824.  * Note:  Do not have to verify index is less than 0 since
  1825.  * cmd->cmnd[0] is an unsigned char
  1826.  */
  1827. switch(cmd->sc_data_direction){
  1828. case SCSI_DATA_READ:
  1829. scsidir  =0x40000000; // DATA IN  (iop<--dev)
  1830. break;
  1831. case SCSI_DATA_WRITE:
  1832. direction=0x04000000; // SGL OUT
  1833. scsidir  =0x80000000; // DATA OUT (iop-->dev)
  1834. break;
  1835. case SCSI_DATA_NONE:
  1836. break;
  1837. case SCSI_DATA_UNKNOWN:
  1838. scsidir  =0x40000000; // DATA IN  (iop<--dev)
  1839. // Assume In - and continue;
  1840. break;
  1841. default:
  1842. printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.n",
  1843.      pHba->name, cmd->cmnd[0]);
  1844. cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
  1845. cmd->scsi_done(cmd);
  1846. return  0;
  1847. }
  1848. }
  1849. // msg[0] is set later
  1850. // I2O_CMD_SCSI_EXEC
  1851. msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
  1852. msg[2] = 0;
  1853. msg[3] = (u32)cmd; /* We want the SCSI control block back */
  1854. // Our cards use the transaction context as the tag for queueing
  1855. // Adaptec/DPT Private stuff 
  1856. msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
  1857. msg[5] = d->tid;
  1858. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  1859. // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
  1860. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
  1861. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  1862. msg[6] = scsidir|0x20a00000|cmd->cmd_len;
  1863. mptr=msg+7;
  1864. // Write SCSI command into the message - always 16 byte block 
  1865. memset(mptr, 0,  16);
  1866. memcpy(mptr, cmd->cmnd, cmd->cmd_len);
  1867. mptr+=4;
  1868. lenptr=mptr++; /* Remember me - fill in when we know */
  1869. reqlen = 14; // SINGLE SGE
  1870. /* Now fill in the SGList and command */
  1871. if(cmd->use_sg) {
  1872. struct scatterlist *sg = (struct scatterlist *)cmd->request_buffer;
  1873. len = 0;
  1874. for(i = 0 ; i < cmd->use_sg; i++) {
  1875. *mptr++ = direction|0x10000000|sg->length;
  1876. len+=sg->length;
  1877. *mptr++ = virt_to_bus(sg->address);
  1878. sg++;
  1879. }
  1880. /* Make this an end of list */
  1881. mptr[-2] = direction|0xD0000000|(sg-1)->length;
  1882. reqlen = mptr - msg;
  1883. *lenptr = len;
  1884. if(cmd->underflow && len != cmd->underflow){
  1885. printk(KERN_WARNING"Cmd len %08X Cmd underflow %08Xn",
  1886. len, cmd->underflow);
  1887. }
  1888. } else {
  1889. *lenptr = len = cmd->request_bufflen;
  1890. if(len == 0) {
  1891. reqlen = 12;
  1892. } else {
  1893. *mptr++ = 0xD0000000|direction|cmd->request_bufflen;
  1894. *mptr++ = virt_to_bus(cmd->request_buffer);
  1895. }
  1896. }
  1897. /* Stick the headers on */
  1898. msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
  1899. // Send it on it's way
  1900. rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
  1901. if (rcode == 0) {
  1902. return 0;
  1903. }
  1904. return rcode;
  1905. }
  1906. static s32 adpt_scsi_register(adpt_hba* pHba,Scsi_Host_Template * sht)
  1907. {
  1908. struct Scsi_Host *host = NULL;
  1909. host = scsi_register(sht, sizeof(adpt_hba*));
  1910. if (host == NULL) {
  1911. printk ("%s: scsi_register returned NULLn",pHba->name);
  1912. return -1;
  1913. }
  1914. (adpt_hba*)(host->hostdata[0]) = pHba;
  1915. pHba->host = host;
  1916. host->irq = pHba->pDev->irq;;
  1917. /* no IO ports, so don't have to set host->io_port and 
  1918.  * host->n_io_port
  1919.  */
  1920. host->io_port = 0;
  1921. host->n_io_port = 0;
  1922. /* see comments in hosts.h */
  1923. host->max_id = 16;
  1924. host->max_lun = 256;
  1925. host->max_channel = pHba->top_scsi_channel + 1;
  1926. host->cmd_per_lun = 256;
  1927. host->unique_id = (uint) pHba;
  1928. host->sg_tablesize = pHba->sg_tablesize;
  1929. host->can_queue = pHba->post_fifo_size;
  1930. host->select_queue_depths = adpt_select_queue_depths;
  1931. return 0;
  1932. }
  1933. static s32 adpt_i2o_to_scsi(ulong reply, Scsi_Cmnd* cmd)
  1934. {
  1935. adpt_hba* pHba;
  1936. u32 hba_status;
  1937. u32 dev_status;
  1938. u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits 
  1939. // I know this would look cleaner if I just read bytes
  1940. // but the model I have been using for all the rest of the
  1941. // io is in 4 byte words - so I keep that model
  1942. u16 detailed_status = readl(reply+16) &0xffff;
  1943. dev_status = (detailed_status & 0xff);
  1944. hba_status = detailed_status >> 8;
  1945. // calculate resid for sg 
  1946. cmd->resid = cmd->request_bufflen - readl(reply+5);
  1947. pHba = (adpt_hba*) cmd->host->hostdata[0];
  1948. cmd->sense_buffer[0] = '';  // initialize sense valid flag to false
  1949. if(!(reply_flags & MSG_FAIL)) {
  1950. switch(detailed_status & I2O_SCSI_DSC_MASK) {
  1951. case I2O_SCSI_DSC_SUCCESS:
  1952. cmd->result = (DID_OK << 16);
  1953. // handle underflow
  1954. if(readl(reply+5) < cmd->underflow ) {
  1955. cmd->result = (DID_ERROR <<16);
  1956. printk(KERN_WARNING"%s: SCSI CMD underflown",pHba->name);
  1957. }
  1958. break;
  1959. case I2O_SCSI_DSC_REQUEST_ABORTED:
  1960. cmd->result = (DID_ABORT << 16);
  1961. break;
  1962. case I2O_SCSI_DSC_PATH_INVALID:
  1963. case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
  1964. case I2O_SCSI_DSC_SELECTION_TIMEOUT:
  1965. case I2O_SCSI_DSC_COMMAND_TIMEOUT:
  1966. case I2O_SCSI_DSC_NO_ADAPTER:
  1967. case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
  1968. printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%xn",
  1969. pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd->lun, hba_status, dev_status, cmd->cmnd[0]);
  1970. cmd->result = (DID_TIME_OUT << 16);
  1971. break;
  1972. case I2O_SCSI_DSC_ADAPTER_BUSY:
  1973. case I2O_SCSI_DSC_BUS_BUSY:
  1974. cmd->result = (DID_BUS_BUSY << 16);
  1975. break;
  1976. case I2O_SCSI_DSC_SCSI_BUS_RESET:
  1977. case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
  1978. cmd->result = (DID_RESET << 16);
  1979. break;
  1980. case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
  1981. printk(KERN_WARNING"%s: SCSI CMD parity errorn",pHba->name);
  1982. cmd->result = (DID_PARITY << 16);
  1983. break;
  1984. case I2O_SCSI_DSC_UNABLE_TO_ABORT:
  1985. case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
  1986. case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
  1987. case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
  1988. case I2O_SCSI_DSC_AUTOSENSE_FAILED:
  1989. case I2O_SCSI_DSC_DATA_OVERRUN:
  1990. case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
  1991. case I2O_SCSI_DSC_SEQUENCE_FAILURE:
  1992. case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
  1993. case I2O_SCSI_DSC_PROVIDE_FAILURE:
  1994. case I2O_SCSI_DSC_REQUEST_TERMINATED:
  1995. case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
  1996. case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
  1997. case I2O_SCSI_DSC_MESSAGE_RECEIVED:
  1998. case I2O_SCSI_DSC_INVALID_CDB:
  1999. case I2O_SCSI_DSC_LUN_INVALID:
  2000. case I2O_SCSI_DSC_SCSI_TID_INVALID:
  2001. case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
  2002. case I2O_SCSI_DSC_NO_NEXUS:
  2003. case I2O_SCSI_DSC_CDB_RECEIVED:
  2004. case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
  2005. case I2O_SCSI_DSC_QUEUE_FROZEN:
  2006. case I2O_SCSI_DSC_REQUEST_INVALID:
  2007. default:
  2008. printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%xn",
  2009. pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun,
  2010.        hba_status, dev_status, cmd->cmnd[0]);
  2011. cmd->result = (DID_ERROR << 16);
  2012. break;
  2013. }
  2014. // copy over the request sense data if it was a check
  2015. // condition status
  2016. if(dev_status == 0x02 /*CHECK_CONDITION*/) {
  2017. u32 len = sizeof(cmd->sense_buffer);
  2018. len = (len > 40) ?  40 : len;
  2019. // Copy over the sense data
  2020. memcpy(cmd->sense_buffer, (void*)(reply+28) , len);
  2021. if(cmd->sense_buffer[0] == 0x70 /* class 7 */ && 
  2022.    cmd->sense_buffer[2] == DATA_PROTECT ){
  2023. /* This is to handle an array failed */
  2024. cmd->result = (DID_TIME_OUT << 16);
  2025. printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%xn",
  2026. pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd->lun, 
  2027. hba_status, dev_status, cmd->cmnd[0]);
  2028. }
  2029. }
  2030. } else {
  2031. /* In this condtion we could not talk to the tid
  2032.  * the card rejected it.  We should signal a retry
  2033.  * for a limitted number of retries.
  2034.  */
  2035. cmd->result = (DID_TIME_OUT << 16);
  2036. printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%xn",
  2037. pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun,
  2038. ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
  2039. }
  2040. cmd->result |= (dev_status);
  2041. if(cmd->scsi_done != NULL){
  2042. cmd->scsi_done(cmd);
  2043. return cmd->result;
  2044. }
  2045. static s32 adpt_rescan(adpt_hba* pHba)
  2046. {
  2047. s32 rcode;
  2048. ulong flags;
  2049. spin_lock_irqsave(&io_request_lock, flags);
  2050. if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
  2051. spin_unlock_irqrestore(&io_request_lock, flags);
  2052. return rcode;
  2053. }
  2054. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
  2055. spin_unlock_irqrestore(&io_request_lock, flags);
  2056. return rcode;
  2057. }
  2058. spin_unlock_irqrestore(&io_request_lock, flags);
  2059. return 0;
  2060. }
  2061. static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
  2062. {
  2063. int i;
  2064. int max;
  2065. int tid;
  2066. struct i2o_device *d;
  2067. i2o_lct *lct = pHba->lct;
  2068. u8 bus_no = 0;
  2069. s16 scsi_id;
  2070. s16 scsi_lun;
  2071. u32 buf[10]; // at least 8 u32's
  2072. struct adpt_device* pDev = NULL;
  2073. struct i2o_device* pI2o_dev = NULL;
  2074. if (lct == NULL) {
  2075. printk(KERN_ERR "%s: LCT is empty???n",pHba->name);
  2076. return -1;
  2077. }
  2078. max = lct->table_size;
  2079. max -= 3;
  2080. max /= 9;
  2081. // Mark each drive as unscanned
  2082. for (d = pHba->devices; d; d = d->next) {
  2083. pDev =(struct adpt_device*) d->owner;
  2084. if(!pDev){
  2085. continue;
  2086. }
  2087. pDev->state |= DPTI_DEV_UNSCANNED;
  2088. }
  2089. printk(KERN_INFO "%s: LCT has %d entries.n", pHba->name,max);
  2090. for(i=0;i<max;i++) {
  2091. if( lct->lct_entry[i].user_tid != 0xfff){
  2092. continue;
  2093. }
  2094. if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  2095.     lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
  2096.     lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  2097. tid = lct->lct_entry[i].tid;
  2098. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  2099. printk(KERN_ERR"%s: Could not query devicen",pHba->name);
  2100. continue;
  2101. }
  2102. bus_no = buf[0]>>16;
  2103. scsi_id = buf[1];
  2104. scsi_lun = (buf[2]>>8 )&0xff;
  2105. pDev = pHba->channel[bus_no].device[scsi_id];
  2106. /* da lun */
  2107. while(pDev) {
  2108. if(pDev->scsi_lun == scsi_lun) {
  2109. break;
  2110. }
  2111. pDev = pDev->next_lun;
  2112. }
  2113. if(!pDev ) { // Something new add it
  2114. d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
  2115. if(d==NULL)
  2116. {
  2117. printk(KERN_CRIT "Out of memory for I2O device data.n");
  2118. return -ENOMEM;
  2119. }
  2120. d->controller = (void*)pHba;
  2121. d->next = NULL;
  2122. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2123. d->flags = 0;
  2124. adpt_i2o_report_hba_unit(pHba, d);
  2125. adpt_i2o_install_device(pHba, d);
  2126. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  2127. printk(KERN_WARNING"%s: Channel number %d out of range n", pHba->name, bus_no);
  2128. continue;
  2129. }
  2130. pDev = pHba->channel[bus_no].device[scsi_id];
  2131. if( pDev == NULL){
  2132. pDev =  kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
  2133. if(pDev == NULL) {
  2134. return -ENOMEM;
  2135. }
  2136. pHba->channel[bus_no].device[scsi_id] = pDev;
  2137. } else {
  2138. while (pDev->next_lun) {
  2139. pDev = pDev->next_lun;
  2140. }
  2141. pDev = pDev->next_lun = kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
  2142. if(pDev == NULL) {
  2143. return -ENOMEM;
  2144. }
  2145. }
  2146. memset(pDev,0,sizeof(struct adpt_device));
  2147. pDev->tid = d->lct_data.tid;
  2148. pDev->scsi_channel = bus_no;
  2149. pDev->scsi_id = scsi_id;
  2150. pDev->scsi_lun = scsi_lun;
  2151. pDev->pI2o_dev = d;
  2152. d->owner = pDev;
  2153. pDev->type = (buf[0])&0xff;
  2154. pDev->flags = (buf[0]>>8)&0xff;
  2155. // Too late, SCSI system has made up it's mind, but what the hey ...
  2156. if(scsi_id > pHba->top_scsi_id){
  2157. pHba->top_scsi_id = scsi_id;
  2158. }
  2159. if(scsi_lun > pHba->top_scsi_lun){
  2160. pHba->top_scsi_lun = scsi_lun;
  2161. }
  2162. continue;
  2163. } // end of new i2o device
  2164. // We found an old device - check it
  2165. while(pDev) {
  2166. if(pDev->scsi_lun == scsi_lun) {
  2167. if(pDev->pScsi_dev->online == FALSE) {
  2168. printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back onlinen",
  2169. pHba->name,bus_no,scsi_id,scsi_lun);
  2170. if (pDev->pScsi_dev) {
  2171. pDev->pScsi_dev->online = TRUE;
  2172. }
  2173. }
  2174. d = pDev->pI2o_dev;
  2175. if(d->lct_data.tid != tid) { // something changed
  2176. pDev->tid = tid;
  2177. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2178. if (pDev->pScsi_dev) {
  2179. pDev->pScsi_dev->changed = TRUE;
  2180. pDev->pScsi_dev->removable = TRUE;
  2181. }
  2182. }
  2183. // Found it - mark it scanned
  2184. pDev->state = DPTI_DEV_ONLINE;
  2185. break;
  2186. }
  2187. pDev = pDev->next_lun;
  2188. }
  2189. }
  2190. }
  2191. for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
  2192. pDev =(struct adpt_device*) pI2o_dev->owner;
  2193. if(!pDev){
  2194. continue;
  2195. }
  2196. // Drive offline drives that previously existed but could not be found
  2197. // in the LCT table
  2198. if (pDev->state & DPTI_DEV_UNSCANNED){
  2199. pDev->state = DPTI_DEV_OFFLINE;
  2200. printk(KERN_WARNING"%s: Device (%d,%d,%d) offlinen",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
  2201. if (pDev->pScsi_dev) {
  2202. pDev->pScsi_dev->online = FALSE;
  2203. if (pDev->pScsi_dev->access_count) {
  2204. // A drive that was mounted is no longer there... bad!
  2205. SCSI_LOG_ERROR_RECOVERY(1, printk ("%s:Rescan: Previously "
  2206.  "mounted drive not found!n",pHba->name));
  2207. printk(KERN_WARNING"%s:Mounted drive taken offlinen",pHba->name);
  2208. }
  2209. }
  2210. }
  2211. }
  2212. return 0;
  2213. }
  2214. static void adpt_fail_posted_scbs(adpt_hba* pHba)
  2215. {
  2216. Scsi_Cmnd*  cmd = NULL;
  2217. Scsi_Device*  d = NULL;
  2218. if( pHba->host->host_queue != NULL ) {
  2219. d = pHba->host->host_queue;
  2220. if(!d){
  2221. return;
  2222. }
  2223. while( d->next != NULL ){
  2224. for(cmd = d->device_queue; cmd ; cmd = cmd->next){
  2225. if(cmd->serial_number == 0){
  2226. continue;
  2227. }
  2228. cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
  2229. cmd->scsi_done(cmd);
  2230. }
  2231. d = d->next;
  2232. }
  2233. }
  2234. }
  2235. /*============================================================================
  2236.  *  Routines from i2o subsystem
  2237.  *============================================================================
  2238.  */
  2239. /*
  2240.  * Bring an I2O controller into HOLD state. See the spec.
  2241.  */
  2242. static int adpt_i2o_activate_hba(adpt_hba* pHba)
  2243. {
  2244. int rcode;
  2245. if(pHba->initialized ) {
  2246. if (adpt_i2o_status_get(pHba) < 0) {
  2247. if((rcode = adpt_i2o_reset_hba(pHba) != 0)){
  2248. printk(KERN_WARNING"%s: Could NOT reset.n", pHba->name);
  2249. return rcode;
  2250. }
  2251. if (adpt_i2o_status_get(pHba) < 0) {
  2252. printk(KERN_INFO "HBA not responding.n");
  2253. return -1;
  2254. }
  2255. }
  2256. if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
  2257. printk(KERN_CRIT "%s: hardware faultn", pHba->name);
  2258. return -1;
  2259. }
  2260. if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
  2261.     pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
  2262.     pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
  2263.     pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
  2264. adpt_i2o_reset_hba(pHba);
  2265. if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  2266. printk(KERN_ERR "%s: Failed to initialize.n", pHba->name);
  2267. return -1;
  2268. }
  2269. }
  2270. } else {
  2271. if((rcode = adpt_i2o_reset_hba(pHba) != 0)){
  2272. printk(KERN_WARNING"%s: Could NOT reset.n", pHba->name);
  2273. return rcode;
  2274. }
  2275. }
  2276. if (adpt_i2o_init_outbound_q(pHba) < 0) {
  2277. return -1;
  2278. }
  2279. /* In HOLD state */
  2280. if (adpt_i2o_hrt_get(pHba) < 0) {
  2281. return -1;
  2282. }
  2283. return 0;
  2284. }
  2285. /*
  2286.  * Bring a controller online into OPERATIONAL state. 
  2287.  */
  2288.  
  2289. static int adpt_i2o_online_hba(adpt_hba* pHba)
  2290. {
  2291. if (adpt_i2o_systab_send(pHba) < 0) {
  2292. adpt_i2o_delete_hba(pHba);
  2293. return -1;
  2294. }
  2295. /* In READY state */
  2296. if (adpt_i2o_enable_hba(pHba) < 0) {
  2297. adpt_i2o_delete_hba(pHba);
  2298. return -1;
  2299. }
  2300. /* In OPERATIONAL state  */
  2301. return 0;
  2302. }
  2303. static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
  2304. {
  2305. u32 *msg;
  2306. ulong timeout = jiffies + 5*HZ;
  2307. while(m == EMPTY_QUEUE){
  2308. rmb();
  2309. m = readl(pHba->post_port);
  2310. if(m != EMPTY_QUEUE){
  2311. break;
  2312. }
  2313. if(time_after(jiffies,timeout)){
  2314. printk(KERN_ERR "%s: Timeout waiting for message frame!n",pHba->name);
  2315. return 2;
  2316. }
  2317. }
  2318. msg = (u32*)(pHba->msg_addr_virt + m);
  2319. writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
  2320. writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
  2321. writel( 0,&msg[2]);
  2322. wmb();
  2323. writel(m, pHba->post_port);
  2324. wmb();
  2325. return 0;
  2326. }
  2327. static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
  2328. {
  2329. u8 *status;
  2330. u32 *msg = NULL;
  2331. int i;
  2332. ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
  2333. u32* ptr;
  2334. u32 outbound_frame;  // This had to be a 32 bit address
  2335. u32 m;
  2336. do {
  2337. rmb();
  2338. m = readl(pHba->post_port);
  2339. if (m != EMPTY_QUEUE) {
  2340. break;
  2341. }
  2342. if(time_after(jiffies,timeout)){
  2343. printk(KERN_WARNING"%s: Timeout waiting for message framen",pHba->name);
  2344. return -ETIMEDOUT;
  2345. }
  2346. } while(m == EMPTY_QUEUE);
  2347. msg=(u32 *)(pHba->msg_addr_virt+m);
  2348. status = kmalloc(4,GFP_KERNEL|ADDR32);
  2349. if (status==NULL) {
  2350. adpt_send_nop(pHba, m);
  2351. printk(KERN_WARNING"%s: IOP reset failed - no free memory.n",
  2352. pHba->name);
  2353. return -ENOMEM;
  2354. }
  2355. memset(status, 0, 4);
  2356. writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
  2357. writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
  2358. writel(0, &msg[2]);
  2359. writel(0x0106, &msg[3]); /* Transaction context */
  2360. writel(4096, &msg[4]); /* Host page frame size */
  2361. writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
  2362. writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
  2363. writel(virt_to_bus(status), &msg[7]);
  2364. writel(m, pHba->post_port);
  2365. wmb();
  2366. // Wait for the reply status to come back
  2367. do {
  2368. if (*status) {
  2369. if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
  2370. break;
  2371. }
  2372. }
  2373. rmb();
  2374. if(time_after(jiffies,timeout)){
  2375. printk(KERN_WARNING"%s: Timeout Initializingn",pHba->name);
  2376. kfree((void*)status);
  2377. return -ETIMEDOUT;
  2378. }
  2379. } while (1);
  2380. // If the command was successful, fill the fifo with our reply
  2381. // message packets
  2382. if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
  2383. kfree((void*)status);
  2384. return -2;
  2385. }
  2386. kfree((void*)status);
  2387. if(pHba->reply_pool != NULL){
  2388. kfree(pHba->reply_pool);
  2389. }
  2390. pHba->reply_pool = (u32*)kmalloc(pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4, GFP_KERNEL|ADDR32);
  2391. if(!pHba->reply_pool){
  2392. printk(KERN_ERR"%s: Could not allocate reply pooln",pHba->name);
  2393. return -1;
  2394. }
  2395. memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
  2396. ptr = pHba->reply_pool;
  2397. for(i = 0; i < pHba->reply_fifo_size; i++) {
  2398. outbound_frame = (u32)virt_to_bus(ptr);
  2399. writel(outbound_frame, pHba->reply_port);
  2400. wmb();
  2401. ptr +=  REPLY_FRAME_SIZE;
  2402. }
  2403. adpt_i2o_status_get(pHba);
  2404. return 0;
  2405. }
  2406. /*
  2407.  * I2O System Table.  Contains information about
  2408.  * all the IOPs in the system.  Used to inform IOPs
  2409.  * about each other's existence.
  2410.  *
  2411.  * sys_tbl_ver is the CurrentChangeIndicator that is
  2412.  * used by IOPs to track changes.
  2413.  */
  2414. static s32 adpt_i2o_status_get(adpt_hba* pHba)
  2415. {
  2416. ulong timeout;
  2417. u32 m;
  2418. u32 *msg;
  2419. u8 *status_block=NULL;
  2420. ulong status_block_bus;
  2421. if(pHba->status_block == NULL) {
  2422. pHba->status_block = (i2o_status_block*)
  2423. kmalloc(sizeof(i2o_status_block),GFP_KERNEL|ADDR32);
  2424. if(pHba->status_block == NULL) {
  2425. printk(KERN_ERR
  2426. "dpti%d: Get Status Block failed; Out of memory. n", 
  2427. pHba->unit);
  2428. return -ENOMEM;
  2429. }
  2430. }
  2431. memset(pHba->status_block, 0, sizeof(i2o_status_block));
  2432. status_block = (u8*)(pHba->status_block);
  2433. status_block_bus = virt_to_bus(pHba->status_block);
  2434. timeout = jiffies+TMOUT_GETSTATUS*HZ;
  2435. do {
  2436. rmb();
  2437. m = readl(pHba->post_port);
  2438. if (m != EMPTY_QUEUE) {
  2439. break;
  2440. }
  2441. if(time_after(jiffies,timeout)){
  2442. printk(KERN_ERR "%s: Timeout waiting for message !n",
  2443. pHba->name);
  2444. return -ETIMEDOUT;
  2445. }
  2446. } while(m==EMPTY_QUEUE);
  2447. msg=(u32*)(pHba->msg_addr_virt+m);
  2448. writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
  2449. writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
  2450. writel(1, &msg[2]);
  2451. writel(0, &msg[3]);
  2452. writel(0, &msg[4]);
  2453. writel(0, &msg[5]);
  2454. writel(((u32)status_block_bus)&0xffffffff, &msg[6]);
  2455. writel(0, &msg[7]);
  2456. writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
  2457. //post message
  2458. writel(m, pHba->post_port);
  2459. wmb();
  2460. while(status_block[87]!=0xff){
  2461. if(time_after(jiffies,timeout)){
  2462. printk(KERN_ERR"dpti%d: Get status timeout.n",
  2463. pHba->unit);
  2464. return -ETIMEDOUT;
  2465. }
  2466. rmb();
  2467. }
  2468. // Set up our number of outbound and inbound messages
  2469. pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
  2470. if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
  2471. pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
  2472. }
  2473. pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
  2474. if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
  2475. pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
  2476. }
  2477. // Calculate the Scatter Gather list size
  2478. pHba->sg_tablesize = (pHba->status_block->inbound_frame_size * 4 -40)/ sizeof(struct sg_simple_element);
  2479. if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
  2480. pHba->sg_tablesize = SG_LIST_ELEMENTS;
  2481. }
  2482. #ifdef DEBUG
  2483. printk("dpti%d: State = ",pHba->unit);
  2484. switch(pHba->status_block->iop_state) {
  2485. case 0x01:
  2486. printk("INITn");
  2487. break;
  2488. case 0x02:
  2489. printk("RESETn");
  2490. break;
  2491. case 0x04:
  2492. printk("HOLDn");
  2493. break;
  2494. case 0x05:
  2495. printk("READYn");
  2496. break;
  2497. case 0x08:
  2498. printk("OPERATIONALn");
  2499. break;
  2500. case 0x10:
  2501. printk("FAILEDn");
  2502. break;
  2503. case 0x11:
  2504. printk("FAULTEDn");
  2505. break;
  2506. default:
  2507. printk("%x (unknown!!)n",pHba->status_block->iop_state);
  2508. }
  2509. #endif
  2510. return 0;
  2511. }
  2512. /*
  2513.  * Get the IOP's Logical Configuration Table
  2514.  */
  2515. static int adpt_i2o_lct_get(adpt_hba* pHba)
  2516. {
  2517. u32 msg[8];
  2518. int ret;
  2519. u32 buf[16];
  2520. if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
  2521. pHba->lct_size = pHba->status_block->expected_lct_size;
  2522. }
  2523. do {
  2524. if (pHba->lct == NULL) {
  2525. pHba->lct = kmalloc(pHba->lct_size, GFP_KERNEL|ADDR32);
  2526. if(pHba->lct == NULL) {
  2527. printk(KERN_CRIT "%s: Lct Get failed. Out of memory.n",
  2528. pHba->name);
  2529. return -ENOMEM;
  2530. }
  2531. }
  2532. memset(pHba->lct, 0, pHba->lct_size);
  2533. msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
  2534. msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
  2535. msg[2] = 0;
  2536. msg[3] = 0;
  2537. msg[4] = 0xFFFFFFFF; /* All devices */
  2538. msg[5] = 0x00000000; /* Report now */
  2539. msg[6] = 0xD0000000|pHba->lct_size;
  2540. msg[7] = virt_to_bus(pHba->lct);
  2541. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
  2542. printk(KERN_ERR "%s: LCT Get failed (status=%#10x.n", 
  2543. pHba->name, ret);
  2544. printk(KERN_ERR"Adaptec: Error Reading Hardware.n");
  2545. return ret;
  2546. }
  2547. if ((pHba->lct->table_size << 2) > pHba->lct_size) {
  2548. pHba->lct_size = pHba->lct->table_size << 2;
  2549. kfree(pHba->lct);
  2550. pHba->lct = NULL;
  2551. }
  2552. } while (pHba->lct == NULL);
  2553. PDEBUG("%s: Hardware resource table read.n", pHba->name);
  2554. // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
  2555. if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
  2556. pHba->FwDebugBufferSize = buf[1];
  2557. pHba->FwDebugBuffer_P    = pHba->base_addr_virt + buf[0];
  2558. pHba->FwDebugFlags_P     = pHba->FwDebugBuffer_P + FW_DEBUG_FLAGS_OFFSET;
  2559. pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P + FW_DEBUG_BLED_OFFSET;
  2560. pHba->FwDebugBLEDflag_P  = pHba->FwDebugBLEDvalue_P + 1;
  2561. pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P + FW_DEBUG_STR_LENGTH_OFFSET;
  2562. pHba->FwDebugBuffer_P += buf[2]; 
  2563. pHba->FwDebugFlags = 0;
  2564. }
  2565. return 0;
  2566. }
  2567. static int adpt_i2o_build_sys_table(void)
  2568. {
  2569. adpt_hba* pHba = NULL;
  2570. int count = 0;
  2571. sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
  2572. (hba_count) * sizeof(struct i2o_sys_tbl_entry);
  2573. if(sys_tbl)
  2574. kfree(sys_tbl);
  2575. sys_tbl = kmalloc(sys_tbl_len, GFP_KERNEL|ADDR32);
  2576. if(!sys_tbl) {
  2577. printk(KERN_WARNING "SysTab Set failed. Out of memory.n");
  2578. return -ENOMEM;
  2579. }
  2580. memset(sys_tbl, 0, sys_tbl_len);
  2581. sys_tbl->num_entries = hba_count;
  2582. sys_tbl->version = I2OVERSION;
  2583. sys_tbl->change_ind = sys_tbl_ind++;
  2584. for(pHba = hba_chain; pHba; pHba = pHba->next) {
  2585. // Get updated Status Block so we have the latest information
  2586. if (adpt_i2o_status_get(pHba)) {
  2587. sys_tbl->num_entries--;
  2588. continue; // try next one
  2589. }
  2590. sys_tbl->iops[count].org_id = pHba->status_block->org_id;
  2591. sys_tbl->iops[count].iop_id = pHba->unit + 2;
  2592. sys_tbl->iops[count].seg_num = 0;
  2593. sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
  2594. sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
  2595. sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
  2596. sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
  2597. sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
  2598. sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
  2599. sys_tbl->iops[count].inbound_low = (u32)virt_to_bus((void*)pHba->post_port);
  2600. sys_tbl->iops[count].inbound_high = (u32)((u64)virt_to_bus((void*)pHba->post_port)>>32);
  2601. count++;
  2602. }
  2603. #ifdef DEBUG
  2604. {
  2605. u32 *table = (u32*)sys_tbl;
  2606. printk(KERN_DEBUG"sys_tbl_len=%d in 32bit wordsn",(sys_tbl_len >>2));
  2607. for(count = 0; count < (sys_tbl_len >>2); count++) {
  2608. printk(KERN_INFO "sys_tbl[%d] = %0#10xn", 
  2609. count, table[count]);
  2610. }
  2611. }
  2612. #endif
  2613. return 0;
  2614. }
  2615. /*
  2616.  *  Dump the information block associated with a given unit (TID)
  2617.  */
  2618.  
  2619. static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
  2620. {
  2621. char buf[64];
  2622. int unit = d->lct_data.tid;
  2623. printk(KERN_INFO "TID %3.3d ", unit);
  2624. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
  2625. {
  2626. buf[16]=0;
  2627. printk(" Vendor: %-12.12s", buf);
  2628. }
  2629. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
  2630. {
  2631. buf[16]=0;
  2632. printk(" Device: %-12.12s", buf);
  2633. }
  2634. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
  2635. {
  2636. buf[8]=0;
  2637. printk(" Rev: %-12.12sn", buf);
  2638. }
  2639. #ifdef DEBUG
  2640.  printk(KERN_INFO "tClass: %.21sn", adpt_i2o_get_class_name(d->lct_data.class_id));
  2641.  printk(KERN_INFO "tSubclass: 0x%04Xn", d->lct_data.sub_class);
  2642.  printk(KERN_INFO "tFlags: ");
  2643.  if(d->lct_data.device_flags&(1<<0))
  2644.   printk("C");      // ConfigDialog requested
  2645.  if(d->lct_data.device_flags&(1<<1))
  2646.   printk("U");      // Multi-user capable
  2647.  if(!(d->lct_data.device_flags&(1<<4)))
  2648.   printk("P");      // Peer service enabled!
  2649.  if(!(d->lct_data.device_flags&(1<<5)))
  2650.   printk("M");      // Mgmt service enabled!
  2651.  printk("n");
  2652. #endif
  2653. }
  2654. #ifdef DEBUG
  2655. /*
  2656.  * Do i2o class name lookup
  2657.  */
  2658. static const char *adpt_i2o_get_class_name(int class)
  2659. {
  2660. int idx = 16;
  2661. static char *i2o_class_name[] = {
  2662. "Executive",
  2663. "Device Driver Module",
  2664. "Block Device",
  2665. "Tape Device",
  2666. "LAN Interface",
  2667. "WAN Interface",
  2668. "Fibre Channel Port",
  2669. "Fibre Channel Device",
  2670. "SCSI Device",
  2671. "ATE Port",
  2672. "ATE Device",
  2673. "Floppy Controller",
  2674. "Floppy Device",
  2675. "Secondary Bus Port",
  2676. "Peer Transport Agent",
  2677. "Peer Transport",
  2678. "Unknown"
  2679. };
  2680. switch(class&0xFFF) {
  2681. case I2O_CLASS_EXECUTIVE:
  2682. idx = 0; break;
  2683. case I2O_CLASS_DDM:
  2684. idx = 1; break;
  2685. case I2O_CLASS_RANDOM_BLOCK_STORAGE:
  2686. idx = 2; break;
  2687. case I2O_CLASS_SEQUENTIAL_STORAGE:
  2688. idx = 3; break;
  2689. case I2O_CLASS_LAN:
  2690. idx = 4; break;
  2691. case I2O_CLASS_WAN:
  2692. idx = 5; break;
  2693. case I2O_CLASS_FIBRE_CHANNEL_PORT:
  2694. idx = 6; break;
  2695. case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
  2696. idx = 7; break;
  2697. case I2O_CLASS_SCSI_PERIPHERAL:
  2698. idx = 8; break;
  2699. case I2O_CLASS_ATE_PORT:
  2700. idx = 9; break;
  2701. case I2O_CLASS_ATE_PERIPHERAL:
  2702. idx = 10; break;
  2703. case I2O_CLASS_FLOPPY_CONTROLLER:
  2704. idx = 11; break;
  2705. case I2O_CLASS_FLOPPY_DEVICE:
  2706. idx = 12; break;
  2707. case I2O_CLASS_BUS_ADAPTER_PORT:
  2708. idx = 13; break;
  2709. case I2O_CLASS_PEER_TRANSPORT_AGENT:
  2710. idx = 14; break;
  2711. case I2O_CLASS_PEER_TRANSPORT:
  2712. idx = 15; break;
  2713. }
  2714. return i2o_class_name[idx];
  2715. }
  2716. #endif
  2717. static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
  2718. {
  2719. u32 msg[6];
  2720. int ret, size = sizeof(i2o_hrt);
  2721. do {
  2722. if (pHba->hrt == NULL) {
  2723. pHba->hrt=kmalloc(size, GFP_KERNEL|ADDR32);
  2724. if (pHba->hrt == NULL) {
  2725. printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.n", pHba->name);
  2726. return -ENOMEM;
  2727. }
  2728. }
  2729. msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
  2730. msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
  2731. msg[2]= 0;
  2732. msg[3]= 0;
  2733. msg[4]= (0xD0000000 | size);    /* Simple transaction */
  2734. msg[5]= virt_to_bus(pHba->hrt);   /* Dump it here */
  2735. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
  2736. printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)n", pHba->name, ret);
  2737. return ret;
  2738. }
  2739. if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
  2740. size = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
  2741. kfree(pHba->hrt);
  2742. pHba->hrt = NULL;
  2743. }
  2744. } while(pHba->hrt == NULL);
  2745. return 0;
  2746. }                                                                                                                                       
  2747. /*
  2748.  *  Query one scalar group value or a whole scalar group.
  2749.  */     
  2750. static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid, 
  2751. int group, int field, void *buf, int buflen)
  2752. {
  2753. u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
  2754. u8  resblk[8+buflen]; /* 8 bytes for header */
  2755. int size;
  2756. if (field == -1)   /* whole group */
  2757. opblk[4] = -1;
  2758. size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid, 
  2759. opblk, sizeof(opblk), resblk, sizeof(resblk));
  2760. memcpy(buf, resblk+8, buflen);  /* cut off header */
  2761. if (size < 0)
  2762. return size;
  2763. return buflen;
  2764. }
  2765. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  2766.  *
  2767.  * This function can be used for all UtilParamsGet/Set operations.
  2768.  * The OperationBlock is given in opblk-buffer, 
  2769.  * and results are returned in resblk-buffer.
  2770.  * Note that the minimum sized resblk is 8 bytes and contains
  2771.  * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  2772.  */
  2773. static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid, 
  2774.   void *opblk, int oplen, void *resblk, int reslen)
  2775. {
  2776. u32 msg[9]; 
  2777. u32 *res = (u32 *)resblk;
  2778. int wait_status;
  2779. msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
  2780. msg[1] = cmd << 24 | HOST_TID << 12 | tid; 
  2781. msg[2] = 0;
  2782. msg[3] = 0;
  2783. msg[4] = 0;
  2784. msg[5] = 0x54000000 | oplen; /* OperationBlock */
  2785. msg[6] = virt_to_bus(opblk);
  2786. msg[7] = 0xD0000000 | reslen; /* ResultBlock */
  2787. msg[8] = virt_to_bus(resblk);
  2788. if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
  2789.     return wait_status;  /* -DetailedStatus */
  2790. }
  2791. if (res[1]&0x00FF0000) {  /* BlockStatus != SUCCESS */
  2792. printk(KERN_WARNING "%s: %s - Error:n  ErrorInfoSize = 0x%02x, "
  2793. "BlockStatus = 0x%02x, BlockSize = 0x%04xn",
  2794. pHba->name,
  2795. (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
  2796.  : "PARAMS_GET",   
  2797. res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
  2798. return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
  2799. }
  2800.  return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */ 
  2801. }
  2802. static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
  2803. {
  2804. u32 msg[4];
  2805. int ret;
  2806. adpt_i2o_status_get(pHba);
  2807. /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
  2808. if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
  2809.        (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
  2810. return 0;
  2811. }
  2812. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  2813. msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
  2814. msg[2] = 0;
  2815. msg[3] = 0;
  2816. if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  2817. printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).n",
  2818. pHba->unit, -ret);
  2819. } else {
  2820. printk(KERN_INFO"dpti%d: Quiesced.n",pHba->unit);
  2821. }
  2822. adpt_i2o_status_get(pHba);
  2823. return ret;
  2824. }
  2825. /* 
  2826.  * Enable IOP. Allows the IOP to resume external operations.
  2827.  */
  2828. static int adpt_i2o_enable_hba(adpt_hba* pHba)
  2829. {
  2830. u32 msg[4];
  2831. int ret;
  2832. adpt_i2o_status_get(pHba);
  2833. if(!pHba->status_block){
  2834. return -ENOMEM;
  2835. }
  2836. /* Enable only allowed on READY state */
  2837. if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
  2838. return 0;
  2839. if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
  2840. return -EINVAL;
  2841. msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  2842. msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
  2843. msg[2]= 0;
  2844. msg[3]= 0;
  2845. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  2846. printk(KERN_WARNING"%s: Could not enable (status=%#10x).n", 
  2847. pHba->name, ret);
  2848. } else {
  2849. PDEBUG("%s: Enabled.n", pHba->name);
  2850. }
  2851. adpt_i2o_status_get(pHba);
  2852. return ret;
  2853. }
  2854. static int adpt_i2o_systab_send(adpt_hba* pHba)
  2855. {
  2856.  u32 msg[12];
  2857.  int ret;
  2858. msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
  2859. msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
  2860. msg[2] = 0;
  2861. msg[3] = 0;
  2862. msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
  2863. msg[5] = 0;    /* Segment 0 */
  2864. /* 
  2865.  * Provide three SGL-elements:
  2866.  * System table (SysTab), Private memory space declaration and 
  2867.  * Private i/o space declaration  
  2868.  */
  2869. msg[6] = 0x54000000 | sys_tbl_len;
  2870. msg[7] = virt_to_phys(sys_tbl);
  2871. msg[8] = 0x54000000 | 0;
  2872. msg[9] = 0;
  2873. msg[10] = 0xD4000000 | 0;
  2874. msg[11] = 0;
  2875. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
  2876. printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).n", 
  2877. pHba->name, ret);
  2878. }
  2879. #ifdef DEBUG
  2880. else {
  2881. PINFO("%s: SysTab set.n", pHba->name);
  2882. }
  2883. #endif
  2884. return ret;
  2885.  }
  2886. /*============================================================================
  2887.  *
  2888.  *============================================================================
  2889.  */
  2890. #ifdef UARTDELAY 
  2891. static static void adpt_delay(int millisec)
  2892. {
  2893. int i;
  2894. for (i = 0; i < millisec; i++) {
  2895. udelay(1000); /* delay for one millisecond */
  2896. }
  2897. }
  2898. #endif
  2899. static Scsi_Host_Template driver_template = DPT_I2O;
  2900. #include "scsi_module.c"
  2901. EXPORT_NO_SYMBOLS;
  2902. MODULE_LICENSE("GPL");