sym_glue.c 54 KB

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