sym_glue.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. /*
  2. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  3. * of PCI-SCSI IO processors.
  4. *
  5. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  6. * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
  7. *
  8. * This driver is derived from the Linux sym53c8xx driver.
  9. * Copyright (C) 1998-2000 Gerard Roudier
  10. *
  11. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  12. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  13. *
  14. * The original ncr driver has been written for 386bsd and FreeBSD by
  15. * Wolfgang Stanglmeier <wolf@cologne.de>
  16. * Stefan Esser <se@mi.Uni-Koeln.de>
  17. * Copyright (C) 1994 Wolfgang Stanglmeier
  18. *
  19. * Other major contributions:
  20. *
  21. * NVRAM detection and reading.
  22. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  23. *
  24. *-----------------------------------------------------------------------------
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include <linux/ctype.h>
  41. #include <linux/init.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/spinlock.h>
  46. #include <scsi/scsi.h>
  47. #include <scsi/scsi_tcq.h>
  48. #include <scsi/scsi_device.h>
  49. #include <scsi/scsi_transport.h>
  50. #include "sym_glue.h"
  51. #include "sym_nvram.h"
  52. #define NAME53C "sym53c"
  53. #define NAME53C8XX "sym53c8xx"
  54. /* SPARC just has to be different ... */
  55. #ifdef __sparc__
  56. #define IRQ_FMT "%s"
  57. #define IRQ_PRM(x) __irq_itoa(x)
  58. #else
  59. #define IRQ_FMT "%d"
  60. #define IRQ_PRM(x) (x)
  61. #endif
  62. struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
  63. unsigned int sym_debug_flags = 0;
  64. static char *excl_string;
  65. static char *safe_string;
  66. module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
  67. module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
  68. module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
  69. module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
  70. module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
  71. module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
  72. module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
  73. module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
  74. module_param_named(verb, sym_driver_setup.verbose, byte, 0);
  75. module_param_named(debug, sym_debug_flags, uint, 0);
  76. module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
  77. module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
  78. module_param_named(excl, excl_string, charp, 0);
  79. module_param_named(safe, safe_string, charp, 0);
  80. MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
  81. MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
  82. MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
  83. MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
  84. MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
  85. MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
  86. MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
  87. MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
  88. MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
  89. MODULE_PARM_DESC(debug, "Set bits to enable debugging");
  90. MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
  91. MODULE_PARM_DESC(nvram, "Option currently not used");
  92. MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
  93. MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
  94. MODULE_LICENSE("GPL");
  95. MODULE_VERSION(SYM_VERSION);
  96. MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
  97. MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
  98. static void sym2_setup_params(void)
  99. {
  100. char *p = excl_string;
  101. int xi = 0;
  102. while (p && (xi < 8)) {
  103. char *next_p;
  104. int val = (int) simple_strtoul(p, &next_p, 0);
  105. sym_driver_setup.excludes[xi++] = val;
  106. p = next_p;
  107. }
  108. if (safe_string) {
  109. if (*safe_string == 'y') {
  110. sym_driver_setup.max_tag = 0;
  111. sym_driver_setup.burst_order = 0;
  112. sym_driver_setup.scsi_led = 0;
  113. sym_driver_setup.scsi_diff = 1;
  114. sym_driver_setup.irq_mode = 0;
  115. sym_driver_setup.scsi_bus_check = 2;
  116. sym_driver_setup.host_id = 7;
  117. sym_driver_setup.verbose = 2;
  118. sym_driver_setup.settle_delay = 10;
  119. sym_driver_setup.use_nvram = 1;
  120. } else if (*safe_string != 'n') {
  121. printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
  122. " passed to safe option", safe_string);
  123. }
  124. }
  125. }
  126. /*
  127. * We used to try to deal with 64-bit BARs here, but don't any more.
  128. * There are many parts of this driver which would need to be modified
  129. * to handle a 64-bit base address, including scripts. I'm uncomfortable
  130. * with making those changes when I have no way of testing it, so I'm
  131. * just going to disable it.
  132. *
  133. * Note that some machines (eg HP rx8620 and Superdome) have bus addresses
  134. * below 4GB and physical addresses above 4GB. These will continue to work.
  135. */
  136. static int __devinit
  137. pci_get_base_address(struct pci_dev *pdev, int index, unsigned long *basep)
  138. {
  139. u32 tmp;
  140. unsigned long base;
  141. #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
  142. pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
  143. base = tmp;
  144. if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
  145. pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
  146. if (tmp > 0) {
  147. dev_err(&pdev->dev,
  148. "BAR %d is 64-bit, disabling\n", index - 1);
  149. base = 0;
  150. }
  151. }
  152. if ((base & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  153. base &= PCI_BASE_ADDRESS_IO_MASK;
  154. } else {
  155. base &= PCI_BASE_ADDRESS_MEM_MASK;
  156. }
  157. *basep = base;
  158. return index;
  159. #undef PCI_BAR_OFFSET
  160. }
  161. static struct scsi_transport_template *sym2_transport_template = NULL;
  162. /*
  163. * Used by the eh thread to wait for command completion.
  164. * It is allocated on the eh thread stack.
  165. */
  166. struct sym_eh_wait {
  167. struct completion done;
  168. struct timer_list timer;
  169. void (*old_done)(struct scsi_cmnd *);
  170. int to_do;
  171. int timed_out;
  172. };
  173. /*
  174. * Driver private area in the SCSI command structure.
  175. */
  176. struct sym_ucmd { /* Override the SCSI pointer structure */
  177. dma_addr_t data_mapping;
  178. u_char data_mapped;
  179. struct sym_eh_wait *eh_wait;
  180. };
  181. #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
  182. #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
  183. static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
  184. {
  185. int dma_dir = cmd->sc_data_direction;
  186. switch(SYM_UCMD_PTR(cmd)->data_mapped) {
  187. case 2:
  188. pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
  189. break;
  190. case 1:
  191. pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
  192. cmd->request_bufflen, dma_dir);
  193. break;
  194. }
  195. SYM_UCMD_PTR(cmd)->data_mapped = 0;
  196. }
  197. static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
  198. {
  199. dma_addr_t mapping;
  200. int dma_dir = cmd->sc_data_direction;
  201. mapping = pci_map_single(pdev, cmd->request_buffer,
  202. cmd->request_bufflen, dma_dir);
  203. if (mapping) {
  204. SYM_UCMD_PTR(cmd)->data_mapped = 1;
  205. SYM_UCMD_PTR(cmd)->data_mapping = mapping;
  206. }
  207. return mapping;
  208. }
  209. static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
  210. {
  211. int use_sg;
  212. int dma_dir = cmd->sc_data_direction;
  213. use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
  214. if (use_sg > 0) {
  215. SYM_UCMD_PTR(cmd)->data_mapped = 2;
  216. SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
  217. }
  218. return use_sg;
  219. }
  220. #define unmap_scsi_data(np, cmd) \
  221. __unmap_scsi_data(np->s.device, cmd)
  222. #define map_scsi_single_data(np, cmd) \
  223. __map_scsi_single_data(np->s.device, cmd)
  224. #define map_scsi_sg_data(np, cmd) \
  225. __map_scsi_sg_data(np->s.device, cmd)
  226. /*
  227. * Complete a pending CAM CCB.
  228. */
  229. void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
  230. {
  231. unmap_scsi_data(np, cmd);
  232. cmd->scsi_done(cmd);
  233. }
  234. static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *cmd, int cam_status)
  235. {
  236. sym_set_cam_status(cmd, cam_status);
  237. sym_xpt_done(np, cmd);
  238. }
  239. /*
  240. * Tell the SCSI layer about a BUS RESET.
  241. */
  242. void sym_xpt_async_bus_reset(struct sym_hcb *np)
  243. {
  244. printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
  245. np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
  246. np->s.settle_time_valid = 1;
  247. if (sym_verbose >= 2)
  248. printf_info("%s: command processing suspended for %d seconds\n",
  249. sym_name(np), sym_driver_setup.settle_delay);
  250. }
  251. /*
  252. * Tell the SCSI layer about a BUS DEVICE RESET message sent.
  253. */
  254. void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
  255. {
  256. printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
  257. }
  258. /*
  259. * Choose the more appropriate CAM status if
  260. * the IO encountered an extended error.
  261. */
  262. static int sym_xerr_cam_status(int cam_status, int x_status)
  263. {
  264. if (x_status) {
  265. if (x_status & XE_PARITY_ERR)
  266. cam_status = DID_PARITY;
  267. else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
  268. cam_status = DID_ERROR;
  269. else if (x_status & XE_BAD_PHASE)
  270. cam_status = DID_ERROR;
  271. else
  272. cam_status = DID_ERROR;
  273. }
  274. return cam_status;
  275. }
  276. /*
  277. * Build CAM result for a failed or auto-sensed IO.
  278. */
  279. void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
  280. {
  281. struct scsi_cmnd *cmd = cp->cmd;
  282. u_int cam_status, scsi_status, drv_status;
  283. drv_status = 0;
  284. cam_status = DID_OK;
  285. scsi_status = cp->ssss_status;
  286. if (cp->host_flags & HF_SENSE) {
  287. scsi_status = cp->sv_scsi_status;
  288. resid = cp->sv_resid;
  289. if (sym_verbose && cp->sv_xerr_status)
  290. sym_print_xerr(cmd, cp->sv_xerr_status);
  291. if (cp->host_status == HS_COMPLETE &&
  292. cp->ssss_status == S_GOOD &&
  293. cp->xerr_status == 0) {
  294. cam_status = sym_xerr_cam_status(DID_OK,
  295. cp->sv_xerr_status);
  296. drv_status = DRIVER_SENSE;
  297. /*
  298. * Bounce back the sense data to user.
  299. */
  300. memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  301. memcpy(cmd->sense_buffer, cp->sns_bbuf,
  302. min(sizeof(cmd->sense_buffer),
  303. (size_t)SYM_SNS_BBUF_LEN));
  304. #if 0
  305. /*
  306. * If the device reports a UNIT ATTENTION condition
  307. * due to a RESET condition, we should consider all
  308. * disconnect CCBs for this unit as aborted.
  309. */
  310. if (1) {
  311. u_char *p;
  312. p = (u_char *) cmd->sense_data;
  313. if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
  314. sym_clear_tasks(np, DID_ABORT,
  315. cp->target,cp->lun, -1);
  316. }
  317. #endif
  318. } else {
  319. /*
  320. * Error return from our internal request sense. This
  321. * is bad: we must clear the contingent allegiance
  322. * condition otherwise the device will always return
  323. * BUSY. Use a big stick.
  324. */
  325. sym_reset_scsi_target(np, cmd->device->id);
  326. cam_status = DID_ERROR;
  327. }
  328. } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
  329. cam_status = DID_OK;
  330. else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
  331. cam_status = DID_NO_CONNECT;
  332. else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
  333. cam_status = DID_ERROR;
  334. else { /* Extended error */
  335. if (sym_verbose) {
  336. sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
  337. cp->host_status, cp->ssss_status,
  338. cp->xerr_status);
  339. }
  340. /*
  341. * Set the most appropriate value for CAM status.
  342. */
  343. cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
  344. }
  345. cmd->resid = resid;
  346. cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
  347. }
  348. /*
  349. * Build the scatter/gather array for an I/O.
  350. */
  351. static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
  352. {
  353. struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
  354. int segment;
  355. unsigned int len = cmd->request_bufflen;
  356. if (len) {
  357. dma_addr_t baddr = map_scsi_single_data(np, cmd);
  358. if (baddr) {
  359. if (len & 1) {
  360. struct sym_tcb *tp = &np->target[cp->target];
  361. if (tp->head.wval & EWS) {
  362. len++;
  363. cp->odd_byte_adjustment++;
  364. }
  365. }
  366. cp->data_len = len;
  367. sym_build_sge(np, data, baddr, len);
  368. segment = 1;
  369. } else {
  370. segment = -2;
  371. }
  372. } else {
  373. segment = 0;
  374. }
  375. return segment;
  376. }
  377. static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
  378. {
  379. int segment;
  380. int use_sg = (int) cmd->use_sg;
  381. cp->data_len = 0;
  382. if (!use_sg)
  383. segment = sym_scatter_no_sglist(np, cp, cmd);
  384. else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
  385. struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
  386. struct sym_tcb *tp = &np->target[cp->target];
  387. struct sym_tblmove *data;
  388. if (use_sg > SYM_CONF_MAX_SG) {
  389. unmap_scsi_data(np, cmd);
  390. return -1;
  391. }
  392. data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
  393. for (segment = 0; segment < use_sg; segment++) {
  394. dma_addr_t baddr = sg_dma_address(&scatter[segment]);
  395. unsigned int len = sg_dma_len(&scatter[segment]);
  396. if ((len & 1) && (tp->head.wval & EWS)) {
  397. len++;
  398. cp->odd_byte_adjustment++;
  399. }
  400. sym_build_sge(np, &data[segment], baddr, len);
  401. cp->data_len += len;
  402. }
  403. } else {
  404. segment = -2;
  405. }
  406. return segment;
  407. }
  408. /*
  409. * Queue a SCSI command.
  410. */
  411. static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
  412. {
  413. struct scsi_device *sdev = cmd->device;
  414. struct sym_tcb *tp;
  415. struct sym_lcb *lp;
  416. struct sym_ccb *cp;
  417. int order;
  418. /*
  419. * Minimal checkings, so that we will not
  420. * go outside our tables.
  421. */
  422. if (sdev->id == np->myaddr) {
  423. sym_xpt_done2(np, cmd, DID_NO_CONNECT);
  424. return 0;
  425. }
  426. /*
  427. * Retrieve the target descriptor.
  428. */
  429. tp = &np->target[sdev->id];
  430. /*
  431. * Select tagged/untagged.
  432. */
  433. lp = sym_lp(tp, sdev->lun);
  434. order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
  435. /*
  436. * Queue the SCSI IO.
  437. */
  438. cp = sym_get_ccb(np, cmd, order);
  439. if (!cp)
  440. return 1; /* Means resource shortage */
  441. sym_queue_scsiio(np, cmd, cp);
  442. return 0;
  443. }
  444. /*
  445. * Setup buffers and pointers that address the CDB.
  446. */
  447. static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  448. {
  449. memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
  450. cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
  451. cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
  452. return 0;
  453. }
  454. /*
  455. * Setup pointers that address the data and start the I/O.
  456. */
  457. int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  458. {
  459. struct sym_tcb *tp = &np->target[cp->target];
  460. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  461. u32 lastp, goalp;
  462. int dir;
  463. /*
  464. * Build the CDB.
  465. */
  466. if (sym_setup_cdb(np, cmd, cp))
  467. goto out_abort;
  468. /*
  469. * No direction means no data.
  470. */
  471. dir = cmd->sc_data_direction;
  472. if (dir != DMA_NONE) {
  473. cp->segments = sym_scatter(np, cp, cmd);
  474. if (cp->segments < 0) {
  475. sym_set_cam_status(cmd, DID_ERROR);
  476. goto out_abort;
  477. }
  478. /*
  479. * No segments means no data.
  480. */
  481. if (!cp->segments)
  482. dir = DMA_NONE;
  483. } else {
  484. cp->data_len = 0;
  485. cp->segments = 0;
  486. }
  487. /*
  488. * Set the data pointer.
  489. */
  490. switch (dir) {
  491. case DMA_BIDIRECTIONAL:
  492. printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
  493. sym_set_cam_status(cmd, DID_ERROR);
  494. goto out_abort;
  495. case DMA_TO_DEVICE:
  496. goalp = SCRIPTA_BA(np, data_out2) + 8;
  497. lastp = goalp - 8 - (cp->segments * (2*4));
  498. break;
  499. case DMA_FROM_DEVICE:
  500. cp->host_flags |= HF_DATA_IN;
  501. goalp = SCRIPTA_BA(np, data_in2) + 8;
  502. lastp = goalp - 8 - (cp->segments * (2*4));
  503. break;
  504. case DMA_NONE:
  505. default:
  506. lastp = goalp = SCRIPTB_BA(np, no_data);
  507. break;
  508. }
  509. /*
  510. * Set all pointers values needed by SCRIPTS.
  511. */
  512. cp->phys.head.lastp = cpu_to_scr(lastp);
  513. cp->phys.head.savep = cpu_to_scr(lastp);
  514. cp->startp = cp->phys.head.savep;
  515. cp->goalp = cpu_to_scr(goalp);
  516. /*
  517. * When `#ifed 1', the code below makes the driver
  518. * panic on the first attempt to write to a SCSI device.
  519. * It is the first test we want to do after a driver
  520. * change that does not seem obviously safe. :)
  521. */
  522. #if 0
  523. switch (cp->cdb_buf[0]) {
  524. case 0x0A: case 0x2A: case 0xAA:
  525. panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
  526. break;
  527. default:
  528. break;
  529. }
  530. #endif
  531. /*
  532. * activate this job.
  533. */
  534. sym_start_next_ccbs(np, lp, 2);
  535. return 0;
  536. out_abort:
  537. sym_free_ccb(np, cp);
  538. sym_xpt_done(np, cmd);
  539. return 0;
  540. }
  541. /*
  542. * timer daemon.
  543. *
  544. * Misused to keep the driver running when
  545. * interrupts are not configured correctly.
  546. */
  547. static void sym_timer(struct sym_hcb *np)
  548. {
  549. unsigned long thistime = jiffies;
  550. /*
  551. * Restart the timer.
  552. */
  553. np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
  554. add_timer(&np->s.timer);
  555. /*
  556. * If we are resetting the ncr, wait for settle_time before
  557. * clearing it. Then command processing will be resumed.
  558. */
  559. if (np->s.settle_time_valid) {
  560. if (time_before_eq(np->s.settle_time, thistime)) {
  561. if (sym_verbose >= 2 )
  562. printk("%s: command processing resumed\n",
  563. sym_name(np));
  564. np->s.settle_time_valid = 0;
  565. }
  566. return;
  567. }
  568. /*
  569. * Nothing to do for now, but that may come.
  570. */
  571. if (np->s.lasttime + 4*HZ < thistime) {
  572. np->s.lasttime = thistime;
  573. }
  574. #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
  575. /*
  576. * Some way-broken PCI bridges may lead to
  577. * completions being lost when the clearing
  578. * of the INTFLY flag by the CPU occurs
  579. * concurrently with the chip raising this flag.
  580. * If this ever happen, lost completions will
  581. * be reaped here.
  582. */
  583. sym_wakeup_done(np);
  584. #endif
  585. }
  586. /*
  587. * PCI BUS error handler.
  588. */
  589. void sym_log_bus_error(struct sym_hcb *np)
  590. {
  591. u_short pci_sts;
  592. pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
  593. if (pci_sts & 0xf900) {
  594. pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
  595. printf("%s: PCI STATUS = 0x%04x\n",
  596. sym_name(np), pci_sts & 0xf900);
  597. }
  598. }
  599. /*
  600. * queuecommand method. Entered with the host adapter lock held and
  601. * interrupts disabled.
  602. */
  603. static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
  604. void (*done)(struct scsi_cmnd *))
  605. {
  606. struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
  607. struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
  608. int sts = 0;
  609. cmd->scsi_done = done;
  610. memset(ucp, 0, sizeof(*ucp));
  611. /*
  612. * Shorten our settle_time if needed for
  613. * this command not to time out.
  614. */
  615. if (np->s.settle_time_valid && cmd->timeout_per_command) {
  616. unsigned long tlimit = jiffies + cmd->timeout_per_command;
  617. tlimit -= SYM_CONF_TIMER_INTERVAL*2;
  618. if (time_after(np->s.settle_time, tlimit)) {
  619. np->s.settle_time = tlimit;
  620. }
  621. }
  622. if (np->s.settle_time_valid)
  623. return SCSI_MLQUEUE_HOST_BUSY;
  624. sts = sym_queue_command(np, cmd);
  625. if (sts)
  626. return SCSI_MLQUEUE_HOST_BUSY;
  627. return 0;
  628. }
  629. /*
  630. * Linux entry point of the interrupt handler.
  631. */
  632. static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
  633. {
  634. unsigned long flags;
  635. struct sym_hcb *np = (struct sym_hcb *)dev_id;
  636. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
  637. spin_lock_irqsave(np->s.host->host_lock, flags);
  638. sym_interrupt(np);
  639. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  640. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
  641. return IRQ_HANDLED;
  642. }
  643. /*
  644. * Linux entry point of the timer handler
  645. */
  646. static void sym53c8xx_timer(unsigned long npref)
  647. {
  648. struct sym_hcb *np = (struct sym_hcb *)npref;
  649. unsigned long flags;
  650. spin_lock_irqsave(np->s.host->host_lock, flags);
  651. sym_timer(np);
  652. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  653. }
  654. /*
  655. * What the eh thread wants us to perform.
  656. */
  657. #define SYM_EH_ABORT 0
  658. #define SYM_EH_DEVICE_RESET 1
  659. #define SYM_EH_BUS_RESET 2
  660. #define SYM_EH_HOST_RESET 3
  661. /*
  662. * What we will do regarding the involved SCSI command.
  663. */
  664. #define SYM_EH_DO_IGNORE 0
  665. #define SYM_EH_DO_COMPLETE 1
  666. #define SYM_EH_DO_WAIT 2
  667. /*
  668. * Our general completion handler.
  669. */
  670. static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
  671. {
  672. struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
  673. if (!ep)
  674. return;
  675. /* Try to avoid a race here (not 100% safe) */
  676. if (!timed_out) {
  677. ep->timed_out = 0;
  678. if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
  679. return;
  680. }
  681. /* Revert everything */
  682. SYM_UCMD_PTR(cmd)->eh_wait = NULL;
  683. cmd->scsi_done = ep->old_done;
  684. /* Wake up the eh thread if it wants to sleep */
  685. if (ep->to_do == SYM_EH_DO_WAIT)
  686. complete(&ep->done);
  687. }
  688. /*
  689. * scsi_done() alias when error recovery is in progress.
  690. */
  691. static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
  692. /*
  693. * Some timeout handler to avoid waiting too long.
  694. */
  695. static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
  696. /*
  697. * Generic method for our eh processing.
  698. * The 'op' argument tells what we have to do.
  699. */
  700. static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
  701. {
  702. struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
  703. SYM_QUEHEAD *qp;
  704. int to_do = SYM_EH_DO_IGNORE;
  705. int sts = -1;
  706. struct sym_eh_wait eh, *ep = &eh;
  707. dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
  708. /* This one is queued in some place -> to wait for completion */
  709. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  710. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  711. if (cp->cmd == cmd) {
  712. to_do = SYM_EH_DO_WAIT;
  713. goto prepare;
  714. }
  715. }
  716. prepare:
  717. /* Prepare stuff to either ignore, complete or wait for completion */
  718. switch(to_do) {
  719. default:
  720. case SYM_EH_DO_IGNORE:
  721. break;
  722. case SYM_EH_DO_WAIT:
  723. init_completion(&ep->done);
  724. /* fall through */
  725. case SYM_EH_DO_COMPLETE:
  726. ep->old_done = cmd->scsi_done;
  727. cmd->scsi_done = sym_eh_done;
  728. SYM_UCMD_PTR(cmd)->eh_wait = ep;
  729. }
  730. /* Try to proceed the operation we have been asked for */
  731. sts = -1;
  732. switch(op) {
  733. case SYM_EH_ABORT:
  734. sts = sym_abort_scsiio(np, cmd, 1);
  735. break;
  736. case SYM_EH_DEVICE_RESET:
  737. sts = sym_reset_scsi_target(np, cmd->device->id);
  738. break;
  739. case SYM_EH_BUS_RESET:
  740. sym_reset_scsi_bus(np, 1);
  741. sts = 0;
  742. break;
  743. case SYM_EH_HOST_RESET:
  744. sym_reset_scsi_bus(np, 0);
  745. sym_start_up (np, 1);
  746. sts = 0;
  747. break;
  748. default:
  749. break;
  750. }
  751. /* On error, restore everything and cross fingers :) */
  752. if (sts) {
  753. SYM_UCMD_PTR(cmd)->eh_wait = NULL;
  754. cmd->scsi_done = ep->old_done;
  755. to_do = SYM_EH_DO_IGNORE;
  756. }
  757. ep->to_do = to_do;
  758. /* Complete the command with locks held as required by the driver */
  759. if (to_do == SYM_EH_DO_COMPLETE)
  760. sym_xpt_done2(np, cmd, DID_ABORT);
  761. /* Wait for completion with locks released, as required by kernel */
  762. if (to_do == SYM_EH_DO_WAIT) {
  763. init_timer(&ep->timer);
  764. ep->timer.expires = jiffies + (5*HZ);
  765. ep->timer.function = sym_eh_timeout;
  766. ep->timer.data = (u_long)cmd;
  767. ep->timed_out = 1; /* Be pessimistic for once :) */
  768. add_timer(&ep->timer);
  769. spin_unlock_irq(np->s.host->host_lock);
  770. wait_for_completion(&ep->done);
  771. spin_lock_irq(np->s.host->host_lock);
  772. if (ep->timed_out)
  773. sts = -2;
  774. }
  775. dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
  776. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  777. return sts ? SCSI_FAILED : SCSI_SUCCESS;
  778. }
  779. /*
  780. * Error handlers called from the eh thread (one thread per HBA).
  781. */
  782. static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
  783. {
  784. int rc;
  785. spin_lock_irq(cmd->device->host->host_lock);
  786. rc = sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
  787. spin_unlock_irq(cmd->device->host->host_lock);
  788. return rc;
  789. }
  790. static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
  791. {
  792. int rc;
  793. spin_lock_irq(cmd->device->host->host_lock);
  794. rc = sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
  795. spin_unlock_irq(cmd->device->host->host_lock);
  796. return rc;
  797. }
  798. static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  799. {
  800. int rc;
  801. spin_lock_irq(cmd->device->host->host_lock);
  802. rc = sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
  803. spin_unlock_irq(cmd->device->host->host_lock);
  804. return rc;
  805. }
  806. static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
  807. {
  808. int rc;
  809. spin_lock_irq(cmd->device->host->host_lock);
  810. rc = sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
  811. spin_unlock_irq(cmd->device->host->host_lock);
  812. return rc;
  813. }
  814. /*
  815. * Tune device queuing depth, according to various limits.
  816. */
  817. static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
  818. {
  819. struct sym_lcb *lp = sym_lp(tp, lun);
  820. u_short oldtags;
  821. if (!lp)
  822. return;
  823. oldtags = lp->s.reqtags;
  824. if (reqtags > lp->s.scdev_depth)
  825. reqtags = lp->s.scdev_depth;
  826. lp->started_limit = reqtags ? reqtags : 2;
  827. lp->started_max = 1;
  828. lp->s.reqtags = reqtags;
  829. if (reqtags != oldtags) {
  830. dev_info(&tp->starget->dev,
  831. "tagged command queuing %s, command queue depth %d.\n",
  832. lp->s.reqtags ? "enabled" : "disabled",
  833. lp->started_limit);
  834. }
  835. }
  836. /*
  837. * Linux select queue depths function
  838. */
  839. #define DEF_DEPTH (sym_driver_setup.max_tag)
  840. #define ALL_TARGETS -2
  841. #define NO_TARGET -1
  842. #define ALL_LUNS -2
  843. #define NO_LUN -1
  844. static int device_queue_depth(struct sym_hcb *np, int target, int lun)
  845. {
  846. int c, h, t, u, v;
  847. char *p = sym_driver_setup.tag_ctrl;
  848. char *ep;
  849. h = -1;
  850. t = NO_TARGET;
  851. u = NO_LUN;
  852. while ((c = *p++) != 0) {
  853. v = simple_strtoul(p, &ep, 0);
  854. switch(c) {
  855. case '/':
  856. ++h;
  857. t = ALL_TARGETS;
  858. u = ALL_LUNS;
  859. break;
  860. case 't':
  861. if (t != target)
  862. t = (target == v) ? v : NO_TARGET;
  863. u = ALL_LUNS;
  864. break;
  865. case 'u':
  866. if (u != lun)
  867. u = (lun == v) ? v : NO_LUN;
  868. break;
  869. case 'q':
  870. if (h == np->s.unit &&
  871. (t == ALL_TARGETS || t == target) &&
  872. (u == ALL_LUNS || u == lun))
  873. return v;
  874. break;
  875. case '-':
  876. t = ALL_TARGETS;
  877. u = ALL_LUNS;
  878. break;
  879. default:
  880. break;
  881. }
  882. p = ep;
  883. }
  884. return DEF_DEPTH;
  885. }
  886. static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
  887. {
  888. struct sym_hcb *np = sym_get_hcb(sdev->host);
  889. struct sym_tcb *tp = &np->target[sdev->id];
  890. struct sym_lcb *lp;
  891. if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
  892. return -ENXIO;
  893. tp->starget = sdev->sdev_target;
  894. /*
  895. * Fail the device init if the device is flagged NOSCAN at BOOT in
  896. * the NVRAM. This may speed up boot and maintain coherency with
  897. * BIOS device numbering. Clearing the flag allows the user to
  898. * rescan skipped devices later. We also return an error for
  899. * devices not flagged for SCAN LUNS in the NVRAM since some single
  900. * lun devices behave badly when asked for a non zero LUN.
  901. */
  902. if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
  903. tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
  904. starget_printk(KERN_INFO, tp->starget,
  905. "Scan at boot disabled in NVRAM\n");
  906. return -ENXIO;
  907. }
  908. if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
  909. if (sdev->lun != 0)
  910. return -ENXIO;
  911. starget_printk(KERN_INFO, tp->starget,
  912. "Multiple LUNs disabled in NVRAM\n");
  913. }
  914. lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
  915. if (!lp)
  916. return -ENOMEM;
  917. spi_min_period(tp->starget) = tp->usr_period;
  918. spi_max_width(tp->starget) = tp->usr_width;
  919. return 0;
  920. }
  921. /*
  922. * Linux entry point for device queue sizing.
  923. */
  924. static int sym53c8xx_slave_configure(struct scsi_device *sdev)
  925. {
  926. struct sym_hcb *np = sym_get_hcb(sdev->host);
  927. struct sym_tcb *tp = &np->target[sdev->id];
  928. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  929. int reqtags, depth_to_use;
  930. /*
  931. * Get user flags.
  932. */
  933. lp->curr_flags = lp->user_flags;
  934. /*
  935. * Select queue depth from driver setup.
  936. * Donnot use more than configured by user.
  937. * Use at least 2.
  938. * Donnot use more than our maximum.
  939. */
  940. reqtags = device_queue_depth(np, sdev->id, sdev->lun);
  941. if (reqtags > tp->usrtags)
  942. reqtags = tp->usrtags;
  943. if (!sdev->tagged_supported)
  944. reqtags = 0;
  945. #if 1 /* Avoid to locally queue commands for no good reasons */
  946. if (reqtags > SYM_CONF_MAX_TAG)
  947. reqtags = SYM_CONF_MAX_TAG;
  948. depth_to_use = (reqtags ? reqtags : 2);
  949. #else
  950. depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
  951. #endif
  952. scsi_adjust_queue_depth(sdev,
  953. (sdev->tagged_supported ?
  954. MSG_SIMPLE_TAG : 0),
  955. depth_to_use);
  956. lp->s.scdev_depth = depth_to_use;
  957. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  958. if (!spi_initial_dv(sdev->sdev_target))
  959. spi_dv_device(sdev);
  960. return 0;
  961. }
  962. static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
  963. {
  964. struct sym_hcb *np = sym_get_hcb(sdev->host);
  965. struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
  966. if (lp->itlq_tbl)
  967. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
  968. kfree(lp->cb_tags);
  969. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  970. }
  971. /*
  972. * Linux entry point for info() function
  973. */
  974. static const char *sym53c8xx_info (struct Scsi_Host *host)
  975. {
  976. return SYM_DRIVER_NAME;
  977. }
  978. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  979. /*
  980. * Proc file system stuff
  981. *
  982. * A read operation returns adapter information.
  983. * A write operation is a control command.
  984. * The string is parsed in the driver code and the command is passed
  985. * to the sym_usercmd() function.
  986. */
  987. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  988. struct sym_usrcmd {
  989. u_long target;
  990. u_long lun;
  991. u_long data;
  992. u_long cmd;
  993. };
  994. #define UC_SETSYNC 10
  995. #define UC_SETTAGS 11
  996. #define UC_SETDEBUG 12
  997. #define UC_SETWIDE 14
  998. #define UC_SETFLAG 15
  999. #define UC_SETVERBOSE 17
  1000. #define UC_RESETDEV 18
  1001. #define UC_CLEARDEV 19
  1002. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  1003. {
  1004. struct sym_tcb *tp;
  1005. int t, l;
  1006. switch (uc->cmd) {
  1007. case 0: return;
  1008. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  1009. case UC_SETDEBUG:
  1010. sym_debug_flags = uc->data;
  1011. break;
  1012. #endif
  1013. case UC_SETVERBOSE:
  1014. np->verbose = uc->data;
  1015. break;
  1016. default:
  1017. /*
  1018. * We assume that other commands apply to targets.
  1019. * This should always be the case and avoid the below
  1020. * 4 lines to be repeated 6 times.
  1021. */
  1022. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  1023. if (!((uc->target >> t) & 1))
  1024. continue;
  1025. tp = &np->target[t];
  1026. switch (uc->cmd) {
  1027. case UC_SETSYNC:
  1028. if (!uc->data || uc->data >= 255) {
  1029. tp->tgoal.iu = tp->tgoal.dt =
  1030. tp->tgoal.qas = 0;
  1031. tp->tgoal.offset = 0;
  1032. } else if (uc->data <= 9 && np->minsync_dt) {
  1033. if (uc->data < np->minsync_dt)
  1034. uc->data = np->minsync_dt;
  1035. tp->tgoal.iu = tp->tgoal.dt =
  1036. tp->tgoal.qas = 1;
  1037. tp->tgoal.width = 1;
  1038. tp->tgoal.period = uc->data;
  1039. tp->tgoal.offset = np->maxoffs_dt;
  1040. } else {
  1041. if (uc->data < np->minsync)
  1042. uc->data = np->minsync;
  1043. tp->tgoal.iu = tp->tgoal.dt =
  1044. tp->tgoal.qas = 0;
  1045. tp->tgoal.period = uc->data;
  1046. tp->tgoal.offset = np->maxoffs;
  1047. }
  1048. tp->tgoal.check_nego = 1;
  1049. break;
  1050. case UC_SETWIDE:
  1051. tp->tgoal.width = uc->data ? 1 : 0;
  1052. tp->tgoal.check_nego = 1;
  1053. break;
  1054. case UC_SETTAGS:
  1055. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  1056. sym_tune_dev_queuing(tp, l, uc->data);
  1057. break;
  1058. case UC_RESETDEV:
  1059. tp->to_reset = 1;
  1060. np->istat_sem = SEM;
  1061. OUTB(np, nc_istat, SIGP|SEM);
  1062. break;
  1063. case UC_CLEARDEV:
  1064. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  1065. struct sym_lcb *lp = sym_lp(tp, l);
  1066. if (lp) lp->to_clear = 1;
  1067. }
  1068. np->istat_sem = SEM;
  1069. OUTB(np, nc_istat, SIGP|SEM);
  1070. break;
  1071. case UC_SETFLAG:
  1072. tp->usrflags = uc->data;
  1073. break;
  1074. }
  1075. }
  1076. break;
  1077. }
  1078. }
  1079. static int skip_spaces(char *ptr, int len)
  1080. {
  1081. int cnt, c;
  1082. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  1083. return (len - cnt);
  1084. }
  1085. static int get_int_arg(char *ptr, int len, u_long *pv)
  1086. {
  1087. char *end;
  1088. *pv = simple_strtoul(ptr, &end, 10);
  1089. return (end - ptr);
  1090. }
  1091. static int is_keyword(char *ptr, int len, char *verb)
  1092. {
  1093. int verb_len = strlen(verb);
  1094. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  1095. return verb_len;
  1096. else
  1097. return 0;
  1098. }
  1099. #define SKIP_SPACES(ptr, len) \
  1100. if ((arg_len = skip_spaces(ptr, len)) < 1) \
  1101. return -EINVAL; \
  1102. ptr += arg_len; len -= arg_len;
  1103. #define GET_INT_ARG(ptr, len, v) \
  1104. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  1105. return -EINVAL; \
  1106. ptr += arg_len; len -= arg_len;
  1107. /*
  1108. * Parse a control command
  1109. */
  1110. static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
  1111. {
  1112. char *ptr = buffer;
  1113. int len = length;
  1114. struct sym_usrcmd cmd, *uc = &cmd;
  1115. int arg_len;
  1116. u_long target;
  1117. memset(uc, 0, sizeof(*uc));
  1118. if (len > 0 && ptr[len-1] == '\n')
  1119. --len;
  1120. if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
  1121. uc->cmd = UC_SETSYNC;
  1122. else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
  1123. uc->cmd = UC_SETTAGS;
  1124. else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
  1125. uc->cmd = UC_SETVERBOSE;
  1126. else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
  1127. uc->cmd = UC_SETWIDE;
  1128. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  1129. else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
  1130. uc->cmd = UC_SETDEBUG;
  1131. #endif
  1132. else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
  1133. uc->cmd = UC_SETFLAG;
  1134. else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
  1135. uc->cmd = UC_RESETDEV;
  1136. else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
  1137. uc->cmd = UC_CLEARDEV;
  1138. else
  1139. arg_len = 0;
  1140. #ifdef DEBUG_PROC_INFO
  1141. printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
  1142. #endif
  1143. if (!arg_len)
  1144. return -EINVAL;
  1145. ptr += arg_len; len -= arg_len;
  1146. switch(uc->cmd) {
  1147. case UC_SETSYNC:
  1148. case UC_SETTAGS:
  1149. case UC_SETWIDE:
  1150. case UC_SETFLAG:
  1151. case UC_RESETDEV:
  1152. case UC_CLEARDEV:
  1153. SKIP_SPACES(ptr, len);
  1154. if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
  1155. ptr += arg_len; len -= arg_len;
  1156. uc->target = ~0;
  1157. } else {
  1158. GET_INT_ARG(ptr, len, target);
  1159. uc->target = (1<<target);
  1160. #ifdef DEBUG_PROC_INFO
  1161. printk("sym_user_command: target=%ld\n", target);
  1162. #endif
  1163. }
  1164. break;
  1165. }
  1166. switch(uc->cmd) {
  1167. case UC_SETVERBOSE:
  1168. case UC_SETSYNC:
  1169. case UC_SETTAGS:
  1170. case UC_SETWIDE:
  1171. SKIP_SPACES(ptr, len);
  1172. GET_INT_ARG(ptr, len, uc->data);
  1173. #ifdef DEBUG_PROC_INFO
  1174. printk("sym_user_command: data=%ld\n", uc->data);
  1175. #endif
  1176. break;
  1177. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  1178. case UC_SETDEBUG:
  1179. while (len > 0) {
  1180. SKIP_SPACES(ptr, len);
  1181. if ((arg_len = is_keyword(ptr, len, "alloc")))
  1182. uc->data |= DEBUG_ALLOC;
  1183. else if ((arg_len = is_keyword(ptr, len, "phase")))
  1184. uc->data |= DEBUG_PHASE;
  1185. else if ((arg_len = is_keyword(ptr, len, "queue")))
  1186. uc->data |= DEBUG_QUEUE;
  1187. else if ((arg_len = is_keyword(ptr, len, "result")))
  1188. uc->data |= DEBUG_RESULT;
  1189. else if ((arg_len = is_keyword(ptr, len, "scatter")))
  1190. uc->data |= DEBUG_SCATTER;
  1191. else if ((arg_len = is_keyword(ptr, len, "script")))
  1192. uc->data |= DEBUG_SCRIPT;
  1193. else if ((arg_len = is_keyword(ptr, len, "tiny")))
  1194. uc->data |= DEBUG_TINY;
  1195. else if ((arg_len = is_keyword(ptr, len, "timing")))
  1196. uc->data |= DEBUG_TIMING;
  1197. else if ((arg_len = is_keyword(ptr, len, "nego")))
  1198. uc->data |= DEBUG_NEGO;
  1199. else if ((arg_len = is_keyword(ptr, len, "tags")))
  1200. uc->data |= DEBUG_TAGS;
  1201. else if ((arg_len = is_keyword(ptr, len, "pointer")))
  1202. uc->data |= DEBUG_POINTER;
  1203. else
  1204. return -EINVAL;
  1205. ptr += arg_len; len -= arg_len;
  1206. }
  1207. #ifdef DEBUG_PROC_INFO
  1208. printk("sym_user_command: data=%ld\n", uc->data);
  1209. #endif
  1210. break;
  1211. #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
  1212. case UC_SETFLAG:
  1213. while (len > 0) {
  1214. SKIP_SPACES(ptr, len);
  1215. if ((arg_len = is_keyword(ptr, len, "no_disc")))
  1216. uc->data &= ~SYM_DISC_ENABLED;
  1217. else
  1218. return -EINVAL;
  1219. ptr += arg_len; len -= arg_len;
  1220. }
  1221. break;
  1222. default:
  1223. break;
  1224. }
  1225. if (len)
  1226. return -EINVAL;
  1227. else {
  1228. unsigned long flags;
  1229. spin_lock_irqsave(np->s.host->host_lock, flags);
  1230. sym_exec_user_command (np, uc);
  1231. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  1232. }
  1233. return length;
  1234. }
  1235. #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
  1236. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1237. /*
  1238. * Informations through the proc file system.
  1239. */
  1240. struct info_str {
  1241. char *buffer;
  1242. int length;
  1243. int offset;
  1244. int pos;
  1245. };
  1246. static void copy_mem_info(struct info_str *info, char *data, int len)
  1247. {
  1248. if (info->pos + len > info->length)
  1249. len = info->length - info->pos;
  1250. if (info->pos + len < info->offset) {
  1251. info->pos += len;
  1252. return;
  1253. }
  1254. if (info->pos < info->offset) {
  1255. data += (info->offset - info->pos);
  1256. len -= (info->offset - info->pos);
  1257. }
  1258. if (len > 0) {
  1259. memcpy(info->buffer + info->pos, data, len);
  1260. info->pos += len;
  1261. }
  1262. }
  1263. static int copy_info(struct info_str *info, char *fmt, ...)
  1264. {
  1265. va_list args;
  1266. char buf[81];
  1267. int len;
  1268. va_start(args, fmt);
  1269. len = vsprintf(buf, fmt, args);
  1270. va_end(args);
  1271. copy_mem_info(info, buf, len);
  1272. return len;
  1273. }
  1274. /*
  1275. * Copy formatted information into the input buffer.
  1276. */
  1277. static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
  1278. {
  1279. struct info_str info;
  1280. info.buffer = ptr;
  1281. info.length = len;
  1282. info.offset = offset;
  1283. info.pos = 0;
  1284. copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
  1285. "revision id 0x%x\n",
  1286. np->s.chip_name, np->device_id, np->revision_id);
  1287. copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
  1288. pci_name(np->s.device), IRQ_PRM(np->s.irq));
  1289. copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
  1290. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1291. np->maxwide ? "Wide" : "Narrow",
  1292. np->minsync_dt ? ", DT capable" : "");
  1293. copy_info(&info, "Max. started commands %d, "
  1294. "max. commands per LUN %d\n",
  1295. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1296. return info.pos > info.offset? info.pos - info.offset : 0;
  1297. }
  1298. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1299. /*
  1300. * Entry point of the scsi proc fs of the driver.
  1301. * - func = 0 means read (returns adapter infos)
  1302. * - func = 1 means write (not yet merget from sym53c8xx)
  1303. */
  1304. static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
  1305. char **start, off_t offset, int length, int func)
  1306. {
  1307. struct sym_hcb *np = sym_get_hcb(host);
  1308. int retv;
  1309. if (func) {
  1310. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1311. retv = sym_user_command(np, buffer, length);
  1312. #else
  1313. retv = -EINVAL;
  1314. #endif
  1315. } else {
  1316. if (start)
  1317. *start = buffer;
  1318. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1319. retv = sym_host_info(np, buffer, offset, length);
  1320. #else
  1321. retv = -EINVAL;
  1322. #endif
  1323. }
  1324. return retv;
  1325. }
  1326. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1327. /*
  1328. * Free controller resources.
  1329. */
  1330. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
  1331. {
  1332. /*
  1333. * Free O/S specific resources.
  1334. */
  1335. if (np->s.irq)
  1336. free_irq(np->s.irq, np);
  1337. if (np->s.ioaddr)
  1338. pci_iounmap(pdev, np->s.ioaddr);
  1339. if (np->s.ramaddr)
  1340. pci_iounmap(pdev, np->s.ramaddr);
  1341. /*
  1342. * Free O/S independent resources.
  1343. */
  1344. sym_hcb_free(np);
  1345. sym_mfree_dma(np, sizeof(*np), "HCB");
  1346. }
  1347. /*
  1348. * Ask/tell the system about DMA addressing.
  1349. */
  1350. static int sym_setup_bus_dma_mask(struct sym_hcb *np)
  1351. {
  1352. #if SYM_CONF_DMA_ADDRESSING_MODE > 0
  1353. #if SYM_CONF_DMA_ADDRESSING_MODE == 1
  1354. #define DMA_DAC_MASK DMA_40BIT_MASK
  1355. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  1356. #define DMA_DAC_MASK DMA_64BIT_MASK
  1357. #endif
  1358. if ((np->features & FE_DAC) &&
  1359. !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
  1360. np->use_dac = 1;
  1361. return 0;
  1362. }
  1363. #endif
  1364. if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
  1365. return 0;
  1366. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1367. return -1;
  1368. }
  1369. /*
  1370. * Host attach and initialisations.
  1371. *
  1372. * Allocate host data and ncb structure.
  1373. * Remap MMIO region.
  1374. * Do chip initialization.
  1375. * If all is OK, install interrupt handling and
  1376. * start the timer daemon.
  1377. */
  1378. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1379. int unit, struct sym_device *dev)
  1380. {
  1381. struct host_data *host_data;
  1382. struct sym_hcb *np = NULL;
  1383. struct Scsi_Host *instance = NULL;
  1384. struct pci_dev *pdev = dev->pdev;
  1385. unsigned long flags;
  1386. struct sym_fw *fw;
  1387. printk(KERN_INFO
  1388. "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
  1389. unit, dev->chip.name, dev->chip.revision_id,
  1390. pci_name(pdev), IRQ_PRM(pdev->irq));
  1391. /*
  1392. * Get the firmware for this chip.
  1393. */
  1394. fw = sym_find_firmware(&dev->chip);
  1395. if (!fw)
  1396. goto attach_failed;
  1397. /*
  1398. * Allocate host_data structure
  1399. */
  1400. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  1401. if (!instance)
  1402. goto attach_failed;
  1403. host_data = (struct host_data *) instance->hostdata;
  1404. /*
  1405. * Allocate immediately the host control block,
  1406. * since we are only expecting to succeed. :)
  1407. * We keep track in the HCB of all the resources that
  1408. * are to be released on error.
  1409. */
  1410. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1411. if (!np)
  1412. goto attach_failed;
  1413. np->s.device = pdev;
  1414. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1415. host_data->ncb = np;
  1416. np->s.host = instance;
  1417. pci_set_drvdata(pdev, np);
  1418. /*
  1419. * Copy some useful infos to the HCB.
  1420. */
  1421. np->hcb_ba = vtobus(np);
  1422. np->verbose = sym_driver_setup.verbose;
  1423. np->s.device = pdev;
  1424. np->s.unit = unit;
  1425. np->device_id = dev->chip.device_id;
  1426. np->revision_id = dev->chip.revision_id;
  1427. np->features = dev->chip.features;
  1428. np->clock_divn = dev->chip.nr_divisor;
  1429. np->maxoffs = dev->chip.offset_max;
  1430. np->maxburst = dev->chip.burst_max;
  1431. np->myaddr = dev->host_id;
  1432. /*
  1433. * Edit its name.
  1434. */
  1435. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1436. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1437. if (sym_setup_bus_dma_mask(np))
  1438. goto attach_failed;
  1439. /*
  1440. * Try to map the controller chip to
  1441. * virtual and physical memory.
  1442. */
  1443. np->mmio_ba = (u32)dev->mmio_base;
  1444. np->s.ioaddr = dev->s.ioaddr;
  1445. np->s.ramaddr = dev->s.ramaddr;
  1446. np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
  1447. /*
  1448. * Map on-chip RAM if present and supported.
  1449. */
  1450. if (!(np->features & FE_RAM))
  1451. dev->ram_base = 0;
  1452. if (dev->ram_base) {
  1453. np->ram_ba = (u32)dev->ram_base;
  1454. np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
  1455. }
  1456. if (sym_hcb_attach(instance, fw, dev->nvram))
  1457. goto attach_failed;
  1458. /*
  1459. * Install the interrupt handler.
  1460. * If we synchonize the C code with SCRIPTS on interrupt,
  1461. * we do not want to share the INTR line at all.
  1462. */
  1463. if (request_irq(pdev->irq, sym53c8xx_intr, SA_SHIRQ, NAME53C8XX, np)) {
  1464. printf_err("%s: request irq %d failure\n",
  1465. sym_name(np), pdev->irq);
  1466. goto attach_failed;
  1467. }
  1468. np->s.irq = pdev->irq;
  1469. /*
  1470. * After SCSI devices have been opened, we cannot
  1471. * reset the bus safely, so we do it here.
  1472. */
  1473. spin_lock_irqsave(instance->host_lock, flags);
  1474. if (sym_reset_scsi_bus(np, 0))
  1475. goto reset_failed;
  1476. /*
  1477. * Start the SCRIPTS.
  1478. */
  1479. sym_start_up (np, 1);
  1480. /*
  1481. * Start the timer daemon
  1482. */
  1483. init_timer(&np->s.timer);
  1484. np->s.timer.data = (unsigned long) np;
  1485. np->s.timer.function = sym53c8xx_timer;
  1486. np->s.lasttime=0;
  1487. sym_timer (np);
  1488. /*
  1489. * Fill Linux host instance structure
  1490. * and return success.
  1491. */
  1492. instance->max_channel = 0;
  1493. instance->this_id = np->myaddr;
  1494. instance->max_id = np->maxwide ? 16 : 8;
  1495. instance->max_lun = SYM_CONF_MAX_LUN;
  1496. instance->unique_id = pci_resource_start(pdev, 0);
  1497. instance->cmd_per_lun = SYM_CONF_MAX_TAG;
  1498. instance->can_queue = (SYM_CONF_MAX_START-2);
  1499. instance->sg_tablesize = SYM_CONF_MAX_SG;
  1500. instance->max_cmd_len = 16;
  1501. BUG_ON(sym2_transport_template == NULL);
  1502. instance->transportt = sym2_transport_template;
  1503. spin_unlock_irqrestore(instance->host_lock, flags);
  1504. return instance;
  1505. reset_failed:
  1506. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1507. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1508. spin_unlock_irqrestore(instance->host_lock, flags);
  1509. attach_failed:
  1510. if (!instance)
  1511. return NULL;
  1512. printf_info("%s: giving up ...\n", sym_name(np));
  1513. if (np)
  1514. sym_free_resources(np, pdev);
  1515. scsi_host_put(instance);
  1516. return NULL;
  1517. }
  1518. /*
  1519. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1520. */
  1521. #if SYM_CONF_NVRAM_SUPPORT
  1522. static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1523. {
  1524. devp->nvram = nvp;
  1525. devp->device_id = devp->chip.device_id;
  1526. nvp->type = 0;
  1527. sym_read_nvram(devp, nvp);
  1528. }
  1529. #else
  1530. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1531. {
  1532. }
  1533. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1534. static int __devinit sym_check_supported(struct sym_device *device)
  1535. {
  1536. struct sym_chip *chip;
  1537. struct pci_dev *pdev = device->pdev;
  1538. u_char revision;
  1539. unsigned long io_port = pci_resource_start(pdev, 0);
  1540. int i;
  1541. /*
  1542. * If user excluded this chip, do not initialize it.
  1543. * I hate this code so much. Must kill it.
  1544. */
  1545. if (io_port) {
  1546. for (i = 0 ; i < 8 ; i++) {
  1547. if (sym_driver_setup.excludes[i] == io_port)
  1548. return -ENODEV;
  1549. }
  1550. }
  1551. /*
  1552. * Check if the chip is supported. Then copy the chip description
  1553. * to our device structure so we can make it match the actual device
  1554. * and options.
  1555. */
  1556. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
  1557. chip = sym_lookup_chip_table(pdev->device, revision);
  1558. if (!chip) {
  1559. dev_info(&pdev->dev, "device not supported\n");
  1560. return -ENODEV;
  1561. }
  1562. memcpy(&device->chip, chip, sizeof(device->chip));
  1563. device->chip.revision_id = revision;
  1564. return 0;
  1565. }
  1566. /*
  1567. * Ignore Symbios chips controlled by various RAID controllers.
  1568. * These controllers set value 0x52414944 at RAM end - 16.
  1569. */
  1570. static int __devinit sym_check_raid(struct sym_device *device)
  1571. {
  1572. unsigned int ram_size, ram_val;
  1573. if (!device->s.ramaddr)
  1574. return 0;
  1575. if (device->chip.features & FE_RAM8K)
  1576. ram_size = 8192;
  1577. else
  1578. ram_size = 4096;
  1579. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1580. if (ram_val != 0x52414944)
  1581. return 0;
  1582. dev_info(&device->pdev->dev,
  1583. "not initializing, driven by RAID controller.\n");
  1584. return -ENODEV;
  1585. }
  1586. static int __devinit sym_set_workarounds(struct sym_device *device)
  1587. {
  1588. struct sym_chip *chip = &device->chip;
  1589. struct pci_dev *pdev = device->pdev;
  1590. u_short status_reg;
  1591. /*
  1592. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1593. * We must ensure the chip will use WRITE AND INVALIDATE.
  1594. * The revision number limit is for now arbitrary.
  1595. */
  1596. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
  1597. chip->features |= (FE_WRIE | FE_CLSE);
  1598. }
  1599. /* If the chip can do Memory Write Invalidate, enable it */
  1600. if (chip->features & FE_WRIE) {
  1601. if (pci_set_mwi(pdev))
  1602. return -ENODEV;
  1603. }
  1604. /*
  1605. * Work around for errant bit in 895A. The 66Mhz
  1606. * capable bit is set erroneously. Clear this bit.
  1607. * (Item 1 DEL 533)
  1608. *
  1609. * Make sure Config space and Features agree.
  1610. *
  1611. * Recall: writes are not normal to status register -
  1612. * write a 1 to clear and a 0 to leave unchanged.
  1613. * Can only reset bits.
  1614. */
  1615. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1616. if (chip->features & FE_66MHZ) {
  1617. if (!(status_reg & PCI_STATUS_66MHZ))
  1618. chip->features &= ~FE_66MHZ;
  1619. } else {
  1620. if (status_reg & PCI_STATUS_66MHZ) {
  1621. status_reg = PCI_STATUS_66MHZ;
  1622. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1623. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1624. }
  1625. }
  1626. return 0;
  1627. }
  1628. /*
  1629. * Read and check the PCI configuration for any detected NCR
  1630. * boards and save data for attaching after all boards have
  1631. * been detected.
  1632. */
  1633. static void __devinit
  1634. sym_init_device(struct pci_dev *pdev, struct sym_device *device)
  1635. {
  1636. int i;
  1637. device->host_id = SYM_SETUP_HOST_ID;
  1638. device->pdev = pdev;
  1639. i = pci_get_base_address(pdev, 1, &device->mmio_base);
  1640. pci_get_base_address(pdev, i, &device->ram_base);
  1641. #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
  1642. if (device->mmio_base)
  1643. device->s.ioaddr = pci_iomap(pdev, 1,
  1644. pci_resource_len(pdev, 1));
  1645. #endif
  1646. if (!device->s.ioaddr)
  1647. device->s.ioaddr = pci_iomap(pdev, 0,
  1648. pci_resource_len(pdev, 0));
  1649. if (device->ram_base)
  1650. device->s.ramaddr = pci_iomap(pdev, i,
  1651. pci_resource_len(pdev, i));
  1652. }
  1653. /*
  1654. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1655. * behind which sits a proprietary NCR memory controller and
  1656. * either four or two 53c875s as separate devices. We can tell
  1657. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1658. * be on the same bus as the memory controller. In its usual
  1659. * mode of operation, the 875s are slaved to the memory
  1660. * controller for all transfers. To operate with the Linux
  1661. * driver, the memory controller is disabled and the 875s
  1662. * freed to function independently. The only wrinkle is that
  1663. * the preset SCSI ID (which may be zero) must be read in from
  1664. * a special configuration space register of the 875.
  1665. */
  1666. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1667. {
  1668. int slot;
  1669. u8 tmp;
  1670. for (slot = 0; slot < 256; slot++) {
  1671. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1672. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1673. pci_dev_put(memc);
  1674. continue;
  1675. }
  1676. /* bit 1: allow individual 875 configuration */
  1677. pci_read_config_byte(memc, 0x44, &tmp);
  1678. if ((tmp & 0x2) == 0) {
  1679. tmp |= 0x2;
  1680. pci_write_config_byte(memc, 0x44, tmp);
  1681. }
  1682. /* bit 2: drive individual 875 interrupts to the bus */
  1683. pci_read_config_byte(memc, 0x45, &tmp);
  1684. if ((tmp & 0x4) == 0) {
  1685. tmp |= 0x4;
  1686. pci_write_config_byte(memc, 0x45, tmp);
  1687. }
  1688. pci_dev_put(memc);
  1689. break;
  1690. }
  1691. pci_read_config_byte(pdev, 0x84, &tmp);
  1692. sym_dev->host_id = tmp;
  1693. }
  1694. /*
  1695. * Called before unloading the module.
  1696. * Detach the host.
  1697. * We have to free resources and halt the NCR chip.
  1698. */
  1699. static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
  1700. {
  1701. printk("%s: detaching ...\n", sym_name(np));
  1702. del_timer_sync(&np->s.timer);
  1703. /*
  1704. * Reset NCR chip.
  1705. * We should use sym_soft_reset(), but we don't want to do
  1706. * so, since we may not be safe if interrupts occur.
  1707. */
  1708. printk("%s: resetting chip\n", sym_name(np));
  1709. OUTB(np, nc_istat, SRST);
  1710. INB(np, nc_mbox1);
  1711. udelay(10);
  1712. OUTB(np, nc_istat, 0);
  1713. sym_free_resources(np, pdev);
  1714. return 1;
  1715. }
  1716. /*
  1717. * Driver host template.
  1718. */
  1719. static struct scsi_host_template sym2_template = {
  1720. .module = THIS_MODULE,
  1721. .name = "sym53c8xx",
  1722. .info = sym53c8xx_info,
  1723. .queuecommand = sym53c8xx_queue_command,
  1724. .slave_alloc = sym53c8xx_slave_alloc,
  1725. .slave_configure = sym53c8xx_slave_configure,
  1726. .slave_destroy = sym53c8xx_slave_destroy,
  1727. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1728. .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
  1729. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1730. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1731. .this_id = 7,
  1732. .use_clustering = DISABLE_CLUSTERING,
  1733. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1734. .proc_info = sym53c8xx_proc_info,
  1735. .proc_name = NAME53C8XX,
  1736. #endif
  1737. };
  1738. static int attach_count;
  1739. static int __devinit sym2_probe(struct pci_dev *pdev,
  1740. const struct pci_device_id *ent)
  1741. {
  1742. struct sym_device sym_dev;
  1743. struct sym_nvram nvram;
  1744. struct Scsi_Host *instance;
  1745. memset(&sym_dev, 0, sizeof(sym_dev));
  1746. memset(&nvram, 0, sizeof(nvram));
  1747. if (pci_enable_device(pdev))
  1748. goto leave;
  1749. pci_set_master(pdev);
  1750. if (pci_request_regions(pdev, NAME53C8XX))
  1751. goto disable;
  1752. sym_init_device(pdev, &sym_dev);
  1753. if (sym_check_supported(&sym_dev))
  1754. goto free;
  1755. if (sym_check_raid(&sym_dev))
  1756. goto leave; /* Don't disable the device */
  1757. if (sym_set_workarounds(&sym_dev))
  1758. goto free;
  1759. sym_config_pqs(pdev, &sym_dev);
  1760. sym_get_nvram(&sym_dev, &nvram);
  1761. instance = sym_attach(&sym2_template, attach_count, &sym_dev);
  1762. if (!instance)
  1763. goto free;
  1764. if (scsi_add_host(instance, &pdev->dev))
  1765. goto detach;
  1766. scsi_scan_host(instance);
  1767. attach_count++;
  1768. return 0;
  1769. detach:
  1770. sym_detach(pci_get_drvdata(pdev), pdev);
  1771. free:
  1772. pci_release_regions(pdev);
  1773. disable:
  1774. pci_disable_device(pdev);
  1775. leave:
  1776. return -ENODEV;
  1777. }
  1778. static void __devexit sym2_remove(struct pci_dev *pdev)
  1779. {
  1780. struct sym_hcb *np = pci_get_drvdata(pdev);
  1781. struct Scsi_Host *host = np->s.host;
  1782. scsi_remove_host(host);
  1783. scsi_host_put(host);
  1784. sym_detach(np, pdev);
  1785. pci_release_regions(pdev);
  1786. pci_disable_device(pdev);
  1787. attach_count--;
  1788. }
  1789. static void sym2_get_signalling(struct Scsi_Host *shost)
  1790. {
  1791. struct sym_hcb *np = sym_get_hcb(shost);
  1792. enum spi_signal_type type;
  1793. switch (np->scsi_mode) {
  1794. case SMODE_SE:
  1795. type = SPI_SIGNAL_SE;
  1796. break;
  1797. case SMODE_LVD:
  1798. type = SPI_SIGNAL_LVD;
  1799. break;
  1800. case SMODE_HVD:
  1801. type = SPI_SIGNAL_HVD;
  1802. break;
  1803. default:
  1804. type = SPI_SIGNAL_UNKNOWN;
  1805. break;
  1806. }
  1807. spi_signalling(shost) = type;
  1808. }
  1809. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1810. {
  1811. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1812. struct sym_hcb *np = sym_get_hcb(shost);
  1813. struct sym_tcb *tp = &np->target[starget->id];
  1814. tp->tgoal.offset = offset;
  1815. tp->tgoal.check_nego = 1;
  1816. }
  1817. static void sym2_set_period(struct scsi_target *starget, int period)
  1818. {
  1819. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1820. struct sym_hcb *np = sym_get_hcb(shost);
  1821. struct sym_tcb *tp = &np->target[starget->id];
  1822. /* have to have DT for these transfers, but DT will also
  1823. * set width, so check that this is allowed */
  1824. if (period <= np->minsync && spi_width(starget))
  1825. tp->tgoal.dt = 1;
  1826. tp->tgoal.period = period;
  1827. tp->tgoal.check_nego = 1;
  1828. }
  1829. static void sym2_set_width(struct scsi_target *starget, int width)
  1830. {
  1831. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1832. struct sym_hcb *np = sym_get_hcb(shost);
  1833. struct sym_tcb *tp = &np->target[starget->id];
  1834. /* It is illegal to have DT set on narrow transfers. If DT is
  1835. * clear, we must also clear IU and QAS. */
  1836. if (width == 0)
  1837. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1838. tp->tgoal.width = width;
  1839. tp->tgoal.check_nego = 1;
  1840. }
  1841. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1842. {
  1843. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1844. struct sym_hcb *np = sym_get_hcb(shost);
  1845. struct sym_tcb *tp = &np->target[starget->id];
  1846. /* We must clear QAS and IU if DT is clear */
  1847. if (dt)
  1848. tp->tgoal.dt = 1;
  1849. else
  1850. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1851. tp->tgoal.check_nego = 1;
  1852. }
  1853. #if 0
  1854. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1855. {
  1856. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1857. struct sym_hcb *np = sym_get_hcb(shost);
  1858. struct sym_tcb *tp = &np->target[starget->id];
  1859. if (iu)
  1860. tp->tgoal.iu = tp->tgoal.dt = 1;
  1861. else
  1862. tp->tgoal.iu = 0;
  1863. tp->tgoal.check_nego = 1;
  1864. }
  1865. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1866. {
  1867. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1868. struct sym_hcb *np = sym_get_hcb(shost);
  1869. struct sym_tcb *tp = &np->target[starget->id];
  1870. if (qas)
  1871. tp->tgoal.dt = tp->tgoal.qas = 1;
  1872. else
  1873. tp->tgoal.qas = 0;
  1874. tp->tgoal.check_nego = 1;
  1875. }
  1876. #endif
  1877. static struct spi_function_template sym2_transport_functions = {
  1878. .set_offset = sym2_set_offset,
  1879. .show_offset = 1,
  1880. .set_period = sym2_set_period,
  1881. .show_period = 1,
  1882. .set_width = sym2_set_width,
  1883. .show_width = 1,
  1884. .set_dt = sym2_set_dt,
  1885. .show_dt = 1,
  1886. #if 0
  1887. .set_iu = sym2_set_iu,
  1888. .show_iu = 1,
  1889. .set_qas = sym2_set_qas,
  1890. .show_qas = 1,
  1891. #endif
  1892. .get_signalling = sym2_get_signalling,
  1893. };
  1894. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1895. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1896. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1897. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1898. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1899. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1900. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1901. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1902. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1903. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1904. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1905. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1906. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1907. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1908. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1909. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1910. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1911. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1912. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1913. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1914. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1915. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1916. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1917. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1918. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1919. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1920. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1921. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1922. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1923. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1924. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1925. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1926. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1927. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1928. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1929. { 0, }
  1930. };
  1931. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1932. static struct pci_driver sym2_driver = {
  1933. .name = NAME53C8XX,
  1934. .id_table = sym2_id_table,
  1935. .probe = sym2_probe,
  1936. .remove = __devexit_p(sym2_remove),
  1937. };
  1938. static int __init sym2_init(void)
  1939. {
  1940. int error;
  1941. sym2_setup_params();
  1942. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1943. if (!sym2_transport_template)
  1944. return -ENODEV;
  1945. error = pci_register_driver(&sym2_driver);
  1946. if (error)
  1947. spi_release_transport(sym2_transport_template);
  1948. return error;
  1949. }
  1950. static void __exit sym2_exit(void)
  1951. {
  1952. pci_unregister_driver(&sym2_driver);
  1953. spi_release_transport(sym2_transport_template);
  1954. }
  1955. module_init(sym2_init);
  1956. module_exit(sym2_exit);