sym_glue.c 55 KB

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