PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/aic7xxx/aic79xx_osm.c

http://github.com/mirrors/linux
C | 2877 lines | 2076 code | 328 blank | 473 comment | 403 complexity | c54d73ed5603ab3e4e9a16716720a790 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * Adaptec AIC79xx device driver for Linux.
  3. *
  4. * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#171 $
  5. *
  6. * --------------------------------------------------------------------------
  7. * Copyright (c) 1994-2000 Justin T. Gibbs.
  8. * Copyright (c) 1997-1999 Doug Ledford
  9. * Copyright (c) 2000-2003 Adaptec Inc.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions, and the following disclaimer,
  17. * without modification.
  18. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  19. * substantially similar to the "NO WARRANTY" disclaimer below
  20. * ("Disclaimer") and any redistribution must be conditioned upon
  21. * including a substantially similar Disclaimer requirement for further
  22. * binary redistribution.
  23. * 3. Neither the names of the above-listed copyright holders nor the names
  24. * of any contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * Alternatively, this software may be distributed under the terms of the
  28. * GNU General Public License ("GPL") version 2 as published by the Free
  29. * Software Foundation.
  30. *
  31. * NO WARRANTY
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  35. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  36. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  41. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. * POSSIBILITY OF SUCH DAMAGES.
  43. */
  44. #include "aic79xx_osm.h"
  45. #include "aic79xx_inline.h"
  46. #include <scsi/scsicam.h>
  47. static struct scsi_transport_template *ahd_linux_transport_template = NULL;
  48. #include <linux/init.h> /* __setup */
  49. #include <linux/mm.h> /* For fetching system memory size */
  50. #include <linux/blkdev.h> /* For block_size() */
  51. #include <linux/delay.h> /* For ssleep/msleep */
  52. #include <linux/device.h>
  53. #include <linux/slab.h>
  54. /*
  55. * Bucket size for counting good commands in between bad ones.
  56. */
  57. #define AHD_LINUX_ERR_THRESH 1000
  58. /*
  59. * Set this to the delay in seconds after SCSI bus reset.
  60. * Note, we honor this only for the initial bus reset.
  61. * The scsi error recovery code performs its own bus settle
  62. * delay handling for error recovery actions.
  63. */
  64. #ifdef CONFIG_AIC79XX_RESET_DELAY_MS
  65. #define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
  66. #else
  67. #define AIC79XX_RESET_DELAY 5000
  68. #endif
  69. /*
  70. * To change the default number of tagged transactions allowed per-device,
  71. * add a line to the lilo.conf file like:
  72. * append="aic79xx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
  73. * which will result in the first four devices on the first two
  74. * controllers being set to a tagged queue depth of 32.
  75. *
  76. * The tag_commands is an array of 16 to allow for wide and twin adapters.
  77. * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
  78. * for channel 1.
  79. */
  80. typedef struct {
  81. uint16_t tag_commands[16]; /* Allow for wide/twin adapters. */
  82. } adapter_tag_info_t;
  83. /*
  84. * Modify this as you see fit for your system.
  85. *
  86. * 0 tagged queuing disabled
  87. * 1 <= n <= 253 n == max tags ever dispatched.
  88. *
  89. * The driver will throttle the number of commands dispatched to a
  90. * device if it returns queue full. For devices with a fixed maximum
  91. * queue depth, the driver will eventually determine this depth and
  92. * lock it in (a console message is printed to indicate that a lock
  93. * has occurred). On some devices, queue full is returned for a temporary
  94. * resource shortage. These devices will return queue full at varying
  95. * depths. The driver will throttle back when the queue fulls occur and
  96. * attempt to slowly increase the depth over time as the device recovers
  97. * from the resource shortage.
  98. *
  99. * In this example, the first line will disable tagged queueing for all
  100. * the devices on the first probed aic79xx adapter.
  101. *
  102. * The second line enables tagged queueing with 4 commands/LUN for IDs
  103. * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
  104. * driver to attempt to use up to 64 tags for ID 1.
  105. *
  106. * The third line is the same as the first line.
  107. *
  108. * The fourth line disables tagged queueing for devices 0 and 3. It
  109. * enables tagged queueing for the other IDs, with 16 commands/LUN
  110. * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
  111. * IDs 2, 5-7, and 9-15.
  112. */
  113. /*
  114. * NOTE: The below structure is for reference only, the actual structure
  115. * to modify in order to change things is just below this comment block.
  116. adapter_tag_info_t aic79xx_tag_info[] =
  117. {
  118. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  119. {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
  120. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  121. {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
  122. };
  123. */
  124. #ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
  125. #define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
  126. #else
  127. #define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
  128. #endif
  129. #define AIC79XX_CONFIGED_TAG_COMMANDS { \
  130. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  131. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  132. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  133. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  134. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  135. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  136. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
  137. AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE \
  138. }
  139. /*
  140. * By default, use the number of commands specified by
  141. * the users kernel configuration.
  142. */
  143. static adapter_tag_info_t aic79xx_tag_info[] =
  144. {
  145. {AIC79XX_CONFIGED_TAG_COMMANDS},
  146. {AIC79XX_CONFIGED_TAG_COMMANDS},
  147. {AIC79XX_CONFIGED_TAG_COMMANDS},
  148. {AIC79XX_CONFIGED_TAG_COMMANDS},
  149. {AIC79XX_CONFIGED_TAG_COMMANDS},
  150. {AIC79XX_CONFIGED_TAG_COMMANDS},
  151. {AIC79XX_CONFIGED_TAG_COMMANDS},
  152. {AIC79XX_CONFIGED_TAG_COMMANDS},
  153. {AIC79XX_CONFIGED_TAG_COMMANDS},
  154. {AIC79XX_CONFIGED_TAG_COMMANDS},
  155. {AIC79XX_CONFIGED_TAG_COMMANDS},
  156. {AIC79XX_CONFIGED_TAG_COMMANDS},
  157. {AIC79XX_CONFIGED_TAG_COMMANDS},
  158. {AIC79XX_CONFIGED_TAG_COMMANDS},
  159. {AIC79XX_CONFIGED_TAG_COMMANDS},
  160. {AIC79XX_CONFIGED_TAG_COMMANDS}
  161. };
  162. /*
  163. * The I/O cell on the chip is very configurable in respect to its analog
  164. * characteristics. Set the defaults here; they can be overriden with
  165. * the proper insmod parameters.
  166. */
  167. struct ahd_linux_iocell_opts
  168. {
  169. uint8_t precomp;
  170. uint8_t slewrate;
  171. uint8_t amplitude;
  172. };
  173. #define AIC79XX_DEFAULT_PRECOMP 0xFF
  174. #define AIC79XX_DEFAULT_SLEWRATE 0xFF
  175. #define AIC79XX_DEFAULT_AMPLITUDE 0xFF
  176. #define AIC79XX_DEFAULT_IOOPTS \
  177. { \
  178. AIC79XX_DEFAULT_PRECOMP, \
  179. AIC79XX_DEFAULT_SLEWRATE, \
  180. AIC79XX_DEFAULT_AMPLITUDE \
  181. }
  182. #define AIC79XX_PRECOMP_INDEX 0
  183. #define AIC79XX_SLEWRATE_INDEX 1
  184. #define AIC79XX_AMPLITUDE_INDEX 2
  185. static const struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
  186. {
  187. AIC79XX_DEFAULT_IOOPTS,
  188. AIC79XX_DEFAULT_IOOPTS,
  189. AIC79XX_DEFAULT_IOOPTS,
  190. AIC79XX_DEFAULT_IOOPTS,
  191. AIC79XX_DEFAULT_IOOPTS,
  192. AIC79XX_DEFAULT_IOOPTS,
  193. AIC79XX_DEFAULT_IOOPTS,
  194. AIC79XX_DEFAULT_IOOPTS,
  195. AIC79XX_DEFAULT_IOOPTS,
  196. AIC79XX_DEFAULT_IOOPTS,
  197. AIC79XX_DEFAULT_IOOPTS,
  198. AIC79XX_DEFAULT_IOOPTS,
  199. AIC79XX_DEFAULT_IOOPTS,
  200. AIC79XX_DEFAULT_IOOPTS,
  201. AIC79XX_DEFAULT_IOOPTS,
  202. AIC79XX_DEFAULT_IOOPTS
  203. };
  204. /*
  205. * There should be a specific return value for this in scsi.h, but
  206. * it seems that most drivers ignore it.
  207. */
  208. #define DID_UNDERFLOW DID_ERROR
  209. void
  210. ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
  211. {
  212. printk("(scsi%d:%c:%d:%d): ",
  213. ahd->platform_data->host->host_no,
  214. scb != NULL ? SCB_GET_CHANNEL(ahd, scb) : 'X',
  215. scb != NULL ? SCB_GET_TARGET(ahd, scb) : -1,
  216. scb != NULL ? SCB_GET_LUN(scb) : -1);
  217. }
  218. /*
  219. * XXX - these options apply unilaterally to _all_ adapters
  220. * cards in the system. This should be fixed. Exceptions to this
  221. * rule are noted in the comments.
  222. */
  223. /*
  224. * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
  225. * has no effect on any later resets that might occur due to things like
  226. * SCSI bus timeouts.
  227. */
  228. static uint32_t aic79xx_no_reset;
  229. /*
  230. * Should we force EXTENDED translation on a controller.
  231. * 0 == Use whatever is in the SEEPROM or default to off
  232. * 1 == Use whatever is in the SEEPROM or default to on
  233. */
  234. static uint32_t aic79xx_extended;
  235. /*
  236. * PCI bus parity checking of the Adaptec controllers. This is somewhat
  237. * dubious at best. To my knowledge, this option has never actually
  238. * solved a PCI parity problem, but on certain machines with broken PCI
  239. * chipset configurations, it can generate tons of false error messages.
  240. * It's included in the driver for completeness.
  241. * 0 = Shut off PCI parity check
  242. * non-0 = Enable PCI parity check
  243. *
  244. * NOTE: you can't actually pass -1 on the lilo prompt. So, to set this
  245. * variable to -1 you would actually want to simply pass the variable
  246. * name without a number. That will invert the 0 which will result in
  247. * -1.
  248. */
  249. static uint32_t aic79xx_pci_parity = ~0;
  250. /*
  251. * There are lots of broken chipsets in the world. Some of them will
  252. * violate the PCI spec when we issue byte sized memory writes to our
  253. * controller. I/O mapped register access, if allowed by the given
  254. * platform, will work in almost all cases.
  255. */
  256. uint32_t aic79xx_allow_memio = ~0;
  257. /*
  258. * So that we can set how long each device is given as a selection timeout.
  259. * The table of values goes like this:
  260. * 0 - 256ms
  261. * 1 - 128ms
  262. * 2 - 64ms
  263. * 3 - 32ms
  264. * We default to 256ms because some older devices need a longer time
  265. * to respond to initial selection.
  266. */
  267. static uint32_t aic79xx_seltime;
  268. /*
  269. * Certain devices do not perform any aging on commands. Should the
  270. * device be saturated by commands in one portion of the disk, it is
  271. * possible for transactions on far away sectors to never be serviced.
  272. * To handle these devices, we can periodically send an ordered tag to
  273. * force all outstanding transactions to be serviced prior to a new
  274. * transaction.
  275. */
  276. static uint32_t aic79xx_periodic_otag;
  277. /* Some storage boxes are using an LSI chip which has a bug making it
  278. * impossible to use aic79xx Rev B chip in 320 speeds. The following
  279. * storage boxes have been reported to be buggy:
  280. * EonStor 3U 16-Bay: U16U-G3A3
  281. * EonStor 2U 12-Bay: U12U-G3A3
  282. * SentinelRAID: 2500F R5 / R6
  283. * SentinelRAID: 2500F R1
  284. * SentinelRAID: 2500F/1500F
  285. * SentinelRAID: 150F
  286. *
  287. * To get around this LSI bug, you can set your board to 160 mode
  288. * or you can enable the SLOWCRC bit.
  289. */
  290. uint32_t aic79xx_slowcrc;
  291. /*
  292. * Module information and settable options.
  293. */
  294. static char *aic79xx = NULL;
  295. MODULE_AUTHOR("Maintainer: Hannes Reinecke <hare@suse.de>");
  296. MODULE_DESCRIPTION("Adaptec AIC790X U320 SCSI Host Bus Adapter driver");
  297. MODULE_LICENSE("Dual BSD/GPL");
  298. MODULE_VERSION(AIC79XX_DRIVER_VERSION);
  299. module_param(aic79xx, charp, 0444);
  300. MODULE_PARM_DESC(aic79xx,
  301. "period-delimited options string:\n"
  302. " verbose Enable verbose/diagnostic logging\n"
  303. " allow_memio Allow device registers to be memory mapped\n"
  304. " debug Bitmask of debug values to enable\n"
  305. " no_reset Suppress initial bus resets\n"
  306. " extended Enable extended geometry on all controllers\n"
  307. " periodic_otag Send an ordered tagged transaction\n"
  308. " periodically to prevent tag starvation.\n"
  309. " This may be required by some older disk\n"
  310. " or drives/RAID arrays.\n"
  311. " tag_info:<tag_str> Set per-target tag depth\n"
  312. " global_tag_depth:<int> Global tag depth for all targets on all buses\n"
  313. " slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
  314. " precomp:<pcomp_list> Set the signal precompensation (0-7).\n"
  315. " amplitude:<int> Set the signal amplitude (0-7).\n"
  316. " seltime:<int> Selection Timeout:\n"
  317. " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
  318. " slowcrc Turn on the SLOWCRC bit (Rev B only)\n"
  319. "\n"
  320. " Sample modprobe configuration file:\n"
  321. " # Enable verbose logging\n"
  322. " # Set tag depth on Controller 2/Target 2 to 10 tags\n"
  323. " # Shorten the selection timeout to 128ms\n"
  324. "\n"
  325. " options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
  326. );
  327. static void ahd_linux_handle_scsi_status(struct ahd_softc *,
  328. struct scsi_device *,
  329. struct scb *);
  330. static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
  331. struct scsi_cmnd *cmd);
  332. static int ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd);
  333. static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
  334. static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
  335. struct ahd_devinfo *devinfo);
  336. static void ahd_linux_device_queue_depth(struct scsi_device *);
  337. static int ahd_linux_run_command(struct ahd_softc*,
  338. struct ahd_linux_device *,
  339. struct scsi_cmnd *);
  340. static void ahd_linux_setup_tag_info_global(char *p);
  341. static int aic79xx_setup(char *c);
  342. static void ahd_freeze_simq(struct ahd_softc *ahd);
  343. static void ahd_release_simq(struct ahd_softc *ahd);
  344. static int ahd_linux_unit;
  345. /************************** OS Utility Wrappers *******************************/
  346. void ahd_delay(long);
  347. void
  348. ahd_delay(long usec)
  349. {
  350. /*
  351. * udelay on Linux can have problems for
  352. * multi-millisecond waits. Wait at most
  353. * 1024us per call.
  354. */
  355. while (usec > 0) {
  356. udelay(usec % 1024);
  357. usec -= 1024;
  358. }
  359. }
  360. /***************************** Low Level I/O **********************************/
  361. uint8_t ahd_inb(struct ahd_softc * ahd, long port);
  362. void ahd_outb(struct ahd_softc * ahd, long port, uint8_t val);
  363. void ahd_outw_atomic(struct ahd_softc * ahd,
  364. long port, uint16_t val);
  365. void ahd_outsb(struct ahd_softc * ahd, long port,
  366. uint8_t *, int count);
  367. void ahd_insb(struct ahd_softc * ahd, long port,
  368. uint8_t *, int count);
  369. uint8_t
  370. ahd_inb(struct ahd_softc * ahd, long port)
  371. {
  372. uint8_t x;
  373. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  374. x = readb(ahd->bshs[0].maddr + port);
  375. } else {
  376. x = inb(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  377. }
  378. mb();
  379. return (x);
  380. }
  381. #if 0 /* unused */
  382. static uint16_t
  383. ahd_inw_atomic(struct ahd_softc * ahd, long port)
  384. {
  385. uint8_t x;
  386. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  387. x = readw(ahd->bshs[0].maddr + port);
  388. } else {
  389. x = inw(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  390. }
  391. mb();
  392. return (x);
  393. }
  394. #endif
  395. void
  396. ahd_outb(struct ahd_softc * ahd, long port, uint8_t val)
  397. {
  398. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  399. writeb(val, ahd->bshs[0].maddr + port);
  400. } else {
  401. outb(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  402. }
  403. mb();
  404. }
  405. void
  406. ahd_outw_atomic(struct ahd_softc * ahd, long port, uint16_t val)
  407. {
  408. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  409. writew(val, ahd->bshs[0].maddr + port);
  410. } else {
  411. outw(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  412. }
  413. mb();
  414. }
  415. void
  416. ahd_outsb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  417. {
  418. int i;
  419. /*
  420. * There is probably a more efficient way to do this on Linux
  421. * but we don't use this for anything speed critical and this
  422. * should work.
  423. */
  424. for (i = 0; i < count; i++)
  425. ahd_outb(ahd, port, *array++);
  426. }
  427. void
  428. ahd_insb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  429. {
  430. int i;
  431. /*
  432. * There is probably a more efficient way to do this on Linux
  433. * but we don't use this for anything speed critical and this
  434. * should work.
  435. */
  436. for (i = 0; i < count; i++)
  437. *array++ = ahd_inb(ahd, port);
  438. }
  439. /******************************* PCI Routines *********************************/
  440. uint32_t
  441. ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
  442. {
  443. switch (width) {
  444. case 1:
  445. {
  446. uint8_t retval;
  447. pci_read_config_byte(pci, reg, &retval);
  448. return (retval);
  449. }
  450. case 2:
  451. {
  452. uint16_t retval;
  453. pci_read_config_word(pci, reg, &retval);
  454. return (retval);
  455. }
  456. case 4:
  457. {
  458. uint32_t retval;
  459. pci_read_config_dword(pci, reg, &retval);
  460. return (retval);
  461. }
  462. default:
  463. panic("ahd_pci_read_config: Read size too big");
  464. /* NOTREACHED */
  465. return (0);
  466. }
  467. }
  468. void
  469. ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
  470. {
  471. switch (width) {
  472. case 1:
  473. pci_write_config_byte(pci, reg, value);
  474. break;
  475. case 2:
  476. pci_write_config_word(pci, reg, value);
  477. break;
  478. case 4:
  479. pci_write_config_dword(pci, reg, value);
  480. break;
  481. default:
  482. panic("ahd_pci_write_config: Write size too big");
  483. /* NOTREACHED */
  484. }
  485. }
  486. /****************************** Inlines ***************************************/
  487. static void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
  488. static void
  489. ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
  490. {
  491. struct scsi_cmnd *cmd;
  492. cmd = scb->io_ctx;
  493. ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
  494. scsi_dma_unmap(cmd);
  495. }
  496. /******************************** Macros **************************************/
  497. #define BUILD_SCSIID(ahd, cmd) \
  498. (((scmd_id(cmd) << TID_SHIFT) & TID) | (ahd)->our_id)
  499. /*
  500. * Return a string describing the driver.
  501. */
  502. static const char *
  503. ahd_linux_info(struct Scsi_Host *host)
  504. {
  505. static char buffer[512];
  506. char ahd_info[256];
  507. char *bp;
  508. struct ahd_softc *ahd;
  509. bp = &buffer[0];
  510. ahd = *(struct ahd_softc **)host->hostdata;
  511. memset(bp, 0, sizeof(buffer));
  512. strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev " AIC79XX_DRIVER_VERSION "\n"
  513. " <");
  514. strcat(bp, ahd->description);
  515. strcat(bp, ">\n"
  516. " ");
  517. ahd_controller_info(ahd, ahd_info);
  518. strcat(bp, ahd_info);
  519. return (bp);
  520. }
  521. /*
  522. * Queue an SCB to the controller.
  523. */
  524. static int
  525. ahd_linux_queue_lck(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
  526. {
  527. struct ahd_softc *ahd;
  528. struct ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
  529. int rtn = SCSI_MLQUEUE_HOST_BUSY;
  530. ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
  531. cmd->scsi_done = scsi_done;
  532. cmd->result = CAM_REQ_INPROG << 16;
  533. rtn = ahd_linux_run_command(ahd, dev, cmd);
  534. return rtn;
  535. }
  536. static DEF_SCSI_QCMD(ahd_linux_queue)
  537. static struct scsi_target **
  538. ahd_linux_target_in_softc(struct scsi_target *starget)
  539. {
  540. struct ahd_softc *ahd =
  541. *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
  542. unsigned int target_offset;
  543. target_offset = starget->id;
  544. if (starget->channel != 0)
  545. target_offset += 8;
  546. return &ahd->platform_data->starget[target_offset];
  547. }
  548. static int
  549. ahd_linux_target_alloc(struct scsi_target *starget)
  550. {
  551. struct ahd_softc *ahd =
  552. *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
  553. struct seeprom_config *sc = ahd->seep_config;
  554. unsigned long flags;
  555. struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
  556. struct ahd_devinfo devinfo;
  557. struct ahd_initiator_tinfo *tinfo;
  558. struct ahd_tmode_tstate *tstate;
  559. char channel = starget->channel + 'A';
  560. ahd_lock(ahd, &flags);
  561. BUG_ON(*ahd_targp != NULL);
  562. *ahd_targp = starget;
  563. if (sc) {
  564. int flags = sc->device_flags[starget->id];
  565. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  566. starget->id, &tstate);
  567. if ((flags & CFPACKETIZED) == 0) {
  568. /* don't negotiate packetized (IU) transfers */
  569. spi_max_iu(starget) = 0;
  570. } else {
  571. if ((ahd->features & AHD_RTI) == 0)
  572. spi_rti(starget) = 0;
  573. }
  574. if ((flags & CFQAS) == 0)
  575. spi_max_qas(starget) = 0;
  576. /* Transinfo values have been set to BIOS settings */
  577. spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
  578. spi_min_period(starget) = tinfo->user.period;
  579. spi_max_offset(starget) = tinfo->user.offset;
  580. }
  581. tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
  582. starget->id, &tstate);
  583. ahd_compile_devinfo(&devinfo, ahd->our_id, starget->id,
  584. CAM_LUN_WILDCARD, channel,
  585. ROLE_INITIATOR);
  586. ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
  587. AHD_TRANS_GOAL, /*paused*/FALSE);
  588. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  589. AHD_TRANS_GOAL, /*paused*/FALSE);
  590. ahd_unlock(ahd, &flags);
  591. return 0;
  592. }
  593. static void
  594. ahd_linux_target_destroy(struct scsi_target *starget)
  595. {
  596. struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
  597. *ahd_targp = NULL;
  598. }
  599. static int
  600. ahd_linux_slave_alloc(struct scsi_device *sdev)
  601. {
  602. struct ahd_softc *ahd =
  603. *((struct ahd_softc **)sdev->host->hostdata);
  604. struct ahd_linux_device *dev;
  605. if (bootverbose)
  606. printk("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);
  607. dev = scsi_transport_device_data(sdev);
  608. memset(dev, 0, sizeof(*dev));
  609. /*
  610. * We start out life using untagged
  611. * transactions of which we allow one.
  612. */
  613. dev->openings = 1;
  614. /*
  615. * Set maxtags to 0. This will be changed if we
  616. * later determine that we are dealing with
  617. * a tagged queuing capable device.
  618. */
  619. dev->maxtags = 0;
  620. return (0);
  621. }
  622. static int
  623. ahd_linux_slave_configure(struct scsi_device *sdev)
  624. {
  625. struct ahd_softc *ahd;
  626. ahd = *((struct ahd_softc **)sdev->host->hostdata);
  627. if (bootverbose)
  628. sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
  629. ahd_linux_device_queue_depth(sdev);
  630. /* Initial Domain Validation */
  631. if (!spi_initial_dv(sdev->sdev_target))
  632. spi_dv_device(sdev);
  633. return 0;
  634. }
  635. #if defined(__i386__)
  636. /*
  637. * Return the disk geometry for the given SCSI device.
  638. */
  639. static int
  640. ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  641. sector_t capacity, int geom[])
  642. {
  643. int heads;
  644. int sectors;
  645. int cylinders;
  646. int extended;
  647. struct ahd_softc *ahd;
  648. ahd = *((struct ahd_softc **)sdev->host->hostdata);
  649. if (scsi_partsize(bdev, capacity, geom))
  650. return 0;
  651. heads = 64;
  652. sectors = 32;
  653. cylinders = aic_sector_div(capacity, heads, sectors);
  654. if (aic79xx_extended != 0)
  655. extended = 1;
  656. else
  657. extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
  658. if (extended && cylinders >= 1024) {
  659. heads = 255;
  660. sectors = 63;
  661. cylinders = aic_sector_div(capacity, heads, sectors);
  662. }
  663. geom[0] = heads;
  664. geom[1] = sectors;
  665. geom[2] = cylinders;
  666. return (0);
  667. }
  668. #endif
  669. /*
  670. * Abort the current SCSI command(s).
  671. */
  672. static int
  673. ahd_linux_abort(struct scsi_cmnd *cmd)
  674. {
  675. int error;
  676. error = ahd_linux_queue_abort_cmd(cmd);
  677. return error;
  678. }
  679. /*
  680. * Attempt to send a target reset message to the device that timed out.
  681. */
  682. static int
  683. ahd_linux_dev_reset(struct scsi_cmnd *cmd)
  684. {
  685. struct ahd_softc *ahd;
  686. struct ahd_linux_device *dev;
  687. struct scb *reset_scb;
  688. u_int cdb_byte;
  689. int retval = SUCCESS;
  690. int paused;
  691. int wait;
  692. struct ahd_initiator_tinfo *tinfo;
  693. struct ahd_tmode_tstate *tstate;
  694. unsigned long flags;
  695. DECLARE_COMPLETION_ONSTACK(done);
  696. reset_scb = NULL;
  697. paused = FALSE;
  698. wait = FALSE;
  699. ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
  700. scmd_printk(KERN_INFO, cmd,
  701. "Attempting to queue a TARGET RESET message:");
  702. printk("CDB:");
  703. for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
  704. printk(" 0x%x", cmd->cmnd[cdb_byte]);
  705. printk("\n");
  706. /*
  707. * Determine if we currently own this command.
  708. */
  709. dev = scsi_transport_device_data(cmd->device);
  710. if (dev == NULL) {
  711. /*
  712. * No target device for this command exists,
  713. * so we must not still own the command.
  714. */
  715. scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
  716. return SUCCESS;
  717. }
  718. /*
  719. * Generate us a new SCB
  720. */
  721. reset_scb = ahd_get_scb(ahd, AHD_NEVER_COL_IDX);
  722. if (!reset_scb) {
  723. scmd_printk(KERN_INFO, cmd, "No SCB available\n");
  724. return FAILED;
  725. }
  726. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  727. cmd->device->id, &tstate);
  728. reset_scb->io_ctx = cmd;
  729. reset_scb->platform_data->dev = dev;
  730. reset_scb->sg_count = 0;
  731. ahd_set_residual(reset_scb, 0);
  732. ahd_set_sense_residual(reset_scb, 0);
  733. reset_scb->platform_data->xfer_len = 0;
  734. reset_scb->hscb->control = 0;
  735. reset_scb->hscb->scsiid = BUILD_SCSIID(ahd,cmd);
  736. reset_scb->hscb->lun = cmd->device->lun;
  737. reset_scb->hscb->cdb_len = 0;
  738. reset_scb->hscb->task_management = SIU_TASKMGMT_LUN_RESET;
  739. reset_scb->flags |= SCB_DEVICE_RESET|SCB_RECOVERY_SCB|SCB_ACTIVE;
  740. if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
  741. reset_scb->flags |= SCB_PACKETIZED;
  742. } else {
  743. reset_scb->hscb->control |= MK_MESSAGE;
  744. }
  745. dev->openings--;
  746. dev->active++;
  747. dev->commands_issued++;
  748. ahd_lock(ahd, &flags);
  749. LIST_INSERT_HEAD(&ahd->pending_scbs, reset_scb, pending_links);
  750. ahd_queue_scb(ahd, reset_scb);
  751. ahd->platform_data->eh_done = &done;
  752. ahd_unlock(ahd, &flags);
  753. printk("%s: Device reset code sleeping\n", ahd_name(ahd));
  754. if (!wait_for_completion_timeout(&done, 5 * HZ)) {
  755. ahd_lock(ahd, &flags);
  756. ahd->platform_data->eh_done = NULL;
  757. ahd_unlock(ahd, &flags);
  758. printk("%s: Device reset timer expired (active %d)\n",
  759. ahd_name(ahd), dev->active);
  760. retval = FAILED;
  761. }
  762. printk("%s: Device reset returning 0x%x\n", ahd_name(ahd), retval);
  763. return (retval);
  764. }
  765. /*
  766. * Reset the SCSI bus.
  767. */
  768. static int
  769. ahd_linux_bus_reset(struct scsi_cmnd *cmd)
  770. {
  771. struct ahd_softc *ahd;
  772. int found;
  773. unsigned long flags;
  774. ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
  775. #ifdef AHD_DEBUG
  776. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  777. printk("%s: Bus reset called for cmd %p\n",
  778. ahd_name(ahd), cmd);
  779. #endif
  780. ahd_lock(ahd, &flags);
  781. found = ahd_reset_channel(ahd, scmd_channel(cmd) + 'A',
  782. /*initiate reset*/TRUE);
  783. ahd_unlock(ahd, &flags);
  784. if (bootverbose)
  785. printk("%s: SCSI bus reset delivered. "
  786. "%d SCBs aborted.\n", ahd_name(ahd), found);
  787. return (SUCCESS);
  788. }
  789. struct scsi_host_template aic79xx_driver_template = {
  790. .module = THIS_MODULE,
  791. .name = "aic79xx",
  792. .proc_name = "aic79xx",
  793. .show_info = ahd_linux_show_info,
  794. .write_info = ahd_proc_write_seeprom,
  795. .info = ahd_linux_info,
  796. .queuecommand = ahd_linux_queue,
  797. .eh_abort_handler = ahd_linux_abort,
  798. .eh_device_reset_handler = ahd_linux_dev_reset,
  799. .eh_bus_reset_handler = ahd_linux_bus_reset,
  800. #if defined(__i386__)
  801. .bios_param = ahd_linux_biosparam,
  802. #endif
  803. .can_queue = AHD_MAX_QUEUE,
  804. .this_id = -1,
  805. .max_sectors = 8192,
  806. .cmd_per_lun = 2,
  807. .slave_alloc = ahd_linux_slave_alloc,
  808. .slave_configure = ahd_linux_slave_configure,
  809. .target_alloc = ahd_linux_target_alloc,
  810. .target_destroy = ahd_linux_target_destroy,
  811. };
  812. /******************************** Bus DMA *************************************/
  813. int
  814. ahd_dma_tag_create(struct ahd_softc *ahd, bus_dma_tag_t parent,
  815. bus_size_t alignment, bus_size_t boundary,
  816. dma_addr_t lowaddr, dma_addr_t highaddr,
  817. bus_dma_filter_t *filter, void *filterarg,
  818. bus_size_t maxsize, int nsegments,
  819. bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
  820. {
  821. bus_dma_tag_t dmat;
  822. dmat = kmalloc(sizeof(*dmat), GFP_ATOMIC);
  823. if (dmat == NULL)
  824. return (ENOMEM);
  825. /*
  826. * Linux is very simplistic about DMA memory. For now don't
  827. * maintain all specification information. Once Linux supplies
  828. * better facilities for doing these operations, or the
  829. * needs of this particular driver change, we might need to do
  830. * more here.
  831. */
  832. dmat->alignment = alignment;
  833. dmat->boundary = boundary;
  834. dmat->maxsize = maxsize;
  835. *ret_tag = dmat;
  836. return (0);
  837. }
  838. void
  839. ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
  840. {
  841. kfree(dmat);
  842. }
  843. int
  844. ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
  845. int flags, bus_dmamap_t *mapp)
  846. {
  847. *vaddr = pci_alloc_consistent(ahd->dev_softc,
  848. dmat->maxsize, mapp);
  849. if (*vaddr == NULL)
  850. return (ENOMEM);
  851. return(0);
  852. }
  853. void
  854. ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
  855. void* vaddr, bus_dmamap_t map)
  856. {
  857. pci_free_consistent(ahd->dev_softc, dmat->maxsize,
  858. vaddr, map);
  859. }
  860. int
  861. ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map,
  862. void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
  863. void *cb_arg, int flags)
  864. {
  865. /*
  866. * Assume for now that this will only be used during
  867. * initialization and not for per-transaction buffer mapping.
  868. */
  869. bus_dma_segment_t stack_sg;
  870. stack_sg.ds_addr = map;
  871. stack_sg.ds_len = dmat->maxsize;
  872. cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
  873. return (0);
  874. }
  875. void
  876. ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
  877. {
  878. }
  879. int
  880. ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
  881. {
  882. /* Nothing to do */
  883. return (0);
  884. }
  885. /********************* Platform Dependent Functions ***************************/
  886. static void
  887. ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
  888. {
  889. if ((instance >= 0)
  890. && (instance < ARRAY_SIZE(aic79xx_iocell_info))) {
  891. uint8_t *iocell_info;
  892. iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
  893. iocell_info[index] = value & 0xFFFF;
  894. if (bootverbose)
  895. printk("iocell[%d:%ld] = %d\n", instance, index, value);
  896. }
  897. }
  898. static void
  899. ahd_linux_setup_tag_info_global(char *p)
  900. {
  901. int tags, i, j;
  902. tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
  903. printk("Setting Global Tags= %d\n", tags);
  904. for (i = 0; i < ARRAY_SIZE(aic79xx_tag_info); i++) {
  905. for (j = 0; j < AHD_NUM_TARGETS; j++) {
  906. aic79xx_tag_info[i].tag_commands[j] = tags;
  907. }
  908. }
  909. }
  910. static void
  911. ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
  912. {
  913. if ((instance >= 0) && (targ >= 0)
  914. && (instance < ARRAY_SIZE(aic79xx_tag_info))
  915. && (targ < AHD_NUM_TARGETS)) {
  916. aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
  917. if (bootverbose)
  918. printk("tag_info[%d:%d] = %d\n", instance, targ, value);
  919. }
  920. }
  921. static char *
  922. ahd_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
  923. void (*callback)(u_long, int, int, int32_t),
  924. u_long callback_arg)
  925. {
  926. char *tok_end;
  927. char *tok_end2;
  928. int i;
  929. int instance;
  930. int targ;
  931. int done;
  932. char tok_list[] = {'.', ',', '{', '}', '\0'};
  933. /* All options use a ':' name/arg separator */
  934. if (*opt_arg != ':')
  935. return (opt_arg);
  936. opt_arg++;
  937. instance = -1;
  938. targ = -1;
  939. done = FALSE;
  940. /*
  941. * Restore separator that may be in
  942. * the middle of our option argument.
  943. */
  944. tok_end = strchr(opt_arg, '\0');
  945. if (tok_end < end)
  946. *tok_end = ',';
  947. while (!done) {
  948. switch (*opt_arg) {
  949. case '{':
  950. if (instance == -1) {
  951. instance = 0;
  952. } else {
  953. if (depth > 1) {
  954. if (targ == -1)
  955. targ = 0;
  956. } else {
  957. printk("Malformed Option %s\n",
  958. opt_name);
  959. done = TRUE;
  960. }
  961. }
  962. opt_arg++;
  963. break;
  964. case '}':
  965. if (targ != -1)
  966. targ = -1;
  967. else if (instance != -1)
  968. instance = -1;
  969. opt_arg++;
  970. break;
  971. case ',':
  972. case '.':
  973. if (instance == -1)
  974. done = TRUE;
  975. else if (targ >= 0)
  976. targ++;
  977. else if (instance >= 0)
  978. instance++;
  979. opt_arg++;
  980. break;
  981. case '\0':
  982. done = TRUE;
  983. break;
  984. default:
  985. tok_end = end;
  986. for (i = 0; tok_list[i]; i++) {
  987. tok_end2 = strchr(opt_arg, tok_list[i]);
  988. if ((tok_end2) && (tok_end2 < tok_end))
  989. tok_end = tok_end2;
  990. }
  991. callback(callback_arg, instance, targ,
  992. simple_strtol(opt_arg, NULL, 0));
  993. opt_arg = tok_end;
  994. break;
  995. }
  996. }
  997. return (opt_arg);
  998. }
  999. /*
  1000. * Handle Linux boot parameters. This routine allows for assigning a value
  1001. * to a parameter with a ':' between the parameter and the value.
  1002. * ie. aic79xx=stpwlev:1,extended
  1003. */
  1004. static int
  1005. aic79xx_setup(char *s)
  1006. {
  1007. int i, n;
  1008. char *p;
  1009. char *end;
  1010. static const struct {
  1011. const char *name;
  1012. uint32_t *flag;
  1013. } options[] = {
  1014. { "extended", &aic79xx_extended },
  1015. { "no_reset", &aic79xx_no_reset },
  1016. { "verbose", &aic79xx_verbose },
  1017. { "allow_memio", &aic79xx_allow_memio},
  1018. #ifdef AHD_DEBUG
  1019. { "debug", &ahd_debug },
  1020. #endif
  1021. { "periodic_otag", &aic79xx_periodic_otag },
  1022. { "pci_parity", &aic79xx_pci_parity },
  1023. { "seltime", &aic79xx_seltime },
  1024. { "tag_info", NULL },
  1025. { "global_tag_depth", NULL},
  1026. { "slewrate", NULL },
  1027. { "precomp", NULL },
  1028. { "amplitude", NULL },
  1029. { "slowcrc", &aic79xx_slowcrc },
  1030. };
  1031. end = strchr(s, '\0');
  1032. /*
  1033. * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
  1034. * will never be 0 in this case.
  1035. */
  1036. n = 0;
  1037. while ((p = strsep(&s, ",.")) != NULL) {
  1038. if (*p == '\0')
  1039. continue;
  1040. for (i = 0; i < ARRAY_SIZE(options); i++) {
  1041. n = strlen(options[i].name);
  1042. if (strncmp(options[i].name, p, n) == 0)
  1043. break;
  1044. }
  1045. if (i == ARRAY_SIZE(options))
  1046. continue;
  1047. if (strncmp(p, "global_tag_depth", n) == 0) {
  1048. ahd_linux_setup_tag_info_global(p + n);
  1049. } else if (strncmp(p, "tag_info", n) == 0) {
  1050. s = ahd_parse_brace_option("tag_info", p + n, end,
  1051. 2, ahd_linux_setup_tag_info, 0);
  1052. } else if (strncmp(p, "slewrate", n) == 0) {
  1053. s = ahd_parse_brace_option("slewrate",
  1054. p + n, end, 1, ahd_linux_setup_iocell_info,
  1055. AIC79XX_SLEWRATE_INDEX);
  1056. } else if (strncmp(p, "precomp", n) == 0) {
  1057. s = ahd_parse_brace_option("precomp",
  1058. p + n, end, 1, ahd_linux_setup_iocell_info,
  1059. AIC79XX_PRECOMP_INDEX);
  1060. } else if (strncmp(p, "amplitude", n) == 0) {
  1061. s = ahd_parse_brace_option("amplitude",
  1062. p + n, end, 1, ahd_linux_setup_iocell_info,
  1063. AIC79XX_AMPLITUDE_INDEX);
  1064. } else if (p[n] == ':') {
  1065. *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
  1066. } else if (!strncmp(p, "verbose", n)) {
  1067. *(options[i].flag) = 1;
  1068. } else {
  1069. *(options[i].flag) ^= 0xFFFFFFFF;
  1070. }
  1071. }
  1072. return 1;
  1073. }
  1074. __setup("aic79xx=", aic79xx_setup);
  1075. uint32_t aic79xx_verbose;
  1076. int
  1077. ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
  1078. {
  1079. char buf[80];
  1080. struct Scsi_Host *host;
  1081. char *new_name;
  1082. u_long s;
  1083. int retval;
  1084. template->name = ahd->description;
  1085. host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
  1086. if (host == NULL)
  1087. return (ENOMEM);
  1088. *((struct ahd_softc **)host->hostdata) = ahd;
  1089. ahd->platform_data->host = host;
  1090. host->can_queue = AHD_MAX_QUEUE;
  1091. host->cmd_per_lun = 2;
  1092. host->sg_tablesize = AHD_NSEG;
  1093. host->this_id = ahd->our_id;
  1094. host->irq = ahd->platform_data->irq;
  1095. host->max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
  1096. host->max_lun = AHD_NUM_LUNS;
  1097. host->max_channel = 0;
  1098. host->sg_tablesize = AHD_NSEG;
  1099. ahd_lock(ahd, &s);
  1100. ahd_set_unit(ahd, ahd_linux_unit++);
  1101. ahd_unlock(ahd, &s);
  1102. sprintf(buf, "scsi%d", host->host_no);
  1103. new_name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
  1104. if (new_name != NULL) {
  1105. strcpy(new_name, buf);
  1106. ahd_set_name(ahd, new_name);
  1107. }
  1108. host->unique_id = ahd->unit;
  1109. ahd_linux_initialize_scsi_bus(ahd);
  1110. ahd_intr_enable(ahd, TRUE);
  1111. host->transportt = ahd_linux_transport_template;
  1112. retval = scsi_add_host(host, &ahd->dev_softc->dev);
  1113. if (retval) {
  1114. printk(KERN_WARNING "aic79xx: scsi_add_host failed\n");
  1115. scsi_host_put(host);
  1116. return retval;
  1117. }
  1118. scsi_scan_host(host);
  1119. return 0;
  1120. }
  1121. /*
  1122. * Place the SCSI bus into a known state by either resetting it,
  1123. * or forcing transfer negotiations on the next command to any
  1124. * target.
  1125. */
  1126. static void
  1127. ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
  1128. {
  1129. u_int target_id;
  1130. u_int numtarg;
  1131. unsigned long s;
  1132. target_id = 0;
  1133. numtarg = 0;
  1134. if (aic79xx_no_reset != 0)
  1135. ahd->flags &= ~AHD_RESET_BUS_A;
  1136. if ((ahd->flags & AHD_RESET_BUS_A) != 0)
  1137. ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
  1138. else
  1139. numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
  1140. ahd_lock(ahd, &s);
  1141. /*
  1142. * Force negotiation to async for all targets that
  1143. * will not see an initial bus reset.
  1144. */
  1145. for (; target_id < numtarg; target_id++) {
  1146. struct ahd_devinfo devinfo;
  1147. struct ahd_initiator_tinfo *tinfo;
  1148. struct ahd_tmode_tstate *tstate;
  1149. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  1150. target_id, &tstate);
  1151. ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
  1152. CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
  1153. ahd_update_neg_request(ahd, &devinfo, tstate,
  1154. tinfo, AHD_NEG_ALWAYS);
  1155. }
  1156. ahd_unlock(ahd, &s);
  1157. /* Give the bus some time to recover */
  1158. if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
  1159. ahd_freeze_simq(ahd);
  1160. msleep(AIC79XX_RESET_DELAY);
  1161. ahd_release_simq(ahd);
  1162. }
  1163. }
  1164. int
  1165. ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
  1166. {
  1167. ahd->platform_data =
  1168. kzalloc(sizeof(struct ahd_platform_data), GFP_ATOMIC);
  1169. if (ahd->platform_data == NULL)
  1170. return (ENOMEM);
  1171. ahd->platform_data->irq = AHD_LINUX_NOIRQ;
  1172. ahd_lockinit(ahd);
  1173. ahd->seltime = (aic79xx_seltime & 0x3) << 4;
  1174. return (0);
  1175. }
  1176. void
  1177. ahd_platform_free(struct ahd_softc *ahd)
  1178. {
  1179. struct scsi_target *starget;
  1180. int i;
  1181. if (ahd->platform_data != NULL) {
  1182. /* destroy all of the device and target objects */
  1183. for (i = 0; i < AHD_NUM_TARGETS; i++) {
  1184. starget = ahd->platform_data->starget[i];
  1185. if (starget != NULL) {
  1186. ahd->platform_data->starget[i] = NULL;
  1187. }
  1188. }
  1189. if (ahd->platform_data->irq != AHD_LINUX_NOIRQ)
  1190. free_irq(ahd->platform_data->irq, ahd);
  1191. if (ahd->tags[0] == BUS_SPACE_PIO
  1192. && ahd->bshs[0].ioport != 0)
  1193. release_region(ahd->bshs[0].ioport, 256);
  1194. if (ahd->tags[1] == BUS_SPACE_PIO
  1195. && ahd->bshs[1].ioport != 0)
  1196. release_region(ahd->bshs[1].ioport, 256);
  1197. if (ahd->tags[0] == BUS_SPACE_MEMIO
  1198. && ahd->bshs[0].maddr != NULL) {
  1199. iounmap(ahd->bshs[0].maddr);
  1200. release_mem_region(ahd->platform_data->mem_busaddr,
  1201. 0x1000);
  1202. }
  1203. if (ahd->platform_data->host)
  1204. scsi_host_put(ahd->platform_data->host);
  1205. kfree(ahd->platform_data);
  1206. }
  1207. }
  1208. void
  1209. ahd_platform_init(struct ahd_softc *ahd)
  1210. {
  1211. /*
  1212. * Lookup and commit any modified IO Cell options.
  1213. */
  1214. if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
  1215. const struct ahd_linux_iocell_opts *iocell_opts;
  1216. iocell_opts = &aic79xx_iocell_info[ahd->unit];
  1217. if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
  1218. AHD_SET_PRECOMP(ahd, iocell_opts->precomp);
  1219. if (iocell_opts->slewrate != AIC79XX_DEFAULT_SLEWRATE)
  1220. AHD_SET_SLEWRATE(ahd, iocell_opts->slewrate);
  1221. if (iocell_opts->amplitude != AIC79XX_DEFAULT_AMPLITUDE)
  1222. AHD_SET_AMPLITUDE(ahd, iocell_opts->amplitude);
  1223. }
  1224. }
  1225. void
  1226. ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
  1227. {
  1228. ahd_platform_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  1229. SCB_GET_CHANNEL(ahd, scb),
  1230. SCB_GET_LUN(scb), SCB_LIST_NULL,
  1231. ROLE_UNKNOWN, CAM_REQUEUE_REQ);
  1232. }
  1233. void
  1234. ahd_platform_set_tags(struct ahd_softc *ahd, struct scsi_device *sdev,
  1235. struct ahd_devinfo *devinfo, ahd_queue_alg alg)
  1236. {
  1237. struct ahd_linux_device *dev;
  1238. int was_queuing;
  1239. int now_queuing;
  1240. if (sdev == NULL)
  1241. return;
  1242. dev = scsi_transport_device_data(sdev);
  1243. if (dev == NULL)
  1244. return;
  1245. was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
  1246. switch (alg) {
  1247. default:
  1248. case AHD_QUEUE_NONE:
  1249. now_queuing = 0;
  1250. break;
  1251. case AHD_QUEUE_BASIC:
  1252. now_queuing = AHD_DEV_Q_BASIC;
  1253. break;
  1254. case AHD_QUEUE_TAGGED:
  1255. now_queuing = AHD_DEV_Q_TAGGED;
  1256. break;
  1257. }
  1258. if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) == 0
  1259. && (was_queuing != now_queuing)
  1260. && (dev->active != 0)) {
  1261. dev->flags |= AHD_DEV_FREEZE_TIL_EMPTY;
  1262. dev->qfrozen++;
  1263. }
  1264. dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
  1265. if (now_queuing) {
  1266. u_int usertags;
  1267. usertags = ahd_linux_user_tagdepth(ahd, devinfo);
  1268. if (!was_queuing) {
  1269. /*
  1270. * Start out aggressively and allow our
  1271. * dynamic queue depth algorithm to take
  1272. * care of the rest.
  1273. */
  1274. dev->maxtags = usertags;
  1275. dev->openings = dev->maxtags - dev->active;
  1276. }
  1277. if (dev->maxtags == 0) {
  1278. /*
  1279. * Queueing is disabled by the user.
  1280. */
  1281. dev->openings = 1;
  1282. } else if (alg == AHD_QUEUE_TAGGED) {
  1283. dev->flags |= AHD_DEV_Q_TAGGED;
  1284. if (aic79xx_periodic_otag != 0)
  1285. dev->flags |= AHD_DEV_PERIODIC_OTAG;
  1286. } else
  1287. dev->flags |= AHD_DEV_Q_BASIC;
  1288. } else {
  1289. /* We can only have one opening. */
  1290. dev->maxtags = 0;
  1291. dev->openings = 1 - dev->active;
  1292. }
  1293. switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
  1294. case AHD_DEV_Q_BASIC:
  1295. case AHD_DEV_Q_TAGGED:
  1296. scsi_change_queue_depth(sdev,
  1297. dev->openings + dev->active);
  1298. break;
  1299. default:
  1300. /*
  1301. * We allow the OS to queue 2 untagged transactions to
  1302. * us at any time even though we can only execute them
  1303. * serially on the controller/device. This should
  1304. * remove some latency.
  1305. */
  1306. scsi_change_queue_depth(sdev, 1);
  1307. break;
  1308. }
  1309. }
  1310. int
  1311. ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
  1312. int lun, u_int tag, role_t role, uint32_t status)
  1313. {
  1314. return 0;
  1315. }
  1316. static u_int
  1317. ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  1318. {
  1319. static int warned_user;
  1320. u_int tags;
  1321. tags = 0;
  1322. if ((ahd->user_discenable & devinfo->target_mask) != 0) {
  1323. if (ahd->unit >= ARRAY_SIZE(aic79xx_tag_info)) {
  1324. if (warned_user == 0) {
  1325. printk(KERN_WARNING
  1326. "aic79xx: WARNING: Insufficient tag_info instances\n"
  1327. "aic79xx: for installed controllers. Using defaults\n"
  1328. "aic79xx: Please update the aic79xx_tag_info array in\n"
  1329. "aic79xx: the aic79xx_osm.c source file.\n");
  1330. warned_user++;
  1331. }
  1332. tags = AHD_MAX_QUEUE;
  1333. } else {
  1334. adapter_tag_info_t *tag_info;
  1335. tag_info = &aic79xx_tag_info[ahd->unit];
  1336. tags = tag_info->tag_commands[devinfo->target_offset];
  1337. if (tags > AHD_MAX_QUEUE)
  1338. tags = AHD_MAX_QUEUE;
  1339. }
  1340. }
  1341. return (tags);
  1342. }
  1343. /*
  1344. * Determines the queue depth for a given device.
  1345. */
  1346. static void
  1347. ahd_linux_device_queue_depth(struct scsi_device *sdev)
  1348. {
  1349. struct ahd_devinfo devinfo;
  1350. u_int tags;
  1351. struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
  1352. ahd_compile_devinfo(&devinfo,
  1353. ahd->our_id,
  1354. sdev->sdev_target->id, sdev->lun,
  1355. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1356. ROLE_INITIATOR);
  1357. tags = ahd_linux_user_tagdepth(ahd, &devinfo);
  1358. if (tags != 0 && sdev->tagged_supported != 0) {
  1359. ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_TAGGED);
  1360. ahd_send_async(ahd, devinfo.channel, devinfo.target,
  1361. devinfo.lun, AC_TRANSFER_NEG);
  1362. ahd_print_devinfo(ahd, &devinfo);
  1363. printk("Tagged Queuing enabled. Depth %d\n", tags);
  1364. } else {
  1365. ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_NONE);
  1366. ahd_send_async(ahd, devinfo.channel, devinfo.target,
  1367. devinfo.lun, AC_TRANSFER_NEG);
  1368. }
  1369. }
  1370. static int
  1371. ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
  1372. struct scsi_cmnd *cmd)
  1373. {
  1374. struct scb *scb;
  1375. struct hardware_scb *hscb;
  1376. struct ahd_initiator_tinfo *tinfo;
  1377. struct ahd_tmode_tstate *tstate;
  1378. u_int col_idx;
  1379. uint16_t mask;
  1380. unsigned long flags;
  1381. int nseg;
  1382. nseg = scsi_dma_map(cmd);
  1383. if (nseg < 0)
  1384. return SCSI_MLQUEUE_HOST_BUSY;
  1385. ahd_lock(ahd, &flags);
  1386. /*
  1387. * Get an scb to use.
  1388. */
  1389. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  1390. cmd->device->id, &tstate);
  1391. if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) == 0
  1392. || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
  1393. col_idx = AHD_NEVER_COL_IDX;
  1394. } else {
  1395. col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
  1396. cmd->device->lun);
  1397. }
  1398. if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
  1399. ahd->flags |= AHD_RESOURCE_SHORTAGE;
  1400. ahd_unlock(ahd, &flags);
  1401. scsi_dma_unmap(cmd);
  1402. return SCSI_MLQUEUE_HOST_BUSY;
  1403. }
  1404. scb->io_ctx = cmd;
  1405. scb->platform_data->dev = dev;
  1406. hscb = scb->hscb;
  1407. cmd->host_scribble = (char *)scb;
  1408. /*
  1409. * Fill out basics of the HSCB.
  1410. */
  1411. hscb->control = 0;
  1412. hscb->scsiid = BUILD_SCSIID(ahd, cmd);
  1413. hscb->lun = cmd->device->lun;
  1414. scb->hscb->task_management = 0;
  1415. mask = SCB_GET_TARGET_MASK(ahd, scb);
  1416. if ((ahd->user_discenable & mask) != 0)
  1417. hscb->control |= DISCENB;
  1418. if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
  1419. scb->flags |= SCB_PACKETIZED;
  1420. if ((tstate->auto_negotiate & mask) != 0) {
  1421. scb->flags |= SCB_AUTO_NEGOTIATE;
  1422. scb->hscb->control |= MK_MESSAGE;
  1423. }
  1424. if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
  1425. if (dev->commands_since_idle_or_otag == AHD_OTAG_THRESH
  1426. && (dev->flags & AHD_DEV_Q_TAGGED) != 0) {
  1427. hscb->control |= MSG_ORDERED_TASK;
  1428. dev->commands_since_idle_or_otag = 0;
  1429. } else {
  1430. hscb->control |= MSG_SIMPLE_TASK;
  1431. }
  1432. }
  1433. hscb->cdb_len = cmd->cmd_len;
  1434. memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);
  1435. scb->platform_data->xfer_len = 0;
  1436. ahd_set_residual(scb, 0);
  1437. ahd_set_sense_residual(scb, 0);
  1438. scb->sg_count = 0;
  1439. if (nseg > 0) {
  1440. void *sg = scb->sg_list;
  1441. struct scatterlist *cur_seg;
  1442. int i;
  1443. scb->platform_data->xfer_len = 0;
  1444. scsi_for_each_sg(cmd, cur_seg, nseg, i) {
  1445. dma_addr_t addr;
  1446. bus_size_t len;
  1447. addr = sg_dma_address(cur_seg);
  1448. len = sg_dma_len(cur_seg);
  1449. scb->platform_data->xfer_len += len;
  1450. sg = ahd_sg_setup(ahd, scb, sg, addr, len,
  1451. i == (nseg - 1));
  1452. }
  1453. }
  1454. LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
  1455. dev->openings--;
  1456. dev->active++;
  1457. dev->commands_issued++;
  1458. if ((dev->flags & AHD_DEV_PERIODIC_OTAG) != 0)
  1459. dev->commands_since_idle_or_otag++;
  1460. scb->flags |= SCB_ACTIVE;
  1461. ahd_queue_scb(ahd, scb);
  1462. ahd_unlock(ahd, &flags);
  1463. return 0;
  1464. }
  1465. /*
  1466. * SCSI controller interrupt handler.
  1467. */
  1468. irqreturn_t
  1469. ahd_linux_isr(int irq, void *dev_id)
  1470. {
  1471. struct ahd_softc *ahd;
  1472. u_long flags;
  1473. int ours;
  1474. ahd = (struct ahd_softc *) dev_id;
  1475. ahd_lock(ahd, &flags);
  1476. ours = ahd_intr(ahd);
  1477. ahd_unlock(ahd, &flags);
  1478. return IRQ_RETVAL(ours);
  1479. }
  1480. void
  1481. ahd_send_async(struct ahd_softc *ahd, char channel,
  1482. u_int target, u_int lun, ac_code code)
  1483. {
  1484. switch (code) {
  1485. case AC_TRANSFER_NEG:
  1486. {
  1487. struct scsi_target *starget;
  1488. struct ahd_initiator_tinfo *tinfo;
  1489. struct ahd_tmode_tstate *tstate;
  1490. unsigned int target_ppr_options;
  1491. BUG_ON(target == CAM_TARGET_WILDCARD);
  1492. tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
  1493. target, &tstate);
  1494. /*
  1495. * Don't bother reporting results while
  1496. * negotiations are still pending.
  1497. */
  1498. if (tinfo->curr.period != tinfo->goal.period
  1499. || tinfo->curr.width != tinfo->goal.width
  1500. || tinfo->curr.offset != tinfo->goal.offset
  1501. || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
  1502. if (bootverbose == 0)
  1503. break;
  1504. /*
  1505. * Don't bother reporting results that
  1506. * are identical to those last reported.
  1507. */
  1508. starget = ahd->platform_data->starget[target];
  1509. if (starget == NULL)
  1510. break;
  1511. target_ppr_options =
  1512. (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
  1513. + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
  1514. + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0)
  1515. + (spi_rd_strm(starget) ? MSG_EXT_PPR_RD_STRM : 0)
  1516. + (spi_pcomp_en(starget) ? MSG_EXT_PPR_PCOMP_EN : 0)
  1517. + (spi_rti(starget) ? MSG_EXT_PPR_RTI : 0)
  1518. + (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0)
  1519. + (spi_hold_mcs(starget) ? MSG_EXT_PPR_HOLD_MCS : 0);
  1520. if (tinfo->curr.period == spi_period(starget)
  1521. && tinfo->curr.width == spi_width(starget)
  1522. && tinfo->curr.offset == spi_offset(starget)
  1523. && tinfo->curr.ppr_options == target_ppr_options)
  1524. if (bootverbose == 0)
  1525. break;
  1526. spi_period(starget) = tinfo->curr.period;
  1527. spi_width(starget) = tinfo->curr.width;
  1528. spi_offset(starget) = tinfo->curr.offset;
  1529. spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
  1530. spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
  1531. spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
  1532. spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
  1533. spi_pcomp_en(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
  1534. spi_rti(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RTI ? 1 : 0;
  1535. spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
  1536. spi_hold_mcs(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_HOLD_MCS ? 1 : 0;
  1537. spi_display_xfer_agreement(starget);
  1538. break;
  1539. }
  1540. case AC_SENT_BDR:
  1541. {
  1542. WARN_ON(lun != CAM_LUN_WILDCARD);
  1543. scsi_report_device_reset(ahd->platform_data->host,
  1544. channel - 'A', target);
  1545. break;
  1546. }
  1547. case AC_BUS_RESET:
  1548. if (ahd->platform_data->host != NULL) {
  1549. scsi_report_bus_reset(ahd->platform_data->host,
  1550. channel - 'A');
  1551. }
  1552. break;
  1553. default:
  1554. panic("ahd_send_async: Unexpected async event");
  1555. }
  1556. }
  1557. /*
  1558. * Calls the higher level scsi done function and frees the scb.
  1559. */
  1560. void
  1561. ahd_done(struct ahd_softc *ahd, struct scb *scb)
  1562. {
  1563. struct scsi_cmnd *cmd;
  1564. struct ahd_linux_device *dev;
  1565. if ((scb->flags & SCB_ACTIVE) == 0) {
  1566. printk("SCB %d done'd twice\n", SCB_GET_TAG(scb));
  1567. ahd_dump_card_state(ahd);
  1568. panic("Stopping for safety");
  1569. }
  1570. LIST_REMOVE(scb, pending_links);
  1571. cmd = scb->io_ctx;
  1572. dev = scb->platform_data->dev;
  1573. dev->active--;
  1574. dev->openings++;
  1575. if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
  1576. cmd->result &= ~(CAM_DEV_QFRZN << 16);
  1577. dev->qfrozen--;
  1578. }
  1579. ahd_linux_unmap_scb(ahd, scb);
  1580. /*
  1581. * Guard against stale sense data.
  1582. * The Linux mid-layer assumes that sense
  1583. * was retrieved anytime the first byte of
  1584. * the sense buffer looks "sane".
  1585. */
  1586. cmd->sense_buffer[0] = 0;
  1587. if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
  1588. uint32_t amount_xferred;
  1589. amount_xferred =
  1590. ahd_get_transfer_length(scb) - ahd_get_residual(scb);
  1591. if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
  1592. #ifdef AHD_DEBUG
  1593. if ((ahd_debug & AHD_SHOW_MISC) != 0) {
  1594. ahd_print_path(ahd, scb);
  1595. printk("Set CAM_UNCOR_PARITY\n");
  1596. }
  1597. #endif
  1598. ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1599. #ifdef AHD_REPORT_UNDERFLOWS
  1600. /*
  1601. * This code is disabled by default as some
  1602. * clients of the SCSI system do not properly
  1603. * initialize the underflow parameter. This
  1604. * results in spurious termination of commands
  1605. * that complete as expected (e.g. underflow is
  1606. * allowed as command can return variable amounts
  1607. * of data.
  1608. */
  1609. } else if (amount_xferred < scb->io_ctx->underflow) {
  1610. u_int i;
  1611. ahd_print_path(ahd, scb);
  1612. printk("CDB:");
  1613. for (i = 0; i < scb->io_ctx->cmd_len; i++)
  1614. printk(" 0x%x", scb->io_ctx->cmnd[i]);
  1615. printk("\n");
  1616. ahd_print_path(ahd, scb);
  1617. printk("Saw underflow (%ld of %ld bytes). "
  1618. "Treated as error\n",
  1619. ahd_get_residual(scb),
  1620. ahd_get_transfer_length(scb));
  1621. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1622. #endif
  1623. } else {
  1624. ahd_set_transaction_status(scb, CAM_REQ_CMP);
  1625. }
  1626. } else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
  1627. ahd_linux_handle_scsi_status(ahd, cmd->device, scb);
  1628. }
  1629. if (dev->openings == 1
  1630. && ahd_get_transaction_status(scb) == CAM_REQ_CMP
  1631. && ahd_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
  1632. dev->tag_success_count++;
  1633. /*
  1634. * Some devices deal with temporary internal resource
  1635. * shortages by returning queue full. When the queue
  1636. * full occurrs, we throttle back. Slowly try to get
  1637. * back to our previous queue depth.
  1638. */
  1639. if ((dev->openings + dev->active) < dev->maxtags
  1640. && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
  1641. dev->tag_success_count = 0;
  1642. dev->openings++;
  1643. }
  1644. if (dev->active == 0)
  1645. dev->commands_since_idle_or_otag = 0;
  1646. if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
  1647. printk("Recovery SCB completes\n");
  1648. if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
  1649. || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
  1650. ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
  1651. if (ahd->platform_data->eh_done)
  1652. complete(ahd->platform_data->eh_done);
  1653. }
  1654. ahd_free_scb(ahd, scb);
  1655. ahd_linux_queue_cmd_complete(ahd, cmd);
  1656. }
  1657. static void
  1658. ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
  1659. struct scsi_device *sdev, struct scb *scb)
  1660. {
  1661. struct ahd_devinfo devinfo;
  1662. struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
  1663. ahd_compile_devinfo(&devinfo,
  1664. ahd->our_id,
  1665. sdev->sdev_target->id, sdev->lun,
  1666. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1667. ROLE_INITIATOR);
  1668. /*
  1669. * We don't currently trust the mid-layer to
  1670. * properly deal with queue full or busy. So,
  1671. * when one occurs, we tell the mid-layer to
  1672. * unconditionally requeue the command to us
  1673. * so that we can retry it ourselves. We also
  1674. * implement our own throttling mechanism so
  1675. * we don't clobber the device with too many
  1676. * commands.
  1677. */
  1678. switch (ahd_get_scsi_status(scb)) {
  1679. default:
  1680. break;
  1681. case SCSI_STATUS_CHECK_COND:
  1682. case SCSI_STATUS_CMD_TERMINATED:
  1683. {
  1684. struct scsi_cmnd *cmd;
  1685. /*
  1686. * Copy sense information to the OS's cmd
  1687. * structure if it is available.
  1688. */
  1689. cmd = scb->io_ctx;
  1690. if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
  1691. struct scsi_status_iu_header *siu;
  1692. u_int sense_size;
  1693. u_int sense_offset;
  1694. if (scb->flags & SCB_SENSE) {
  1695. sense_size = min(sizeof(struct scsi_sense_data)
  1696. - ahd_get_sense_residual(scb),
  1697. (u_long)SCSI_SENSE_BUFFERSIZE);
  1698. sense_offset = 0;
  1699. } else {
  1700. /*
  1701. * Copy only the sense data into the provided
  1702. * buffer.
  1703. */
  1704. siu = (struct scsi_status_iu_header *)
  1705. scb->sense_data;
  1706. sense_size = min_t(size_t,
  1707. scsi_4btoul(siu->sense_length),
  1708. SCSI_SENSE_BUFFERSIZE);
  1709. sense_offset = SIU_SENSE_OFFSET(siu);
  1710. }
  1711. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1712. memcpy(cmd->sense_buffer,
  1713. ahd_get_sense_buf(ahd, scb)
  1714. + sense_offset, sense_size);
  1715. cmd->result |= (DRIVER_SENSE << 24);
  1716. #ifdef AHD_DEBUG
  1717. if (ahd_debug & AHD_SHOW_SENSE) {
  1718. int i;
  1719. printk("Copied %d bytes of sense data at %d:",
  1720. sense_size, sense_offset);
  1721. for (i = 0; i < sense_size; i++) {
  1722. if ((i & 0xF) == 0)
  1723. printk("\n");
  1724. printk("0x%x ", cmd->sense_buffer[i]);
  1725. }
  1726. printk("\n");
  1727. }
  1728. #endif
  1729. }
  1730. break;
  1731. }
  1732. case SCSI_STATUS_QUEUE_FULL:
  1733. /*
  1734. * By the time the core driver has returned this
  1735. * command, all other commands that were queued
  1736. * to us but not the device have been returned.
  1737. * This ensures that dev->active is equal to
  1738. * the number of commands actually queued to
  1739. * the device.
  1740. */
  1741. dev->tag_success_count = 0;
  1742. if (dev->active != 0) {
  1743. /*
  1744. * Drop our opening count to the number
  1745. * of commands currently outstanding.
  1746. */
  1747. dev->openings = 0;
  1748. #ifdef AHD_DEBUG
  1749. if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
  1750. ahd_print_path(ahd, scb);
  1751. printk("Dropping tag count to %d\n",
  1752. dev->active);
  1753. }
  1754. #endif
  1755. if (dev->active == dev->tags_on_last_queuefull) {
  1756. dev->last_queuefull_same_count++;
  1757. /*
  1758. * If we repeatedly see a queue full
  1759. * at the same queue depth, this
  1760. * device has a fixed number of tag
  1761. * slots. Lock in this tag depth
  1762. * so we stop seeing queue fulls from
  1763. * this device.
  1764. */
  1765. if (dev->last_queuefull_same_count
  1766. == AHD_LOCK_TAGS_COUNT) {
  1767. dev->maxtags = dev->active;
  1768. ahd_print_path(ahd, scb);
  1769. printk("Locking max tag count at %d\n",
  1770. dev->active);
  1771. }
  1772. } else {
  1773. dev->tags_on_last_queuefull = dev->active;
  1774. dev->last_queuefull_same_count = 0;
  1775. }
  1776. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  1777. ahd_set_scsi_status(scb, SCSI_STATUS_OK);
  1778. ahd_platform_set_tags(ahd, sdev, &devinfo,
  1779. (dev->flags & AHD_DEV_Q_BASIC)
  1780. ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
  1781. break;
  1782. }
  1783. /*
  1784. * Drop down to a single opening, and treat this
  1785. * as if the target returned BUSY SCSI status.
  1786. */
  1787. dev->openings = 1;
  1788. ahd_platform_set_tags(ahd, sdev, &devinfo,
  1789. (dev->flags & AHD_DEV_Q_BASIC)
  1790. ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
  1791. ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
  1792. }
  1793. }
  1794. static void
  1795. ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
  1796. {
  1797. int status;
  1798. int new_status = DID_OK;
  1799. int do_fallback = 0;
  1800. int scsi_status;
  1801. /*
  1802. * Map CAM error codes into Linux Error codes. We
  1803. * avoid the conversion so that the DV code has the
  1804. * full error information available when making
  1805. * state change decisions.
  1806. */
  1807. status = ahd_cmd_get_transaction_status(cmd);
  1808. switch (status) {
  1809. case CAM_REQ_INPROG:
  1810. case CAM_REQ_CMP:
  1811. new_status = DID_OK;
  1812. break;
  1813. case CAM_AUTOSENSE_FAIL:
  1814. new_status = DID_ERROR;
  1815. /* Fallthrough */
  1816. case CAM_SCSI_STATUS_ERROR:
  1817. scsi_status = ahd_cmd_get_scsi_status(cmd);
  1818. switch(scsi_status) {
  1819. case SCSI_STATUS_CMD_TERMINATED:
  1820. case SCSI_STATUS_CHECK_COND:
  1821. if ((cmd->result >> 24) != DRIVER_SENSE) {
  1822. do_fallback = 1;
  1823. } else {
  1824. struct scsi_sense_data *sense;
  1825. sense = (struct scsi_sense_data *)
  1826. cmd->sense_buffer;
  1827. if (sense->extra_len >= 5 &&
  1828. (sense->add_sense_code == 0x47
  1829. || sense->add_sense_code == 0x48))
  1830. do_fallback = 1;
  1831. }
  1832. break;
  1833. default:
  1834. break;
  1835. }
  1836. break;
  1837. case CAM_REQ_ABORTED:
  1838. new_status = DID_ABORT;
  1839. break;
  1840. case CAM_BUSY:
  1841. new_status = DID_BUS_BUSY;
  1842. break;
  1843. case CAM_REQ_INVALID:
  1844. case CAM_PATH_INVALID:
  1845. new_status = DID_BAD_TARGET;
  1846. break;
  1847. case CAM_SEL_TIMEOUT:
  1848. new_status = DID_NO_CONNECT;
  1849. break;
  1850. case CAM_SCSI_BUS_RESET:
  1851. case CAM_BDR_SENT:
  1852. new_status = DID_RESET;
  1853. break;
  1854. case CAM_UNCOR_PARITY:
  1855. new_status = DID_PARITY;
  1856. do_fallback = 1;
  1857. break;
  1858. case CAM_CMD_TIMEOUT:
  1859. new_status = DID_TIME_OUT;
  1860. do_fallback = 1;
  1861. break;
  1862. case CAM_REQ_CMP_ERR:
  1863. case CAM_UNEXP_BUSFREE:
  1864. case CAM_DATA_RUN_ERR:
  1865. new_status = DID_ERROR;
  1866. do_fallback = 1;
  1867. break;
  1868. case CAM_UA_ABORT:
  1869. case CAM_NO_HBA:
  1870. case CAM_SEQUENCE_FAIL:
  1871. case CAM_CCB_LEN_ERR:
  1872. case CAM_PROVIDE_FAIL:
  1873. case CAM_REQ_TERMIO:
  1874. case CAM_UNREC_HBA_ERROR:
  1875. case CAM_REQ_TOO_BIG:
  1876. new_status = DID_ERROR;
  1877. break;
  1878. case CAM_REQUEUE_REQ:
  1879. new_status = DID_REQUEUE;
  1880. break;
  1881. default:
  1882. /* We should never get here */
  1883. new_status = DID_ERROR;
  1884. break;
  1885. }
  1886. if (do_fallback) {
  1887. printk("%s: device overrun (status %x) on %d:%d:%d\n",
  1888. ahd_name(ahd), status, cmd->device->channel,
  1889. cmd->device->id, (u8)cmd->device->lun);
  1890. }
  1891. ahd_cmd_set_transaction_status(cmd, new_status);
  1892. cmd->scsi_done(cmd);
  1893. }
  1894. static void
  1895. ahd_freeze_simq(struct ahd_softc *ahd)
  1896. {
  1897. scsi_block_requests(ahd->platform_data->host);
  1898. }
  1899. static void
  1900. ahd_release_simq(struct ahd_softc *ahd)
  1901. {
  1902. scsi_unblock_requests(ahd->platform_data->host);
  1903. }
  1904. static int
  1905. ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd)
  1906. {
  1907. struct ahd_softc *ahd;
  1908. struct ahd_linux_device *dev;
  1909. struct scb *pending_scb;
  1910. u_int saved_scbptr;
  1911. u_int active_scbptr;
  1912. u_int last_phase;
  1913. u_int saved_scsiid;
  1914. u_int cdb_byte;
  1915. int retval;
  1916. int was_paused;
  1917. int paused;
  1918. int wait;
  1919. int disconnected;
  1920. ahd_mode_state saved_modes;
  1921. unsigned long flags;
  1922. pending_scb = NULL;
  1923. paused = FALSE;
  1924. wait = FALSE;
  1925. ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
  1926. scmd_printk(KERN_INFO, cmd,
  1927. "Attempting to queue an ABORT message:");
  1928. printk("CDB:");
  1929. for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
  1930. printk(" 0x%x", cmd->cmnd[cdb_byte]);
  1931. printk("\n");
  1932. ahd_lock(ahd, &flags);
  1933. /*
  1934. * First determine if we currently own this command.
  1935. * Start by searching the device queue. If not found
  1936. * there, check the pending_scb list. If not found
  1937. * at all, and the system wanted us to just abort the
  1938. * command, return success.
  1939. */
  1940. dev = scsi_transport_device_data(cmd->device);
  1941. if (dev == NULL) {
  1942. /*
  1943. * No target device for this command exists,
  1944. * so we must not still own the command.
  1945. */
  1946. scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
  1947. retval = SUCCESS;
  1948. goto no_cmd;
  1949. }
  1950. /*
  1951. * See if we can find a matching cmd in the pending list.
  1952. */
  1953. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  1954. if (pending_scb->io_ctx == cmd)
  1955. break;
  1956. }
  1957. if (pending_scb == NULL) {
  1958. scmd_printk(KERN_INFO, cmd, "Command not found\n");
  1959. goto no_cmd;
  1960. }
  1961. if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
  1962. /*
  1963. * We can't queue two recovery actions using the same SCB
  1964. */
  1965. retval = FAILED;
  1966. goto done;
  1967. }
  1968. /*
  1969. * Ensure that the card doesn't do anything
  1970. * behind our back. Also make sure that we
  1971. * didn't "just" miss an interrupt that would
  1972. * affect this cmd.
  1973. */
  1974. was_paused = ahd_is_paused(ahd);
  1975. ahd_pause_and_flushwork(ahd);
  1976. paused = TRUE;
  1977. if ((pending_scb->flags & SCB_ACTIVE) == 0) {
  1978. scmd_printk(KERN_INFO, cmd, "Command already completed\n");
  1979. goto no_cmd;
  1980. }
  1981. printk("%s: At time of recovery, card was %spaused\n",
  1982. ahd_name(ahd), was_paused ? "" : "not ");
  1983. ahd_dump_card_state(ahd);
  1984. disconnected = TRUE;
  1985. if (ahd_search_qinfifo(ahd, cmd->device->id,
  1986. cmd->device->channel + 'A',
  1987. cmd->device->lun,
  1988. pending_scb->hscb->tag,
  1989. ROLE_INITIATOR, CAM_REQ_ABORTED,
  1990. SEARCH_COMPLETE) > 0) {
  1991. printk("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
  1992. ahd_name(ahd), cmd->device->channel,
  1993. cmd->device->id, (u8)cmd->device->lun);
  1994. retval = SUCCESS;
  1995. goto done;
  1996. }
  1997. saved_modes = ahd_save_modes(ahd);
  1998. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1999. last_phase = ahd_inb(ahd, LASTPHASE);
  2000. saved_scbptr = ahd_get_scbptr(ahd);
  2001. active_scbptr = saved_scbptr;
  2002. if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
  2003. struct scb *bus_scb;
  2004. bus_scb = ahd_lookup_scb(ahd, active_scbptr);
  2005. if (bus_scb == pending_scb)
  2006. disconnected = FALSE;
  2007. }
  2008. /*
  2009. * At this point, pending_scb is the scb associated with the
  2010. * passed in command. That command is currently active on the
  2011. * bus or is in the disconnected state.
  2012. */
  2013. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  2014. if (last_phase != P_BUSFREE
  2015. && SCB_GET_TAG(pending_scb) == active_scbptr) {
  2016. /*
  2017. * We're active on the bus, so assert ATN
  2018. * and hope that the target responds.
  2019. */
  2020. pending_scb = ahd_lookup_scb(ahd, active_scbptr);
  2021. pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
  2022. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  2023. ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
  2024. scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
  2025. wait = TRUE;
  2026. } else if (disconnected) {
  2027. /*
  2028. * Actually re-queue this SCB in an attempt
  2029. * to select the device before it reconnects.
  2030. */
  2031. pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
  2032. ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
  2033. pending_scb->hscb->cdb_len = 0;
  2034. pending_scb->hscb->task_attribute = 0;
  2035. pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
  2036. if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
  2037. /*
  2038. * Mark the SCB has having an outstanding
  2039. * task management function. Should the command
  2040. * complete normally before the task management
  2041. * function can be sent, the host will be notified
  2042. * to abort our requeued SCB.
  2043. */
  2044. ahd_outb(ahd, SCB_TASK_MANAGEMENT,
  2045. pending_scb->hscb->task_management);
  2046. } else {
  2047. /*
  2048. * If non-packetized, set the MK_MESSAGE control
  2049. * bit indicating that we desire to send a message.
  2050. * We also set the disconnected flag since there is
  2051. * no guarantee that our SCB control byte matches
  2052. * the version on the card. We don't want the
  2053. * sequencer to abort the command thinking an
  2054. * unsolicited reselection occurred.
  2055. */
  2056. pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
  2057. /*
  2058. * The sequencer will never re-reference the
  2059. * in-core SCB. To make sure we are notified
  2060. * during reselection, set the MK_MESSAGE flag in
  2061. * the card's copy of the SCB.
  2062. */
  2063. ahd_outb(ahd, SCB_CONTROL,
  2064. ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
  2065. }
  2066. /*
  2067. * Clear out any entries in the QINFIFO first
  2068. * so we are the next SCB for this target
  2069. * to run.
  2070. */
  2071. ahd_search_qinfifo(ahd, cmd->device->id,
  2072. cmd->device->channel + 'A', cmd->device->lun,
  2073. SCB_LIST_NULL, ROLE_INITIATOR,
  2074. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  2075. ahd_qinfifo_requeue_tail(ahd, pending_scb);
  2076. ahd_set_scbptr(ahd, saved_scbptr);
  2077. ahd_print_path(ahd, pending_scb);
  2078. printk("Device is disconnected, re-queuing SCB\n");
  2079. wait = TRUE;
  2080. } else {
  2081. scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
  2082. retval = FAILED;
  2083. goto done;
  2084. }
  2085. no_cmd:
  2086. /*
  2087. * Our assumption is that if we don't have the command, no
  2088. * recovery action was required, so we return success. Again,
  2089. * the semantics of the mid-layer recovery engine are not
  2090. * well defined, so this may change in time.
  2091. */
  2092. retval = SUCCESS;
  2093. done:
  2094. if (paused)
  2095. ahd_unpause(ahd);
  2096. if (wait) {
  2097. DECLARE_COMPLETION_ONSTACK(done);
  2098. ahd->platform_data->eh_done = &done;
  2099. ahd_unlock(ahd, &flags);
  2100. printk("%s: Recovery code sleeping\n", ahd_name(ahd));
  2101. if (!wait_for_completion_timeout(&done, 5 * HZ)) {
  2102. ahd_lock(ahd, &flags);
  2103. ahd->platform_data->eh_done = NULL;
  2104. ahd_unlock(ahd, &flags);
  2105. printk("%s: Timer Expired (active %d)\n",
  2106. ahd_name(ahd), dev->active);
  2107. retval = FAILED;
  2108. }
  2109. printk("Recovery code awake\n");
  2110. } else
  2111. ahd_unlock(ahd, &flags);
  2112. if (retval != SUCCESS)
  2113. printk("%s: Command abort returning 0x%x\n",
  2114. ahd_name(ahd), retval);
  2115. return retval;
  2116. }
  2117. static void ahd_linux_set_width(struct scsi_target *starget, int width)
  2118. {
  2119. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2120. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2121. struct ahd_devinfo devinfo;
  2122. unsigned long flags;
  2123. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2124. starget->channel + 'A', ROLE_INITIATOR);
  2125. ahd_lock(ahd, &flags);
  2126. ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
  2127. ahd_unlock(ahd, &flags);
  2128. }
  2129. static void ahd_linux_set_period(struct scsi_target *starget, int period)
  2130. {
  2131. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2132. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2133. struct ahd_tmode_tstate *tstate;
  2134. struct ahd_initiator_tinfo *tinfo
  2135. = ahd_fetch_transinfo(ahd,
  2136. starget->channel + 'A',
  2137. shost->this_id, starget->id, &tstate);
  2138. struct ahd_devinfo devinfo;
  2139. unsigned int ppr_options = tinfo->goal.ppr_options;
  2140. unsigned int dt;
  2141. unsigned long flags;
  2142. unsigned long offset = tinfo->goal.offset;
  2143. #ifdef AHD_DEBUG
  2144. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2145. printk("%s: set period to %d\n", ahd_name(ahd), period);
  2146. #endif
  2147. if (offset == 0)
  2148. offset = MAX_OFFSET;
  2149. if (period < 8)
  2150. period = 8;
  2151. if (period < 10) {
  2152. if (spi_max_width(starget)) {
  2153. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2154. if (period == 8)
  2155. ppr_options |= MSG_EXT_PPR_IU_REQ;
  2156. } else
  2157. period = 10;
  2158. }
  2159. dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2160. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2161. starget->channel + 'A', ROLE_INITIATOR);
  2162. /* all PPR requests apart from QAS require wide transfers */
  2163. if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
  2164. if (spi_width(starget) == 0)
  2165. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2166. }
  2167. ahd_find_syncrate(ahd, &period, &ppr_options,
  2168. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2169. ahd_lock(ahd, &flags);
  2170. ahd_set_syncrate(ahd, &devinfo, period, offset,
  2171. ppr_options, AHD_TRANS_GOAL, FALSE);
  2172. ahd_unlock(ahd, &flags);
  2173. }
  2174. static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
  2175. {
  2176. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2177. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2178. struct ahd_tmode_tstate *tstate;
  2179. struct ahd_initiator_tinfo *tinfo
  2180. = ahd_fetch_transinfo(ahd,
  2181. starget->channel + 'A',
  2182. shost->this_id, starget->id, &tstate);
  2183. struct ahd_devinfo devinfo;
  2184. unsigned int ppr_options = 0;
  2185. unsigned int period = 0;
  2186. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2187. unsigned long flags;
  2188. #ifdef AHD_DEBUG
  2189. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2190. printk("%s: set offset to %d\n", ahd_name(ahd), offset);
  2191. #endif
  2192. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2193. starget->channel + 'A', ROLE_INITIATOR);
  2194. if (offset != 0) {
  2195. period = tinfo->goal.period;
  2196. ppr_options = tinfo->goal.ppr_options;
  2197. ahd_find_syncrate(ahd, &period, &ppr_options,
  2198. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2199. }
  2200. ahd_lock(ahd, &flags);
  2201. ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
  2202. AHD_TRANS_GOAL, FALSE);
  2203. ahd_unlock(ahd, &flags);
  2204. }
  2205. static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
  2206. {
  2207. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2208. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2209. struct ahd_tmode_tstate *tstate;
  2210. struct ahd_initiator_tinfo *tinfo
  2211. = ahd_fetch_transinfo(ahd,
  2212. starget->channel + 'A',
  2213. shost->this_id, starget->id, &tstate);
  2214. struct ahd_devinfo devinfo;
  2215. unsigned int ppr_options = tinfo->goal.ppr_options
  2216. & ~MSG_EXT_PPR_DT_REQ;
  2217. unsigned int period = tinfo->goal.period;
  2218. unsigned int width = tinfo->goal.width;
  2219. unsigned long flags;
  2220. #ifdef AHD_DEBUG
  2221. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2222. printk("%s: %s DT\n", ahd_name(ahd),
  2223. dt ? "enabling" : "disabling");
  2224. #endif
  2225. if (dt && spi_max_width(starget)) {
  2226. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2227. if (!width)
  2228. ahd_linux_set_width(starget, 1);
  2229. } else {
  2230. if (period <= 9)
  2231. period = 10; /* If resetting DT, period must be >= 25ns */
  2232. /* IU is invalid without DT set */
  2233. ppr_options &= ~MSG_EXT_PPR_IU_REQ;
  2234. }
  2235. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2236. starget->channel + 'A', ROLE_INITIATOR);
  2237. ahd_find_syncrate(ahd, &period, &ppr_options,
  2238. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2239. ahd_lock(ahd, &flags);
  2240. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2241. ppr_options, AHD_TRANS_GOAL, FALSE);
  2242. ahd_unlock(ahd, &flags);
  2243. }
  2244. static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
  2245. {
  2246. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2247. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2248. struct ahd_tmode_tstate *tstate;
  2249. struct ahd_initiator_tinfo *tinfo
  2250. = ahd_fetch_transinfo(ahd,
  2251. starget->channel + 'A',
  2252. shost->this_id, starget->id, &tstate);
  2253. struct ahd_devinfo devinfo;
  2254. unsigned int ppr_options = tinfo->goal.ppr_options
  2255. & ~MSG_EXT_PPR_QAS_REQ;
  2256. unsigned int period = tinfo->goal.period;
  2257. unsigned int dt;
  2258. unsigned long flags;
  2259. #ifdef AHD_DEBUG
  2260. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2261. printk("%s: %s QAS\n", ahd_name(ahd),
  2262. qas ? "enabling" : "disabling");
  2263. #endif
  2264. if (qas) {
  2265. ppr_options |= MSG_EXT_PPR_QAS_REQ;
  2266. }
  2267. dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2268. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2269. starget->channel + 'A', ROLE_INITIATOR);
  2270. ahd_find_syncrate(ahd, &period, &ppr_options,
  2271. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2272. ahd_lock(ahd, &flags);
  2273. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2274. ppr_options, AHD_TRANS_GOAL, FALSE);
  2275. ahd_unlock(ahd, &flags);
  2276. }
  2277. static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
  2278. {
  2279. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2280. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2281. struct ahd_tmode_tstate *tstate;
  2282. struct ahd_initiator_tinfo *tinfo
  2283. = ahd_fetch_transinfo(ahd,
  2284. starget->channel + 'A',
  2285. shost->this_id, starget->id, &tstate);
  2286. struct ahd_devinfo devinfo;
  2287. unsigned int ppr_options = tinfo->goal.ppr_options
  2288. & ~MSG_EXT_PPR_IU_REQ;
  2289. unsigned int period = tinfo->goal.period;
  2290. unsigned int dt;
  2291. unsigned long flags;
  2292. #ifdef AHD_DEBUG
  2293. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2294. printk("%s: %s IU\n", ahd_name(ahd),
  2295. iu ? "enabling" : "disabling");
  2296. #endif
  2297. if (iu && spi_max_width(starget)) {
  2298. ppr_options |= MSG_EXT_PPR_IU_REQ;
  2299. ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
  2300. }
  2301. dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2302. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2303. starget->channel + 'A', ROLE_INITIATOR);
  2304. ahd_find_syncrate(ahd, &period, &ppr_options,
  2305. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2306. ahd_lock(ahd, &flags);
  2307. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2308. ppr_options, AHD_TRANS_GOAL, FALSE);
  2309. ahd_unlock(ahd, &flags);
  2310. }
  2311. static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
  2312. {
  2313. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2314. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2315. struct ahd_tmode_tstate *tstate;
  2316. struct ahd_initiator_tinfo *tinfo
  2317. = ahd_fetch_transinfo(ahd,
  2318. starget->channel + 'A',
  2319. shost->this_id, starget->id, &tstate);
  2320. struct ahd_devinfo devinfo;
  2321. unsigned int ppr_options = tinfo->goal.ppr_options
  2322. & ~MSG_EXT_PPR_RD_STRM;
  2323. unsigned int period = tinfo->goal.period;
  2324. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2325. unsigned long flags;
  2326. #ifdef AHD_DEBUG
  2327. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2328. printk("%s: %s Read Streaming\n", ahd_name(ahd),
  2329. rdstrm ? "enabling" : "disabling");
  2330. #endif
  2331. if (rdstrm && spi_max_width(starget))
  2332. ppr_options |= MSG_EXT_PPR_RD_STRM;
  2333. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2334. starget->channel + 'A', ROLE_INITIATOR);
  2335. ahd_find_syncrate(ahd, &period, &ppr_options,
  2336. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2337. ahd_lock(ahd, &flags);
  2338. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2339. ppr_options, AHD_TRANS_GOAL, FALSE);
  2340. ahd_unlock(ahd, &flags);
  2341. }
  2342. static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
  2343. {
  2344. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2345. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2346. struct ahd_tmode_tstate *tstate;
  2347. struct ahd_initiator_tinfo *tinfo
  2348. = ahd_fetch_transinfo(ahd,
  2349. starget->channel + 'A',
  2350. shost->this_id, starget->id, &tstate);
  2351. struct ahd_devinfo devinfo;
  2352. unsigned int ppr_options = tinfo->goal.ppr_options
  2353. & ~MSG_EXT_PPR_WR_FLOW;
  2354. unsigned int period = tinfo->goal.period;
  2355. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2356. unsigned long flags;
  2357. #ifdef AHD_DEBUG
  2358. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2359. printk("%s: %s Write Flow Control\n", ahd_name(ahd),
  2360. wrflow ? "enabling" : "disabling");
  2361. #endif
  2362. if (wrflow && spi_max_width(starget))
  2363. ppr_options |= MSG_EXT_PPR_WR_FLOW;
  2364. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2365. starget->channel + 'A', ROLE_INITIATOR);
  2366. ahd_find_syncrate(ahd, &period, &ppr_options,
  2367. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2368. ahd_lock(ahd, &flags);
  2369. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2370. ppr_options, AHD_TRANS_GOAL, FALSE);
  2371. ahd_unlock(ahd, &flags);
  2372. }
  2373. static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
  2374. {
  2375. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2376. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2377. struct ahd_tmode_tstate *tstate;
  2378. struct ahd_initiator_tinfo *tinfo
  2379. = ahd_fetch_transinfo(ahd,
  2380. starget->channel + 'A',
  2381. shost->this_id, starget->id, &tstate);
  2382. struct ahd_devinfo devinfo;
  2383. unsigned int ppr_options = tinfo->goal.ppr_options
  2384. & ~MSG_EXT_PPR_RTI;
  2385. unsigned int period = tinfo->goal.period;
  2386. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2387. unsigned long flags;
  2388. if ((ahd->features & AHD_RTI) == 0) {
  2389. #ifdef AHD_DEBUG
  2390. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2391. printk("%s: RTI not available\n", ahd_name(ahd));
  2392. #endif
  2393. return;
  2394. }
  2395. #ifdef AHD_DEBUG
  2396. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2397. printk("%s: %s RTI\n", ahd_name(ahd),
  2398. rti ? "enabling" : "disabling");
  2399. #endif
  2400. if (rti && spi_max_width(starget))
  2401. ppr_options |= MSG_EXT_PPR_RTI;
  2402. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2403. starget->channel + 'A', ROLE_INITIATOR);
  2404. ahd_find_syncrate(ahd, &period, &ppr_options,
  2405. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2406. ahd_lock(ahd, &flags);
  2407. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2408. ppr_options, AHD_TRANS_GOAL, FALSE);
  2409. ahd_unlock(ahd, &flags);
  2410. }
  2411. static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
  2412. {
  2413. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2414. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2415. struct ahd_tmode_tstate *tstate;
  2416. struct ahd_initiator_tinfo *tinfo
  2417. = ahd_fetch_transinfo(ahd,
  2418. starget->channel + 'A',
  2419. shost->this_id, starget->id, &tstate);
  2420. struct ahd_devinfo devinfo;
  2421. unsigned int ppr_options = tinfo->goal.ppr_options
  2422. & ~MSG_EXT_PPR_PCOMP_EN;
  2423. unsigned int period = tinfo->goal.period;
  2424. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2425. unsigned long flags;
  2426. #ifdef AHD_DEBUG
  2427. if ((ahd_debug & AHD_SHOW_DV) != 0)
  2428. printk("%s: %s Precompensation\n", ahd_name(ahd),
  2429. pcomp ? "Enable" : "Disable");
  2430. #endif
  2431. if (pcomp && spi_max_width(starget)) {
  2432. uint8_t precomp;
  2433. if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
  2434. const struct ahd_linux_iocell_opts *iocell_opts;
  2435. iocell_opts = &aic79xx_iocell_info[ahd->unit];
  2436. precomp = iocell_opts->precomp;
  2437. } else {
  2438. precomp = AIC79XX_DEFAULT_PRECOMP;
  2439. }
  2440. ppr_options |= MSG_EXT_PPR_PCOMP_EN;
  2441. AHD_SET_PRECOMP(ahd, precomp);
  2442. } else {
  2443. AHD_SET_PRECOMP(ahd, 0);
  2444. }
  2445. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2446. starget->channel + 'A', ROLE_INITIATOR);
  2447. ahd_find_syncrate(ahd, &period, &ppr_options,
  2448. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2449. ahd_lock(ahd, &flags);
  2450. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2451. ppr_options, AHD_TRANS_GOAL, FALSE);
  2452. ahd_unlock(ahd, &flags);
  2453. }
  2454. static void ahd_linux_set_hold_mcs(struct scsi_target *starget, int hold)
  2455. {
  2456. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2457. struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
  2458. struct ahd_tmode_tstate *tstate;
  2459. struct ahd_initiator_tinfo *tinfo
  2460. = ahd_fetch_transinfo(ahd,
  2461. starget->channel + 'A',
  2462. shost->this_id, starget->id, &tstate);
  2463. struct ahd_devinfo devinfo;
  2464. unsigned int ppr_options = tinfo->goal.ppr_options
  2465. & ~MSG_EXT_PPR_HOLD_MCS;
  2466. unsigned int period = tinfo->goal.period;
  2467. unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
  2468. unsigned long flags;
  2469. if (hold && spi_max_width(starget))
  2470. ppr_options |= MSG_EXT_PPR_HOLD_MCS;
  2471. ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2472. starget->channel + 'A', ROLE_INITIATOR);
  2473. ahd_find_syncrate(ahd, &period, &ppr_options,
  2474. dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
  2475. ahd_lock(ahd, &flags);
  2476. ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
  2477. ppr_options, AHD_TRANS_GOAL, FALSE);
  2478. ahd_unlock(ahd, &flags);
  2479. }
  2480. static void ahd_linux_get_signalling(struct Scsi_Host *shost)
  2481. {
  2482. struct ahd_softc *ahd = *(struct ahd_softc **)shost->hostdata;
  2483. unsigned long flags;
  2484. u8 mode;
  2485. ahd_lock(ahd, &flags);
  2486. ahd_pause(ahd);
  2487. mode = ahd_inb(ahd, SBLKCTL);
  2488. ahd_unpause(ahd);
  2489. ahd_unlock(ahd, &flags);
  2490. if (mode & ENAB40)
  2491. spi_signalling(shost) = SPI_SIGNAL_LVD;
  2492. else if (mode & ENAB20)
  2493. spi_signalling(shost) = SPI_SIGNAL_SE;
  2494. else
  2495. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  2496. }
  2497. static struct spi_function_template ahd_linux_transport_functions = {
  2498. .set_offset = ahd_linux_set_offset,
  2499. .show_offset = 1,
  2500. .set_period = ahd_linux_set_period,
  2501. .show_period = 1,
  2502. .set_width = ahd_linux_set_width,
  2503. .show_width = 1,
  2504. .set_dt = ahd_linux_set_dt,
  2505. .show_dt = 1,
  2506. .set_iu = ahd_linux_set_iu,
  2507. .show_iu = 1,
  2508. .set_qas = ahd_linux_set_qas,
  2509. .show_qas = 1,
  2510. .set_rd_strm = ahd_linux_set_rd_strm,
  2511. .show_rd_strm = 1,
  2512. .set_wr_flow = ahd_linux_set_wr_flow,
  2513. .show_wr_flow = 1,
  2514. .set_rti = ahd_linux_set_rti,
  2515. .show_rti = 1,
  2516. .set_pcomp_en = ahd_linux_set_pcomp_en,
  2517. .show_pcomp_en = 1,
  2518. .set_hold_mcs = ahd_linux_set_hold_mcs,
  2519. .show_hold_mcs = 1,
  2520. .get_signalling = ahd_linux_get_signalling,
  2521. };
  2522. static int __init
  2523. ahd_linux_init(void)
  2524. {
  2525. int error = 0;
  2526. /*
  2527. * If we've been passed any parameters, process them now.
  2528. */
  2529. if (aic79xx)
  2530. aic79xx_setup(aic79xx);
  2531. ahd_linux_transport_template =
  2532. spi_attach_transport(&ahd_linux_transport_functions);
  2533. if (!ahd_linux_transport_template)
  2534. return -ENODEV;
  2535. scsi_transport_reserve_device(ahd_linux_transport_template,
  2536. sizeof(struct ahd_linux_device));
  2537. error = ahd_linux_pci_init();
  2538. if (error)
  2539. spi_release_transport(ahd_linux_transport_template);
  2540. return error;
  2541. }
  2542. static void __exit
  2543. ahd_linux_exit(void)
  2544. {
  2545. ahd_linux_pci_exit();
  2546. spi_release_transport(ahd_linux_transport_template);
  2547. }
  2548. module_init(ahd_linux_init);
  2549. module_exit(ahd_linux_exit);