sym_glue.c 54 KB

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