i2o_scsi.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:21k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* 
  2.  *  This program is free software; you can redistribute it and/or modify it
  3.  *  under the terms of the GNU General Public License as published by the
  4.  *  Free Software Foundation; either version 2, or (at your option) any
  5.  *  later version.
  6.  *
  7.  *  This program is distributed in the hope that it will be useful, but
  8.  *  WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10.  *  General Public License for more details.
  11.  *
  12.  *  Complications for I2O scsi
  13.  *
  14.  * o Each (bus,lun) is a logical device in I2O. We keep a map
  15.  * table. We spoof failed selection for unmapped units
  16.  * o Request sense buffers can come back for free. 
  17.  * o Scatter gather is a bit dynamic. We have to investigate at
  18.  * setup time.
  19.  * o Some of our resources are dynamically shared. The i2o core
  20.  * needs a message reservation protocol to avoid swap v net
  21.  * deadlocking. We need to back off queue requests.
  22.  *
  23.  * In general the firmware wants to help. Where its help isn't performance
  24.  * useful we just ignore the aid. Its not worth the code in truth.
  25.  *
  26.  * Fixes:
  27.  * Steve Ralston : Scatter gather now works
  28.  *
  29.  * To Do
  30.  * 64bit cleanups
  31.  * Fix the resource management problems.
  32.  */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/string.h>
  37. #include <linux/ioport.h>
  38. #include <linux/sched.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/timer.h>
  41. #include <linux/delay.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/prefetch.h>
  44. #include <asm/dma.h>
  45. #include <asm/system.h>
  46. #include <asm/io.h>
  47. #include <asm/atomic.h>
  48. #include <linux/blk.h>
  49. #include <linux/version.h>
  50. #include <linux/i2o.h>
  51. #include "../../scsi/scsi.h"
  52. #include "../../scsi/hosts.h"
  53. #include "../../scsi/sd.h"
  54. #include "i2o_scsi.h"
  55. #define VERSION_STRING        "Version 0.0.1"
  56. #define dprintk(x)
  57. #define MAXHOSTS 32
  58. struct i2o_scsi_host
  59. {
  60. struct i2o_controller *controller;
  61. s16 task[16][8]; /* Allow 16 devices for now */
  62. unsigned long tagclock[16][8]; /* Tag clock for queueing */
  63. s16 bus_task; /* The adapter TID */
  64. };
  65. static int scsi_context;
  66. static int lun_done;
  67. static int i2o_scsi_hosts;
  68. static u32 *retry[32];
  69. static struct i2o_controller *retry_ctrl[32];
  70. static struct timer_list retry_timer;
  71. static int retry_ct = 0;
  72. static atomic_t queue_depth;
  73. /*
  74.  * SG Chain buffer support...
  75.  */
  76. #define SG_MAX_FRAGS 64
  77. /*
  78.  * FIXME: we should allocate one of these per bus we find as we
  79.  * locate them not in a lump at boot.
  80.  */
  81.  
  82. typedef struct _chain_buf
  83. {
  84. u32 sg_flags_cnt[SG_MAX_FRAGS];
  85. u32 sg_buf[SG_MAX_FRAGS];
  86. } chain_buf;
  87. #define SG_CHAIN_BUF_SZ sizeof(chain_buf)
  88. #define SG_MAX_BUFS (i2o_num_controllers * I2O_SCSI_CAN_QUEUE)
  89. #define SG_CHAIN_POOL_SZ (SG_MAX_BUFS * SG_CHAIN_BUF_SZ)
  90. static int max_sg_len = 0;
  91. static chain_buf *sg_chain_pool = NULL;
  92. static int sg_chain_tag = 0;
  93. static int sg_max_frags = SG_MAX_FRAGS;
  94. /*
  95.  * Retry congested frames. This actually needs pushing down into
  96.  * i2o core. We should only bother the OSM with this when we can't
  97.  * queue and retry the frame. Or perhaps we should call the OSM
  98.  * and its default handler should be this in the core, and this
  99.  * call a 2nd "I give up" handler in the OSM ?
  100.  */
  101.  
  102. static void i2o_retry_run(unsigned long f)
  103. {
  104. int i;
  105. unsigned long flags;
  106. save_flags(flags);
  107. cli();
  108. for(i=0;i<retry_ct;i++)
  109. i2o_post_message(retry_ctrl[i], virt_to_bus(retry[i]));
  110. retry_ct=0;
  111. restore_flags(flags);
  112. }
  113. static void flush_pending(void)
  114. {
  115. int i;
  116. unsigned long flags;
  117. save_flags(flags);
  118. cli();
  119. for(i=0;i<retry_ct;i++)
  120. {
  121. retry[i][0]&=~0xFFFFFF;
  122. retry[i][0]|=I2O_CMD_UTIL_NOP<<24;
  123. i2o_post_message(retry_ctrl[i],virt_to_bus(retry[i]));
  124. }
  125. retry_ct=0;
  126. restore_flags(flags);
  127. }
  128. static void i2o_scsi_reply(struct i2o_handler *h, struct i2o_controller *c, struct i2o_message *msg)
  129. {
  130. Scsi_Cmnd *current_command;
  131. u32 *m = (u32 *)msg;
  132. u8 as,ds,st;
  133. spin_lock_prefetch(&io_request_lock);
  134. if(m[0] & (1<<13))
  135. {
  136. printk("IOP fail.n");
  137. printk("From %d To %d Cmd %d.n",
  138. (m[1]>>12)&0xFFF,
  139. m[1]&0xFFF,
  140. m[1]>>24);
  141. printk("Failure Code %d.n", m[4]>>24);
  142. if(m[4]&(1<<16))
  143. printk("Format error.n");
  144. if(m[4]&(1<<17))
  145. printk("Path error.n");
  146. if(m[4]&(1<<18))
  147. printk("Path State.n");
  148. if(m[4]&(1<<18))
  149. printk("Congestion.n");
  150. m=(u32 *)bus_to_virt(m[7]);
  151. printk("Failing message is %p.n", m);
  152. if((m[4]&(1<<18)) && retry_ct < 32)
  153. {
  154. retry_ctrl[retry_ct]=c;
  155. retry[retry_ct]=m;
  156. if(!retry_ct++)
  157. {
  158. retry_timer.expires=jiffies+1;
  159. add_timer(&retry_timer);
  160. }
  161. }
  162. else
  163. {
  164. /* Create a scsi error for this */
  165. current_command = (Scsi_Cmnd *)m[3];
  166. printk("Aborted %ldn", current_command->serial_number);
  167. spin_lock_irq(&io_request_lock);
  168. current_command->result = DID_ERROR << 16;
  169. current_command->scsi_done(current_command);
  170. spin_unlock_irq(&io_request_lock);
  171. /* Now flush the message by making it a NOP */
  172. m[0]&=0x00FFFFFF;
  173. m[0]|=(I2O_CMD_UTIL_NOP)<<24;
  174. i2o_post_message(c,virt_to_bus(m));
  175. }
  176. return;
  177. }
  178. prefetchw(&queue_depth);
  179. /*
  180.  * Low byte is device status, next is adapter status,
  181.  * (then one byte reserved), then request status.
  182.  */
  183. ds=(u8)m[4]; 
  184. as=(u8)(m[4]>>8);
  185. st=(u8)(m[4]>>24);
  186. dprintk(("i2o got a scsi reply %08X: ", m[0]));
  187. dprintk(("m[2]=%08X: ", m[2]));
  188. dprintk(("m[4]=%08Xn", m[4]));
  189. if(m[2]&0x80000000)
  190. {
  191. if(m[2]&0x40000000)
  192. {
  193. dprintk(("Event.n"));
  194. lun_done=1;
  195. return;
  196. }
  197. printk(KERN_ERR "i2o_scsi: bus reset reply.n");
  198. return;
  199. }
  200. current_command = (Scsi_Cmnd *)m[3];
  201. /*
  202.  * Is this a control request coming back - eg an abort ?
  203.  */
  204.  
  205. if(current_command==NULL)
  206. {
  207. if(st)
  208. dprintk(("SCSI abort: %08X", m[4]));
  209. dprintk(("SCSI abort completed.n"));
  210. return;
  211. }
  212. dprintk(("Completed %ldn", current_command->serial_number));
  213. atomic_dec(&queue_depth);
  214. if(st == 0x06)
  215. {
  216. if(m[5] < current_command->underflow)
  217. {
  218. int i;
  219. printk(KERN_ERR "SCSI: underflow 0x%08X 0x%08Xn",
  220. m[5], current_command->underflow);
  221. printk("Cmd: ");
  222. for(i=0;i<15;i++)
  223. printk("%02X ", current_command->cmnd[i]);
  224. printk(".n");
  225. }
  226. else st=0;
  227. }
  228. if(st)
  229. {
  230. /* An error has occurred */
  231. dprintk((KERN_DEBUG "SCSI error %08X", m[4]));
  232. if (as == 0x0E) 
  233. /* SCSI Reset */
  234. current_command->result = DID_RESET << 16;
  235. else if (as == 0x0F)
  236. current_command->result = DID_PARITY << 16;
  237. else
  238. current_command->result = DID_ERROR << 16;
  239. }
  240. else
  241. /*
  242.  * It worked maybe ?
  243.  */
  244. current_command->result = DID_OK << 16 | ds;
  245. spin_lock(&io_request_lock);
  246. current_command->scsi_done(current_command);
  247. spin_unlock(&io_request_lock);
  248. return;
  249. }
  250. struct i2o_handler i2o_scsi_handler=
  251. {
  252. i2o_scsi_reply,
  253. NULL,
  254. NULL,
  255. NULL,
  256. "I2O SCSI OSM",
  257. 0,
  258. I2O_CLASS_SCSI_PERIPHERAL
  259. };
  260. static int i2o_find_lun(struct i2o_controller *c, struct i2o_device *d, int *target, int *lun)
  261. {
  262. u8 reply[8];
  263. if(i2o_query_scalar(c, d->lct_data.tid, 0, 3, reply, 4)<0)
  264. return -1;
  265. *target=reply[0];
  266. if(i2o_query_scalar(c, d->lct_data.tid, 0, 4, reply, 8)<0)
  267. return -1;
  268. *lun=reply[1];
  269. dprintk(("SCSI (%d,%d)n", *target, *lun));
  270. return 0;
  271. }
  272. void i2o_scsi_init(struct i2o_controller *c, struct i2o_device *d, struct Scsi_Host *shpnt)
  273. {
  274. struct i2o_device *unit;
  275. struct i2o_scsi_host *h =(struct i2o_scsi_host *)shpnt->hostdata;
  276. int lun;
  277. int target;
  278. h->controller=c;
  279. h->bus_task=d->lct_data.tid;
  280. for(target=0;target<16;target++)
  281. for(lun=0;lun<8;lun++)
  282. h->task[target][lun] = -1;
  283. for(unit=c->devices;unit!=NULL;unit=unit->next)
  284. {
  285. dprintk(("Class %03X, parent %d, want %d.n",
  286. unit->lct_data.class_id, unit->lct_data.parent_tid, d->lct_data.tid));
  287. /* Only look at scsi and fc devices */
  288. if (    (unit->lct_data.class_id != I2O_CLASS_SCSI_PERIPHERAL)
  289.      && (unit->lct_data.class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL)
  290.    )
  291. continue;
  292. /* On our bus ? */
  293. dprintk(("Found a disk (%d).n", unit->lct_data.tid));
  294. if ((unit->lct_data.parent_tid == d->lct_data.tid)
  295.      || (unit->lct_data.parent_tid == d->lct_data.parent_tid)
  296.    )
  297. {
  298. u16 limit;
  299. dprintk(("Its ours.n"));
  300. if(i2o_find_lun(c, unit, &target, &lun)==-1)
  301. {
  302. printk(KERN_ERR "i2o_scsi: Unable to get lun for tid %d.n", unit->lct_data.tid);
  303. continue;
  304. }
  305. dprintk(("Found disk %d %d.n", target, lun));
  306. h->task[target][lun]=unit->lct_data.tid;
  307. h->tagclock[target][lun]=jiffies;
  308. /* Get the max fragments/request */
  309. i2o_query_scalar(c, d->lct_data.tid, 0xF103, 3, &limit, 2);
  310. /* sanity */
  311. if ( limit == 0 )
  312. {
  313. printk(KERN_WARNING "i2o_scsi: Ignoring unreasonable SG limit of 0 from IOP!n");
  314. limit = 1;
  315. }
  316. shpnt->sg_tablesize = limit;
  317. dprintk(("i2o_scsi: set scatter-gather to %d.n", 
  318. shpnt->sg_tablesize));
  319. }
  320. }
  321. }
  322. int i2o_scsi_detect(Scsi_Host_Template * tpnt)
  323. {
  324. unsigned long flags;
  325. struct Scsi_Host *shpnt = NULL;
  326. int i;
  327. int count;
  328. printk("i2o_scsi.c: %sn", VERSION_STRING);
  329. if(i2o_install_handler(&i2o_scsi_handler)<0)
  330. {
  331. printk(KERN_ERR "i2o_scsi: Unable to install OSM handler.n");
  332. return 0;
  333. }
  334. scsi_context = i2o_scsi_handler.context;
  335. if((sg_chain_pool = kmalloc(SG_CHAIN_POOL_SZ, GFP_KERNEL)) == NULL)
  336. {
  337. printk("i2o_scsi: Unable to alloc %d byte SG chain buffer pool.n", SG_CHAIN_POOL_SZ);
  338. printk("i2o_scsi: SG chaining DISABLED!n");
  339. sg_max_frags = 11;
  340. }
  341. else
  342. {
  343. printk("  chain_pool: %d bytes @ %pn", SG_CHAIN_POOL_SZ, sg_chain_pool);
  344. printk("  (%d byte buffers X %d can_queue X %d i2o controllers)n",
  345. SG_CHAIN_BUF_SZ, I2O_SCSI_CAN_QUEUE, i2o_num_controllers);
  346. sg_max_frags = SG_MAX_FRAGS;    // 64
  347. }
  348. init_timer(&retry_timer);
  349. retry_timer.data = 0UL;
  350. retry_timer.function = i2o_retry_run;
  351. // printk("SCSI OSM at %d.n", scsi_context);
  352. for (count = 0, i = 0; i < MAX_I2O_CONTROLLERS; i++)
  353. {
  354. struct i2o_controller *c=i2o_find_controller(i);
  355. struct i2o_device *d;
  356. /*
  357.  * This controller doesn't exist.
  358.  */
  359. if(c==NULL)
  360. continue;
  361. /*
  362.  * Fixme - we need some altered device locking. This
  363.  * is racing with device addition in theory. Easy to fix.
  364.  */
  365. for(d=c->devices;d!=NULL;d=d->next)
  366. {
  367. /*
  368.  * bus_adapter, SCSI (obsolete), or FibreChannel busses only
  369.  */
  370. if(    (d->lct_data.class_id!=I2O_CLASS_BUS_ADAPTER_PORT) // bus_adapter
  371. //     && (d->lct_data.class_id!=I2O_CLASS_FIBRE_CHANNEL_PORT) // FC_PORT
  372.   )
  373. continue;
  374. shpnt = scsi_register(tpnt, sizeof(struct i2o_scsi_host));
  375. if(shpnt==NULL)
  376. continue;
  377. save_flags(flags);
  378. cli();
  379. shpnt->unique_id = (u32)d;
  380. shpnt->io_port = 0;
  381. shpnt->n_io_port = 0;
  382. shpnt->irq = 0;
  383. shpnt->this_id = /* Good question */15;
  384. restore_flags(flags);
  385. i2o_scsi_init(c, d, shpnt);
  386. count++;
  387. }
  388. }
  389. i2o_scsi_hosts = count;
  390. if(count==0)
  391. {
  392. if(sg_chain_pool!=NULL)
  393. {
  394. kfree(sg_chain_pool);
  395. sg_chain_pool = NULL;
  396. }
  397. flush_pending();
  398. del_timer(&retry_timer);
  399. i2o_remove_handler(&i2o_scsi_handler);
  400. }
  401. return count;
  402. }
  403. int i2o_scsi_release(struct Scsi_Host *host)
  404. {
  405. if(--i2o_scsi_hosts==0)
  406. {
  407. if(sg_chain_pool!=NULL)
  408. {
  409. kfree(sg_chain_pool);
  410. sg_chain_pool = NULL;
  411. }
  412. flush_pending();
  413. del_timer(&retry_timer);
  414. i2o_remove_handler(&i2o_scsi_handler);
  415. }
  416. return 0;
  417. }
  418. const char *i2o_scsi_info(struct Scsi_Host *SChost)
  419. {
  420. struct i2o_scsi_host *hostdata;
  421. hostdata = (struct i2o_scsi_host *)SChost->hostdata;
  422. return(&hostdata->controller->name[0]);
  423. }
  424. /*
  425.  * From the wd93 driver:
  426.  * Returns true if there will be a DATA_OUT phase with this command, 
  427.  * false otherwise.
  428.  * (Thanks to Joerg Dorchain for the research and suggestion.)
  429.  *
  430.  */
  431. static int is_dir_out(Scsi_Cmnd *cmd)
  432. {
  433. switch (cmd->cmnd[0]) 
  434. {
  435.       case WRITE_6:           case WRITE_10:          case WRITE_12:
  436.        case WRITE_LONG:        case WRITE_SAME:        case WRITE_BUFFER:
  437.        case WRITE_VERIFY:      case WRITE_VERIFY_12:      
  438.        case COMPARE:           case COPY:              case COPY_VERIFY:
  439.        case SEARCH_EQUAL:      case SEARCH_HIGH:       case SEARCH_LOW:
  440.        case SEARCH_EQUAL_12:   case SEARCH_HIGH_12:    case SEARCH_LOW_12:      
  441.        case FORMAT_UNIT:       case REASSIGN_BLOCKS:   case RESERVE:
  442.        case MODE_SELECT:       case MODE_SELECT_10:    case LOG_SELECT:
  443.        case SEND_DIAGNOSTIC:   case CHANGE_DEFINITION: case UPDATE_BLOCK:
  444.        case SET_WINDOW:        case MEDIUM_SCAN:       case SEND_VOLUME_TAG:
  445.        case 0xea:
  446.          return 1;
  447. default:
  448.          return 0;
  449. }
  450. }
  451. int i2o_scsi_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
  452. {
  453. int i;
  454. int tid;
  455. struct i2o_controller *c;
  456. Scsi_Cmnd *current_command;
  457. struct Scsi_Host *host;
  458. struct i2o_scsi_host *hostdata;
  459. u32 *msg, *mptr;
  460. u32 m;
  461. u32 *lenptr;
  462. int direction;
  463. int scsidir;
  464. u32 len;
  465. u32 reqlen;
  466. u32 tag;
  467. static int max_qd = 1;
  468. /*
  469.  * Do the incoming paperwork
  470.  */
  471.  
  472. host = SCpnt->host;
  473. hostdata = (struct i2o_scsi_host *)host->hostdata;
  474.  
  475. c = hostdata->controller;
  476. prefetch(c);
  477. prefetchw(&queue_depth);
  478. SCpnt->scsi_done = done;
  479. if(SCpnt->target > 15)
  480. {
  481. printk(KERN_ERR "i2o_scsi: Wild target %d.n", SCpnt->target);
  482. return -1;
  483. }
  484. tid = hostdata->task[SCpnt->target][SCpnt->lun];
  485. dprintk(("qcmd: Tid = %dn", tid));
  486. current_command = SCpnt; /* set current command                */
  487. current_command->scsi_done = done; /* set ptr to done function           */
  488. /* We don't have such a device. Pretend we did the command 
  489.    and that selection timed out */
  490. if(tid == -1)
  491. {
  492. SCpnt->result = DID_NO_CONNECT << 16;
  493. done(SCpnt);
  494. return 0;
  495. }
  496. dprintk(("Real scsi messages.n"));
  497. /*
  498.  * Obtain an I2O message. Right now we _have_ to obtain one
  499.  * until the scsi layer stuff is cleaned up.
  500.  */
  501.  
  502. do
  503. {
  504. mb();
  505. m = I2O_POST_READ32(c);
  506. }
  507. while(m==0xFFFFFFFF);
  508. msg = (u32 *)(c->mem_offset + m);
  509. /*
  510.  * Put together a scsi execscb message
  511.  */
  512. len = SCpnt->request_bufflen;
  513. direction = 0x00000000; // SGL IN  (osm<--iop)
  514. /*
  515.  * The scsi layer should be handling this stuff
  516.  */
  517. scsidir = 0x00000000; // DATA NO XFER
  518. if(len)
  519. {
  520. if(is_dir_out(SCpnt))
  521. {
  522. direction=0x04000000; // SGL OUT  (osm-->iop)
  523. scsidir  =0x80000000; // DATA OUT (iop-->dev)
  524. }
  525. else
  526. {
  527. scsidir  =0x40000000; // DATA IN  (iop<--dev)
  528. }
  529. }
  530. __raw_writel(I2O_CMD_SCSI_EXEC<<24|HOST_TID<<12|tid, &msg[1]);
  531. __raw_writel(scsi_context, &msg[2]); /* So the I2O layer passes to us */
  532. /* Sorry 64bit folks. FIXME */
  533. __raw_writel((u32)SCpnt, &msg[3]); /* We want the SCSI control block back */
  534. /* LSI_920_PCI_QUIRK
  535.  *
  536.  * Intermittant observations of msg frame word data corruption
  537.  * observed on msg[4] after:
  538.  *   WRITE, READ-MODIFY-WRITE
  539.  * operations.  19990606 -sralston
  540.  *
  541.  * (Hence we build this word via tag. Its good practice anyway
  542.  *  we don't want fetches over PCI needlessly)
  543.  */
  544. tag=0;
  545. /*
  546.  * Attach tags to the devices
  547.  */
  548. if(SCpnt->device->tagged_supported)
  549. {
  550. /*
  551.  * Some drives are too stupid to handle fairness issues
  552.  * with tagged queueing. We throw in the odd ordered
  553.  * tag to stop them starving themselves.
  554.  */
  555. if((jiffies - hostdata->tagclock[SCpnt->target][SCpnt->lun]) > (5*HZ))
  556. {
  557. tag=0x01800000; /* ORDERED! */
  558. hostdata->tagclock[SCpnt->target][SCpnt->lun]=jiffies;
  559. }
  560. else
  561. {
  562. /* Hmmm...  I always see value of 0 here,
  563.  *  of which {HEAD_OF, ORDERED, SIMPLE} are NOT!  -sralston
  564.  */
  565. if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
  566. tag=0x01000000;
  567. else if(SCpnt->tag == ORDERED_QUEUE_TAG)
  568. tag=0x01800000;
  569. }
  570. }
  571. /* Direction, disconnect ok, tag, CDBLen */
  572. __raw_writel(scsidir|0x20000000|SCpnt->cmd_len|tag, &msg[4]);
  573. mptr=msg+5;
  574. /* 
  575.  * Write SCSI command into the message - always 16 byte block 
  576.  */
  577.  
  578. memcpy_toio(mptr, SCpnt->cmnd, 16);
  579. mptr+=4;
  580. lenptr=mptr++; /* Remember me - fill in when we know */
  581. reqlen = 12; // SINGLE SGE
  582. /*
  583.  * Now fill in the SGList and command 
  584.  *
  585.  * FIXME: we need to set the sglist limits according to the 
  586.  * message size of the I2O controller. We might only have room
  587.  * for 6 or so worst case
  588.  */
  589. if(SCpnt->use_sg)
  590. {
  591. struct scatterlist *sg = (struct scatterlist *)SCpnt->request_buffer;
  592. int chain = 0;
  593. len = 0;
  594. if((sg_max_frags > 11) && (SCpnt->use_sg > 11))
  595. {
  596. chain = 1;
  597. /*
  598.  * Need to chain!
  599.  */
  600. __raw_writel(direction|0xB0000000|(SCpnt->use_sg*2*4), mptr++);
  601. __raw_writel(virt_to_bus(sg_chain_pool + sg_chain_tag), mptr);
  602. mptr = (u32*)(sg_chain_pool + sg_chain_tag);
  603. if (SCpnt->use_sg > max_sg_len)
  604. {
  605. max_sg_len = SCpnt->use_sg;
  606. printk("i2o_scsi: Chain SG! SCpnt=%p, SG_FragCnt=%d, SG_idx=%dn",
  607. SCpnt, SCpnt->use_sg, sg_chain_tag);
  608. }
  609. if ( ++sg_chain_tag == SG_MAX_BUFS )
  610. sg_chain_tag = 0;
  611. for(i = 0 ; i < SCpnt->use_sg; i++)
  612. {
  613. *mptr++=direction|0x10000000|sg->length;
  614. len+=sg->length;
  615. *mptr++=virt_to_bus(sg->address);
  616. sg++;
  617. }
  618. mptr[-2]=direction|0xD0000000|(sg-1)->length;
  619. }
  620. else
  621. {
  622. for(i = 0 ; i < SCpnt->use_sg; i++)
  623. {
  624. __raw_writel(direction|0x10000000|sg->length, mptr++);
  625. len+=sg->length;
  626. __raw_writel(virt_to_bus(sg->address), mptr++);
  627. sg++;
  628. }
  629. /* Make this an end of list. Again evade the 920 bug and
  630.    unwanted PCI read traffic */
  631. __raw_writel(direction|0xD0000000|(sg-1)->length, &mptr[-2]);
  632. }
  633. if(!chain)
  634. reqlen = mptr - msg;
  635. __raw_writel(len, lenptr);
  636. if(len != SCpnt->underflow)
  637. printk("Cmd len %08X Cmd underflow %08Xn",
  638. len, SCpnt->underflow);
  639. }
  640. else
  641. {
  642. dprintk(("non sg for %p, %dn", SCpnt->request_buffer,
  643. SCpnt->request_bufflen));
  644. __raw_writel(len = SCpnt->request_bufflen, lenptr);
  645. if(len == 0)
  646. {
  647. reqlen = 9;
  648. }
  649. else
  650. {
  651. __raw_writel(0xD0000000|direction|SCpnt->request_bufflen, mptr++);
  652. __raw_writel(virt_to_bus(SCpnt->request_buffer), mptr++);
  653. }
  654. }
  655. /*
  656.  * Stick the headers on 
  657.  */
  658. __raw_writel(reqlen<<16 | SGL_OFFSET_10, msg);
  659. /* Queue the message */
  660. i2o_post_message(c,m);
  661. atomic_inc(&queue_depth);
  662. if(atomic_read(&queue_depth)> max_qd)
  663. {
  664. max_qd=atomic_read(&queue_depth);
  665. printk("Queue depth now %d.n", max_qd);
  666. }
  667. mb();
  668. dprintk(("Issued %ldn", current_command->serial_number));
  669. return 0;
  670. }
  671. static void internal_done(Scsi_Cmnd * SCpnt)
  672. {
  673. SCpnt->SCp.Status++;
  674. }
  675. int i2o_scsi_command(Scsi_Cmnd * SCpnt)
  676. {
  677. i2o_scsi_queuecommand(SCpnt, internal_done);
  678. SCpnt->SCp.Status = 0;
  679. while (!SCpnt->SCp.Status)
  680. barrier();
  681. return SCpnt->result;
  682. }
  683. int i2o_scsi_abort(Scsi_Cmnd * SCpnt)
  684. {
  685. struct i2o_controller *c;
  686. struct Scsi_Host *host;
  687. struct i2o_scsi_host *hostdata;
  688. u32 *msg;
  689. u32 m;
  690. int tid;
  691. printk("i2o_scsi: Aborting command block.n");
  692. host = SCpnt->host;
  693. hostdata = (struct i2o_scsi_host *)host->hostdata;
  694. tid = hostdata->task[SCpnt->target][SCpnt->lun];
  695. if(tid==-1)
  696. {
  697. printk(KERN_ERR "impossible command to abort.n");
  698. return SCSI_ABORT_NOT_RUNNING;
  699. }
  700. c = hostdata->controller;
  701. /*
  702.  * Obtain an I2O message. Right now we _have_ to obtain one
  703.  * until the scsi layer stuff is cleaned up.
  704.  */
  705.  
  706. do
  707. {
  708. mb();
  709. m = I2O_POST_READ32(c);
  710. }
  711. while(m==0xFFFFFFFF);
  712. msg = (u32 *)(c->mem_offset + m);
  713. __raw_writel(FIVE_WORD_MSG_SIZE, &msg[0]);
  714. __raw_writel(I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|tid, &msg[1]);
  715. __raw_writel(scsi_context, &msg[2]);
  716. __raw_writel(0, &msg[3]); /* Not needed for an abort */
  717. __raw_writel((u32)SCpnt, &msg[4]);
  718. wmb();
  719. i2o_post_message(c,m);
  720. wmb();
  721. return SCSI_ABORT_PENDING;
  722. }
  723. int i2o_scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
  724. {
  725. int tid;
  726. struct i2o_controller *c;
  727. struct Scsi_Host *host;
  728. struct i2o_scsi_host *hostdata;
  729. u32 m;
  730. u32 *msg;
  731. /*
  732.  * Find the TID for the bus
  733.  */
  734. printk("i2o_scsi: Attempting to reset the bus.n");
  735. host = SCpnt->host;
  736. hostdata = (struct i2o_scsi_host *)host->hostdata;
  737. tid = hostdata->bus_task;
  738. c = hostdata->controller;
  739. /*
  740.  * Now send a SCSI reset request. Any remaining commands
  741.  * will be aborted by the IOP. We need to catch the reply
  742.  * possibly ?
  743.  */
  744.  
  745. m = I2O_POST_READ32(c);
  746. /*
  747.  * No free messages, try again next time - no big deal
  748.  */
  749.  
  750. if(m == 0xFFFFFFFF)
  751. return SCSI_RESET_PUNT;
  752. msg = (u32 *)(c->mem_offset + m);
  753. __raw_writel(FOUR_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
  754. __raw_writel(I2O_CMD_SCSI_BUSRESET<<24|HOST_TID<<12|tid, &msg[1]);
  755. __raw_writel(scsi_context|0x80000000, &msg[2]);
  756. /* We use the top bit to split controller and unit transactions */
  757. /* Now store unit,tid so we can tie the completion back to a specific device */
  758. __raw_writel(c->unit << 16 | tid, &msg[3]);
  759. wmb();
  760. i2o_post_message(c,m);
  761. return SCSI_RESET_PENDING;
  762. }
  763. /*
  764.  * This is anyones guess quite frankly.
  765.  */
  766.  
  767. int i2o_scsi_bios_param(Disk * disk, kdev_t dev, int *ip)
  768. {
  769. int size;
  770. size = disk->capacity;
  771. ip[0] = 64; /* heads                        */
  772. ip[1] = 32; /* sectors                      */
  773. if ((ip[2] = size >> 11) > 1024) { /* cylinders, test for big disk */
  774. ip[0] = 255; /* heads                        */
  775. ip[1] = 63; /* sectors                      */
  776. ip[2] = size / (255 * 63); /* cylinders                    */
  777. }
  778. return 0;
  779. }
  780. MODULE_AUTHOR("Red Hat Software");
  781. MODULE_LICENSE("GPL");
  782. static Scsi_Host_Template driver_template = I2OSCSI;
  783. #include "../../scsi/scsi_module.c"