i2o_scsi.c 21 KB

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