i2o_scsi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. * For the avoidance of doubt the "preferred form" of this code is one which
  13. * is in an open non patent encumbered format. Where cryptographic key signing
  14. * forms part of the process of creating an executable the information
  15. * including keys needed to generate an equivalently functional executable
  16. * are deemed to be part of the source code.
  17. *
  18. * Complications for I2O scsi
  19. *
  20. * o Each (bus,lun) is a logical device in I2O. We keep a map
  21. * table. We spoof failed selection for unmapped units
  22. * o Request sense buffers can come back for free.
  23. * o Scatter gather is a bit dynamic. We have to investigate at
  24. * setup time.
  25. * o Some of our resources are dynamically shared. The i2o core
  26. * needs a message reservation protocol to avoid swap v net
  27. * deadlocking. We need to back off queue requests.
  28. *
  29. * In general the firmware wants to help. Where its help isn't performance
  30. * useful we just ignore the aid. Its not worth the code in truth.
  31. *
  32. * Fixes/additions:
  33. * Steve Ralston:
  34. * Scatter gather now works
  35. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  36. * Minor fixes for 2.6.
  37. *
  38. * To Do:
  39. * 64bit cleanups
  40. * Fix the resource management problems.
  41. */
  42. #define DEBUG 1
  43. #include <linux/module.h>
  44. #include <linux/kernel.h>
  45. #include <linux/types.h>
  46. #include <linux/string.h>
  47. #include <linux/ioport.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/timer.h>
  51. #include <linux/delay.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/prefetch.h>
  54. #include <linux/pci.h>
  55. #include <linux/blkdev.h>
  56. #include <linux/i2o.h>
  57. #include <asm/dma.h>
  58. #include <asm/system.h>
  59. #include <asm/io.h>
  60. #include <asm/atomic.h>
  61. #include <scsi/scsi.h>
  62. #include <scsi/scsi_host.h>
  63. #include <scsi/scsi_device.h>
  64. #include <scsi/scsi_cmnd.h>
  65. #define OSM_NAME "scsi-osm"
  66. #define OSM_VERSION "$Rev$"
  67. #define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
  68. static struct i2o_driver i2o_scsi_driver;
  69. static int i2o_scsi_max_id = 16;
  70. static int i2o_scsi_max_lun = 8;
  71. struct i2o_scsi_host {
  72. struct Scsi_Host *scsi_host; /* pointer to the SCSI host */
  73. struct i2o_controller *iop; /* pointer to the I2O controller */
  74. struct i2o_device *channel[0]; /* channel->i2o_dev mapping table */
  75. };
  76. static struct scsi_host_template i2o_scsi_host_template;
  77. #define I2O_SCSI_CAN_QUEUE 4
  78. /* SCSI OSM class handling definition */
  79. static struct i2o_class_id i2o_scsi_class_id[] = {
  80. {I2O_CLASS_SCSI_PERIPHERAL},
  81. {I2O_CLASS_END}
  82. };
  83. static struct i2o_scsi_host *i2o_scsi_host_alloc(struct i2o_controller *c)
  84. {
  85. struct i2o_scsi_host *i2o_shost;
  86. struct i2o_device *i2o_dev;
  87. struct Scsi_Host *scsi_host;
  88. int max_channel = 0;
  89. u8 type;
  90. int i;
  91. size_t size;
  92. i2o_status_block *sb;
  93. list_for_each_entry(i2o_dev, &c->devices, list)
  94. if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
  95. if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
  96. && (type == 0x01)) /* SCSI bus */
  97. max_channel++;
  98. }
  99. if (!max_channel) {
  100. osm_warn("no channels found on %s\n", c->name);
  101. return ERR_PTR(-EFAULT);
  102. }
  103. size = max_channel * sizeof(struct i2o_device *)
  104. + sizeof(struct i2o_scsi_host);
  105. scsi_host = scsi_host_alloc(&i2o_scsi_host_template, size);
  106. if (!scsi_host) {
  107. osm_warn("Could not allocate SCSI host\n");
  108. return ERR_PTR(-ENOMEM);
  109. }
  110. scsi_host->max_channel = max_channel - 1;
  111. scsi_host->max_id = i2o_scsi_max_id;
  112. scsi_host->max_lun = i2o_scsi_max_lun;
  113. scsi_host->this_id = c->unit;
  114. sb = c->status_block.virt;
  115. scsi_host->sg_tablesize = (sb->inbound_frame_size -
  116. sizeof(struct i2o_message) / 4 - 6) / 2;
  117. i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
  118. i2o_shost->scsi_host = scsi_host;
  119. i2o_shost->iop = c;
  120. i = 0;
  121. list_for_each_entry(i2o_dev, &c->devices, list)
  122. if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
  123. if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1)) /* only SCSI bus */
  124. i2o_shost->channel[i++] = i2o_dev;
  125. if (i >= max_channel)
  126. break;
  127. }
  128. return i2o_shost;
  129. };
  130. /**
  131. * i2o_scsi_get_host - Get an I2O SCSI host
  132. * @c: I2O controller to for which to get the SCSI host
  133. *
  134. * If the I2O controller already exists as SCSI host, the SCSI host
  135. * is returned, otherwise the I2O controller is added to the SCSI
  136. * core.
  137. *
  138. * Returns pointer to the I2O SCSI host on success or NULL on failure.
  139. */
  140. static struct i2o_scsi_host *i2o_scsi_get_host(struct i2o_controller *c)
  141. {
  142. return c->driver_data[i2o_scsi_driver.context];
  143. };
  144. /**
  145. * i2o_scsi_remove - Remove I2O device from SCSI core
  146. * @dev: device which should be removed
  147. *
  148. * Removes the I2O device from the SCSI core again.
  149. *
  150. * Returns 0 on success.
  151. */
  152. static int i2o_scsi_remove(struct device *dev)
  153. {
  154. struct i2o_device *i2o_dev = to_i2o_device(dev);
  155. struct i2o_controller *c = i2o_dev->iop;
  156. struct i2o_scsi_host *i2o_shost;
  157. struct scsi_device *scsi_dev;
  158. osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
  159. i2o_shost = i2o_scsi_get_host(c);
  160. shost_for_each_device(scsi_dev, i2o_shost->scsi_host)
  161. if (scsi_dev->hostdata == i2o_dev) {
  162. sysfs_remove_link(&i2o_dev->device.kobj, "scsi");
  163. scsi_remove_device(scsi_dev);
  164. scsi_device_put(scsi_dev);
  165. break;
  166. }
  167. return 0;
  168. };
  169. /**
  170. * i2o_scsi_probe - verify if dev is a I2O SCSI device and install it
  171. * @dev: device to verify if it is a I2O SCSI device
  172. *
  173. * Retrieve channel, id and lun for I2O device. If everthing goes well
  174. * register the I2O device as SCSI device on the I2O SCSI controller.
  175. *
  176. * Returns 0 on success or negative error code on failure.
  177. */
  178. static int i2o_scsi_probe(struct device *dev)
  179. {
  180. struct i2o_device *i2o_dev = to_i2o_device(dev);
  181. struct i2o_controller *c = i2o_dev->iop;
  182. struct i2o_scsi_host *i2o_shost;
  183. struct Scsi_Host *scsi_host;
  184. struct i2o_device *parent;
  185. struct scsi_device *scsi_dev;
  186. u32 id;
  187. u64 lun;
  188. int channel = -1;
  189. int i;
  190. i2o_shost = i2o_scsi_get_host(c);
  191. if (!i2o_shost)
  192. return -EFAULT;
  193. scsi_host = i2o_shost->scsi_host;
  194. if (i2o_parm_field_get(i2o_dev, 0, 3, &id, 4) < 0)
  195. return -EFAULT;
  196. if (id >= scsi_host->max_id) {
  197. osm_warn("SCSI device id (%d) >= max_id of I2O host (%d)", id,
  198. scsi_host->max_id);
  199. return -EFAULT;
  200. }
  201. if (i2o_parm_field_get(i2o_dev, 0, 4, &lun, 8) < 0)
  202. return -EFAULT;
  203. if (lun >= scsi_host->max_lun) {
  204. osm_warn("SCSI device id (%d) >= max_lun of I2O host (%d)",
  205. (unsigned int)lun, scsi_host->max_lun);
  206. return -EFAULT;
  207. }
  208. parent = i2o_iop_find_device(c, i2o_dev->lct_data.parent_tid);
  209. if (!parent) {
  210. osm_warn("can not find parent of device %03x\n",
  211. i2o_dev->lct_data.tid);
  212. return -EFAULT;
  213. }
  214. for (i = 0; i <= i2o_shost->scsi_host->max_channel; i++)
  215. if (i2o_shost->channel[i] == parent)
  216. channel = i;
  217. if (channel == -1) {
  218. osm_warn("can not find channel of device %03x\n",
  219. i2o_dev->lct_data.tid);
  220. return -EFAULT;
  221. }
  222. scsi_dev =
  223. __scsi_add_device(i2o_shost->scsi_host, channel, id, lun, i2o_dev);
  224. if (IS_ERR(scsi_dev)) {
  225. osm_warn("can not add SCSI device %03x\n",
  226. i2o_dev->lct_data.tid);
  227. return PTR_ERR(scsi_dev);
  228. }
  229. sysfs_create_link(&i2o_dev->device.kobj, &scsi_dev->sdev_gendev.kobj, "scsi");
  230. osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %d\n",
  231. i2o_dev->lct_data.tid, channel, id, (unsigned int)lun);
  232. return 0;
  233. };
  234. static const char *i2o_scsi_info(struct Scsi_Host *SChost)
  235. {
  236. struct i2o_scsi_host *hostdata;
  237. hostdata = (struct i2o_scsi_host *)SChost->hostdata;
  238. return hostdata->iop->name;
  239. }
  240. /**
  241. * i2o_scsi_reply - SCSI OSM message reply handler
  242. * @c: controller issuing the reply
  243. * @m: message id for flushing
  244. * @msg: the message from the controller
  245. *
  246. * Process reply messages (interrupts in normal scsi controller think).
  247. * We can get a variety of messages to process. The normal path is
  248. * scsi command completions. We must also deal with IOP failures,
  249. * the reply to a bus reset and the reply to a LUN query.
  250. *
  251. * Returns 0 on success and if the reply should not be flushed or > 0
  252. * on success and if the reply should be flushed. Returns negative error
  253. * code on failure and if the reply should be flushed.
  254. */
  255. static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
  256. struct i2o_message *msg)
  257. {
  258. struct scsi_cmnd *cmd;
  259. struct device *dev;
  260. u8 as, ds, st;
  261. cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  262. if (msg->u.head[0] & (1 << 13)) {
  263. struct i2o_message __iomem *pmsg; /* preserved message */
  264. u32 pm;
  265. int err = DID_ERROR;
  266. pm = le32_to_cpu(msg->body[3]);
  267. pmsg = i2o_msg_in_to_virt(c, pm);
  268. osm_err("IOP fail.\n");
  269. osm_err("From %d To %d Cmd %d.\n",
  270. (msg->u.head[1] >> 12) & 0xFFF,
  271. msg->u.head[1] & 0xFFF, msg->u.head[1] >> 24);
  272. osm_err("Failure Code %d.\n", msg->body[0] >> 24);
  273. if (msg->body[0] & (1 << 16))
  274. osm_err("Format error.\n");
  275. if (msg->body[0] & (1 << 17))
  276. osm_err("Path error.\n");
  277. if (msg->body[0] & (1 << 18))
  278. osm_err("Path State.\n");
  279. if (msg->body[0] & (1 << 18))
  280. {
  281. osm_err("Congestion.\n");
  282. err = DID_BUS_BUSY;
  283. }
  284. osm_debug("Failing message is %p.\n", pmsg);
  285. cmd = i2o_cntxt_list_get(c, readl(&pmsg->u.s.tcntxt));
  286. if (!cmd)
  287. return 1;
  288. cmd->result = err << 16;
  289. cmd->scsi_done(cmd);
  290. /* Now flush the message by making it a NOP */
  291. i2o_msg_nop(c, pm);
  292. return 1;
  293. }
  294. /*
  295. * Low byte is device status, next is adapter status,
  296. * (then one byte reserved), then request status.
  297. */
  298. ds = (u8) le32_to_cpu(msg->body[0]);
  299. as = (u8) (le32_to_cpu(msg->body[0]) >> 8);
  300. st = (u8) (le32_to_cpu(msg->body[0]) >> 24);
  301. /*
  302. * Is this a control request coming back - eg an abort ?
  303. */
  304. if (!cmd) {
  305. if (st)
  306. osm_warn("SCSI abort: %08X", le32_to_cpu(msg->body[0]));
  307. osm_info("SCSI abort completed.\n");
  308. return -EFAULT;
  309. }
  310. osm_debug("Completed %ld\n", cmd->serial_number);
  311. if (st) {
  312. u32 count, error;
  313. /* An error has occurred */
  314. switch (st) {
  315. case 0x06:
  316. count = le32_to_cpu(msg->body[1]);
  317. if (count < cmd->underflow) {
  318. int i;
  319. osm_err("SCSI underflow 0x%08X 0x%08X\n", count,
  320. cmd->underflow);
  321. osm_debug("Cmd: ");
  322. for (i = 0; i < 15; i++)
  323. pr_debug("%02X ", cmd->cmnd[i]);
  324. pr_debug(".\n");
  325. cmd->result = (DID_ERROR << 16);
  326. }
  327. break;
  328. default:
  329. error = le32_to_cpu(msg->body[0]);
  330. osm_err("SCSI error %08x\n", error);
  331. if ((error & 0xff) == 0x02 /*CHECK_CONDITION */ ) {
  332. int i;
  333. u32 len = sizeof(cmd->sense_buffer);
  334. len = (len > 40) ? 40 : len;
  335. // Copy over the sense data
  336. memcpy(cmd->sense_buffer, (void *)&msg->body[3],
  337. len);
  338. for (i = 0; i <= len; i++)
  339. osm_info("%02x\n",
  340. cmd->sense_buffer[i]);
  341. if (cmd->sense_buffer[0] == 0x70
  342. && cmd->sense_buffer[2] == DATA_PROTECT) {
  343. /* This is to handle an array failed */
  344. cmd->result = (DID_TIME_OUT << 16);
  345. printk(KERN_WARNING "%s: SCSI Data "
  346. "Protect-Device (%d,%d,%d) "
  347. "hba_status=0x%x, dev_status="
  348. "0x%x, cmd=0x%x\n", c->name,
  349. (u32) cmd->device->channel,
  350. (u32) cmd->device->id,
  351. (u32) cmd->device->lun,
  352. (error >> 8) & 0xff,
  353. error & 0xff, cmd->cmnd[0]);
  354. } else
  355. cmd->result = (DID_ERROR << 16);
  356. break;
  357. }
  358. switch (as) {
  359. case 0x0E:
  360. /* SCSI Reset */
  361. cmd->result = DID_RESET << 16;
  362. break;
  363. case 0x0F:
  364. cmd->result = DID_PARITY << 16;
  365. break;
  366. default:
  367. cmd->result = DID_ERROR << 16;
  368. break;
  369. }
  370. break;
  371. }
  372. cmd->scsi_done(cmd);
  373. return 1;
  374. }
  375. cmd->result = DID_OK << 16 | ds;
  376. dev = &c->pdev->dev;
  377. if (cmd->use_sg)
  378. dma_unmap_sg(dev, (struct scatterlist *)cmd->buffer,
  379. cmd->use_sg, cmd->sc_data_direction);
  380. else if (cmd->request_bufflen)
  381. dma_unmap_single(dev, (dma_addr_t) ((long)cmd->SCp.ptr),
  382. cmd->request_bufflen, cmd->sc_data_direction);
  383. cmd->scsi_done(cmd);
  384. return 1;
  385. };
  386. /**
  387. * i2o_scsi_notify_controller_add - Retrieve notifications of added
  388. * controllers
  389. * @c: the controller which was added
  390. *
  391. * If a I2O controller is added, we catch the notification to add a
  392. * corresponding Scsi_Host.
  393. */
  394. static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
  395. {
  396. struct i2o_scsi_host *i2o_shost;
  397. int rc;
  398. i2o_shost = i2o_scsi_host_alloc(c);
  399. if (IS_ERR(i2o_shost)) {
  400. osm_err("Could not initialize SCSI host\n");
  401. return;
  402. }
  403. rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
  404. if (rc) {
  405. osm_err("Could not add SCSI host\n");
  406. scsi_host_put(i2o_shost->scsi_host);
  407. return;
  408. }
  409. c->driver_data[i2o_scsi_driver.context] = i2o_shost;
  410. osm_debug("new I2O SCSI host added\n");
  411. };
  412. /**
  413. * i2o_scsi_notify_controller_remove - Retrieve notifications of removed
  414. * controllers
  415. * @c: the controller which was removed
  416. *
  417. * If a I2O controller is removed, we catch the notification to remove the
  418. * corresponding Scsi_Host.
  419. */
  420. static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
  421. {
  422. struct i2o_scsi_host *i2o_shost;
  423. i2o_shost = i2o_scsi_get_host(c);
  424. if (!i2o_shost)
  425. return;
  426. c->driver_data[i2o_scsi_driver.context] = NULL;
  427. scsi_remove_host(i2o_shost->scsi_host);
  428. scsi_host_put(i2o_shost->scsi_host);
  429. osm_debug("I2O SCSI host removed\n");
  430. };
  431. /* SCSI OSM driver struct */
  432. static struct i2o_driver i2o_scsi_driver = {
  433. .name = OSM_NAME,
  434. .reply = i2o_scsi_reply,
  435. .classes = i2o_scsi_class_id,
  436. .notify_controller_add = i2o_scsi_notify_controller_add,
  437. .notify_controller_remove = i2o_scsi_notify_controller_remove,
  438. .driver = {
  439. .probe = i2o_scsi_probe,
  440. .remove = i2o_scsi_remove,
  441. },
  442. };
  443. /**
  444. * i2o_scsi_queuecommand - queue a SCSI command
  445. * @SCpnt: scsi command pointer
  446. * @done: callback for completion
  447. *
  448. * Issue a scsi command asynchronously. Return 0 on success or 1 if
  449. * we hit an error (normally message queue congestion). The only
  450. * minor complication here is that I2O deals with the device addressing
  451. * so we have to map the bus/dev/lun back to an I2O handle as well
  452. * as faking absent devices ourself.
  453. *
  454. * Locks: takes the controller lock on error path only
  455. */
  456. static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
  457. void (*done) (struct scsi_cmnd *))
  458. {
  459. struct i2o_controller *c;
  460. struct Scsi_Host *host;
  461. struct i2o_device *i2o_dev;
  462. struct device *dev;
  463. int tid;
  464. struct i2o_message __iomem *msg;
  465. u32 m;
  466. /*
  467. * ENABLE_DISCONNECT
  468. * SIMPLE_TAG
  469. * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
  470. */
  471. u32 scsi_flags = 0x20a00000;
  472. u32 sg_flags;
  473. u32 __iomem *mptr;
  474. u32 __iomem *lenptr;
  475. u32 len;
  476. int i;
  477. /*
  478. * Do the incoming paperwork
  479. */
  480. i2o_dev = SCpnt->device->hostdata;
  481. host = SCpnt->device->host;
  482. c = i2o_dev->iop;
  483. dev = &c->pdev->dev;
  484. SCpnt->scsi_done = done;
  485. if (unlikely(!i2o_dev)) {
  486. osm_warn("no I2O device in request\n");
  487. SCpnt->result = DID_NO_CONNECT << 16;
  488. done(SCpnt);
  489. return 0;
  490. }
  491. tid = i2o_dev->lct_data.tid;
  492. osm_debug("qcmd: Tid = %03x\n", tid);
  493. osm_debug("Real scsi messages.\n");
  494. /*
  495. * Obtain an I2O message. If there are none free then
  496. * throw it back to the scsi layer
  497. */
  498. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  499. if (m == I2O_QUEUE_EMPTY)
  500. return SCSI_MLQUEUE_HOST_BUSY;
  501. mptr = &msg->body[0];
  502. /*
  503. * Put together a scsi execscb message
  504. */
  505. switch (SCpnt->sc_data_direction) {
  506. case PCI_DMA_NONE:
  507. /* DATA NO XFER */
  508. sg_flags = 0x00000000;
  509. break;
  510. case PCI_DMA_TODEVICE:
  511. /* DATA OUT (iop-->dev) */
  512. scsi_flags |= 0x80000000;
  513. sg_flags = 0x14000000;
  514. break;
  515. case PCI_DMA_FROMDEVICE:
  516. /* DATA IN (iop<--dev) */
  517. scsi_flags |= 0x40000000;
  518. sg_flags = 0x10000000;
  519. break;
  520. default:
  521. /* Unknown - kill the command */
  522. SCpnt->result = DID_NO_CONNECT << 16;
  523. done(SCpnt);
  524. return 0;
  525. }
  526. writel(I2O_CMD_SCSI_EXEC << 24 | HOST_TID << 12 | tid, &msg->u.head[1]);
  527. writel(i2o_scsi_driver.context, &msg->u.s.icntxt);
  528. /* We want the SCSI control block back */
  529. writel(i2o_cntxt_list_add(c, SCpnt), &msg->u.s.tcntxt);
  530. /* LSI_920_PCI_QUIRK
  531. *
  532. * Intermittant observations of msg frame word data corruption
  533. * observed on msg[4] after:
  534. * WRITE, READ-MODIFY-WRITE
  535. * operations. 19990606 -sralston
  536. *
  537. * (Hence we build this word via tag. Its good practice anyway
  538. * we don't want fetches over PCI needlessly)
  539. */
  540. /* Attach tags to the devices */
  541. /*
  542. if(SCpnt->device->tagged_supported) {
  543. if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
  544. scsi_flags |= 0x01000000;
  545. else if(SCpnt->tag == ORDERED_QUEUE_TAG)
  546. scsi_flags |= 0x01800000;
  547. }
  548. */
  549. writel(scsi_flags | SCpnt->cmd_len, mptr++);
  550. /* Write SCSI command into the message - always 16 byte block */
  551. memcpy_toio(mptr, SCpnt->cmnd, 16);
  552. mptr += 4;
  553. lenptr = mptr++; /* Remember me - fill in when we know */
  554. /* Now fill in the SGList and command */
  555. if (SCpnt->use_sg) {
  556. struct scatterlist *sg;
  557. int sg_count;
  558. sg = SCpnt->request_buffer;
  559. len = 0;
  560. sg_count = dma_map_sg(dev, sg, SCpnt->use_sg,
  561. SCpnt->sc_data_direction);
  562. if (unlikely(sg_count <= 0))
  563. return -ENOMEM;
  564. for (i = SCpnt->use_sg; i > 0; i--) {
  565. if (i == 1)
  566. sg_flags |= 0xC0000000;
  567. writel(sg_flags | sg_dma_len(sg), mptr++);
  568. writel(sg_dma_address(sg), mptr++);
  569. len += sg_dma_len(sg);
  570. sg++;
  571. }
  572. writel(len, lenptr);
  573. } else {
  574. len = SCpnt->request_bufflen;
  575. writel(len, lenptr);
  576. if (len > 0) {
  577. dma_addr_t dma_addr;
  578. dma_addr = dma_map_single(dev, SCpnt->request_buffer,
  579. SCpnt->request_bufflen,
  580. SCpnt->sc_data_direction);
  581. if (!dma_addr)
  582. return -ENOMEM;
  583. SCpnt->SCp.ptr = (void *)(unsigned long)dma_addr;
  584. sg_flags |= 0xC0000000;
  585. writel(sg_flags | SCpnt->request_bufflen, mptr++);
  586. writel(dma_addr, mptr++);
  587. }
  588. }
  589. /* Stick the headers on */
  590. writel((mptr - &msg->u.head[0]) << 16 | SGL_OFFSET_10, &msg->u.head[0]);
  591. /* Queue the message */
  592. i2o_msg_post(c, m);
  593. osm_debug("Issued %ld\n", SCpnt->serial_number);
  594. return 0;
  595. };
  596. /**
  597. * i2o_scsi_abort - abort a running command
  598. * @SCpnt: command to abort
  599. *
  600. * Ask the I2O controller to abort a command. This is an asynchrnous
  601. * process and our callback handler will see the command complete with an
  602. * aborted message if it succeeds.
  603. *
  604. * Returns 0 if the command is successfully aborted or negative error code
  605. * on failure.
  606. */
  607. static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
  608. {
  609. struct i2o_device *i2o_dev;
  610. struct i2o_controller *c;
  611. struct i2o_message __iomem *msg;
  612. u32 m;
  613. int tid;
  614. int status = FAILED;
  615. osm_warn("Aborting command block.\n");
  616. i2o_dev = SCpnt->device->hostdata;
  617. c = i2o_dev->iop;
  618. tid = i2o_dev->lct_data.tid;
  619. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  620. if (m == I2O_QUEUE_EMPTY)
  621. return SCSI_MLQUEUE_HOST_BUSY;
  622. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  623. writel(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid,
  624. &msg->u.head[1]);
  625. writel(i2o_cntxt_list_get_ptr(c, SCpnt), &msg->body[0]);
  626. if (i2o_msg_post_wait(c, m, I2O_TIMEOUT_SCSI_SCB_ABORT))
  627. status = SUCCESS;
  628. return status;
  629. }
  630. /**
  631. * i2o_scsi_bios_param - Invent disk geometry
  632. * @sdev: scsi device
  633. * @dev: block layer device
  634. * @capacity: size in sectors
  635. * @ip: geometry array
  636. *
  637. * This is anyones guess quite frankly. We use the same rules everyone
  638. * else appears to and hope. It seems to work.
  639. */
  640. static int i2o_scsi_bios_param(struct scsi_device *sdev,
  641. struct block_device *dev, sector_t capacity,
  642. int *ip)
  643. {
  644. int size;
  645. size = capacity;
  646. ip[0] = 64; /* heads */
  647. ip[1] = 32; /* sectors */
  648. if ((ip[2] = size >> 11) > 1024) { /* cylinders, test for big disk */
  649. ip[0] = 255; /* heads */
  650. ip[1] = 63; /* sectors */
  651. ip[2] = size / (255 * 63); /* cylinders */
  652. }
  653. return 0;
  654. }
  655. static struct scsi_host_template i2o_scsi_host_template = {
  656. .proc_name = OSM_NAME,
  657. .name = OSM_DESCRIPTION,
  658. .info = i2o_scsi_info,
  659. .queuecommand = i2o_scsi_queuecommand,
  660. .eh_abort_handler = i2o_scsi_abort,
  661. .bios_param = i2o_scsi_bios_param,
  662. .can_queue = I2O_SCSI_CAN_QUEUE,
  663. .sg_tablesize = 8,
  664. .cmd_per_lun = 6,
  665. .use_clustering = ENABLE_CLUSTERING,
  666. };
  667. /**
  668. * i2o_scsi_init - SCSI OSM initialization function
  669. *
  670. * Register SCSI OSM into I2O core.
  671. *
  672. * Returns 0 on success or negative error code on failure.
  673. */
  674. static int __init i2o_scsi_init(void)
  675. {
  676. int rc;
  677. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  678. /* Register SCSI OSM into I2O core */
  679. rc = i2o_driver_register(&i2o_scsi_driver);
  680. if (rc) {
  681. osm_err("Could not register SCSI driver\n");
  682. return rc;
  683. }
  684. return 0;
  685. };
  686. /**
  687. * i2o_scsi_exit - SCSI OSM exit function
  688. *
  689. * Unregisters SCSI OSM from I2O core.
  690. */
  691. static void __exit i2o_scsi_exit(void)
  692. {
  693. /* Unregister I2O SCSI OSM from I2O core */
  694. i2o_driver_unregister(&i2o_scsi_driver);
  695. };
  696. MODULE_AUTHOR("Red Hat Software");
  697. MODULE_LICENSE("GPL");
  698. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  699. MODULE_VERSION(OSM_VERSION);
  700. module_init(i2o_scsi_init);
  701. module_exit(i2o_scsi_exit);