sym_glue.c 54 KB

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