qla_os.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  1. /*
  2. * QLOGIC LINUX SOFTWARE
  3. *
  4. * QLogic ISP2x00 device driver for Linux 2.6.x
  5. * Copyright (C) 2003-2005 QLogic Corporation
  6. * (www.qlogic.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include "qla_def.h"
  20. #include <linux/moduleparam.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/delay.h>
  24. #include <scsi/scsi_tcq.h>
  25. #include <scsi/scsicam.h>
  26. #include <scsi/scsi_transport.h>
  27. #include <scsi/scsi_transport_fc.h>
  28. /*
  29. * Driver version
  30. */
  31. char qla2x00_version_str[40];
  32. /*
  33. * SRB allocation cache
  34. */
  35. static kmem_cache_t *srb_cachep;
  36. /*
  37. * Ioctl related information.
  38. */
  39. static int num_hosts;
  40. int ql2xlogintimeout = 20;
  41. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  42. MODULE_PARM_DESC(ql2xlogintimeout,
  43. "Login timeout value in seconds.");
  44. int qlport_down_retry = 30;
  45. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  46. MODULE_PARM_DESC(qlport_down_retry,
  47. "Maximum number of command retries to a port that returns"
  48. "a PORT-DOWN status.");
  49. int ql2xplogiabsentdevice;
  50. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  51. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  52. "Option to enable PLOGI to devices that are not present after "
  53. "a Fabric scan. This is needed for several broken switches."
  54. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  55. int ql2xenablezio = 0;
  56. module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
  57. MODULE_PARM_DESC(ql2xenablezio,
  58. "Option to enable ZIO:If 1 then enable it otherwise"
  59. " use the default set in the NVRAM."
  60. " Default is 0 : disabled");
  61. int ql2xintrdelaytimer = 10;
  62. module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
  63. MODULE_PARM_DESC(ql2xintrdelaytimer,
  64. "ZIO: Waiting time for Firmware before it generates an "
  65. "interrupt to the host to notify completion of request.");
  66. int ql2xloginretrycount = 0;
  67. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  68. MODULE_PARM_DESC(ql2xloginretrycount,
  69. "Specify an alternate value for the NVRAM login retry count.");
  70. int ql2xfwloadbin;
  71. module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
  72. MODULE_PARM_DESC(ql2xfwloadbin,
  73. "Load ISP2xxx firmware image via hotplug.");
  74. static void qla2x00_free_device(scsi_qla_host_t *);
  75. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  76. /*
  77. * SCSI host template entry points
  78. */
  79. static int qla2xxx_slave_configure(struct scsi_device * device);
  80. static int qla2xxx_slave_alloc(struct scsi_device *);
  81. static void qla2xxx_slave_destroy(struct scsi_device *);
  82. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  83. void (*fn)(struct scsi_cmnd *));
  84. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  85. void (*fn)(struct scsi_cmnd *));
  86. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  87. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  88. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  89. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  90. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  91. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  92. static struct scsi_host_template qla2x00_driver_template = {
  93. .module = THIS_MODULE,
  94. .name = "qla2xxx",
  95. .queuecommand = qla2x00_queuecommand,
  96. .eh_abort_handler = qla2xxx_eh_abort,
  97. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  98. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  99. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  100. .slave_configure = qla2xxx_slave_configure,
  101. .slave_alloc = qla2xxx_slave_alloc,
  102. .slave_destroy = qla2xxx_slave_destroy,
  103. .this_id = -1,
  104. .cmd_per_lun = 3,
  105. .use_clustering = ENABLE_CLUSTERING,
  106. .sg_tablesize = SG_ALL,
  107. /*
  108. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  109. * which equates to 0x800000 sectors.
  110. */
  111. .max_sectors = 0xFFFF,
  112. };
  113. static struct scsi_host_template qla24xx_driver_template = {
  114. .module = THIS_MODULE,
  115. .name = "qla2xxx",
  116. .queuecommand = qla24xx_queuecommand,
  117. .eh_abort_handler = qla2xxx_eh_abort,
  118. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  119. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  120. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  121. .slave_configure = qla2xxx_slave_configure,
  122. .slave_alloc = qla2xxx_slave_alloc,
  123. .slave_destroy = qla2xxx_slave_destroy,
  124. .this_id = -1,
  125. .cmd_per_lun = 3,
  126. .use_clustering = ENABLE_CLUSTERING,
  127. .sg_tablesize = SG_ALL,
  128. .max_sectors = 0xFFFF,
  129. };
  130. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  131. /* TODO Convert to inlines
  132. *
  133. * Timer routines
  134. */
  135. #define WATCH_INTERVAL 1 /* number of seconds */
  136. static void qla2x00_timer(scsi_qla_host_t *);
  137. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  138. void *, unsigned long);
  139. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  140. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  141. static inline void
  142. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  143. {
  144. init_timer(&ha->timer);
  145. ha->timer.expires = jiffies + interval * HZ;
  146. ha->timer.data = (unsigned long)ha;
  147. ha->timer.function = (void (*)(unsigned long))func;
  148. add_timer(&ha->timer);
  149. ha->timer_active = 1;
  150. }
  151. static inline void
  152. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  153. {
  154. mod_timer(&ha->timer, jiffies + interval * HZ);
  155. }
  156. static __inline__ void
  157. qla2x00_stop_timer(scsi_qla_host_t *ha)
  158. {
  159. del_timer_sync(&ha->timer);
  160. ha->timer_active = 0;
  161. }
  162. static int qla2x00_do_dpc(void *data);
  163. static void qla2x00_rst_aen(scsi_qla_host_t *);
  164. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  165. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  166. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  167. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  168. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  169. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  170. /* -------------------------------------------------------------------------- */
  171. static char *
  172. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  173. {
  174. static char *pci_bus_modes[] = {
  175. "33", "66", "100", "133",
  176. };
  177. uint16_t pci_bus;
  178. strcpy(str, "PCI");
  179. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  180. if (pci_bus) {
  181. strcat(str, "-X (");
  182. strcat(str, pci_bus_modes[pci_bus]);
  183. } else {
  184. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  185. strcat(str, " (");
  186. strcat(str, pci_bus_modes[pci_bus]);
  187. }
  188. strcat(str, " MHz)");
  189. return (str);
  190. }
  191. static char *
  192. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  193. {
  194. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  195. uint32_t pci_bus;
  196. int pcie_reg;
  197. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  198. if (pcie_reg) {
  199. char lwstr[6];
  200. uint16_t pcie_lstat, lspeed, lwidth;
  201. pcie_reg += 0x12;
  202. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  203. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  204. lwidth = (pcie_lstat &
  205. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  206. strcpy(str, "PCIe (");
  207. if (lspeed == 1)
  208. strcat(str, "2.5Gb/s ");
  209. else
  210. strcat(str, "<unknown> ");
  211. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  212. strcat(str, lwstr);
  213. return str;
  214. }
  215. strcpy(str, "PCI");
  216. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  217. if (pci_bus == 0 || pci_bus == 8) {
  218. strcat(str, " (");
  219. strcat(str, pci_bus_modes[pci_bus >> 3]);
  220. } else {
  221. strcat(str, "-X ");
  222. if (pci_bus & BIT_2)
  223. strcat(str, "Mode 2");
  224. else
  225. strcat(str, "Mode 1");
  226. strcat(str, " (");
  227. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  228. }
  229. strcat(str, " MHz)");
  230. return str;
  231. }
  232. char *
  233. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  234. {
  235. char un_str[10];
  236. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  237. ha->fw_minor_version,
  238. ha->fw_subminor_version);
  239. if (ha->fw_attributes & BIT_9) {
  240. strcat(str, "FLX");
  241. return (str);
  242. }
  243. switch (ha->fw_attributes & 0xFF) {
  244. case 0x7:
  245. strcat(str, "EF");
  246. break;
  247. case 0x17:
  248. strcat(str, "TP");
  249. break;
  250. case 0x37:
  251. strcat(str, "IP");
  252. break;
  253. case 0x77:
  254. strcat(str, "VI");
  255. break;
  256. default:
  257. sprintf(un_str, "(%x)", ha->fw_attributes);
  258. strcat(str, un_str);
  259. break;
  260. }
  261. if (ha->fw_attributes & 0x100)
  262. strcat(str, "X");
  263. return (str);
  264. }
  265. char *
  266. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  267. {
  268. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  269. ha->fw_minor_version,
  270. ha->fw_subminor_version);
  271. if (ha->fw_attributes & BIT_0)
  272. strcat(str, "[Class 2] ");
  273. if (ha->fw_attributes & BIT_1)
  274. strcat(str, "[IP] ");
  275. if (ha->fw_attributes & BIT_2)
  276. strcat(str, "[Multi-ID] ");
  277. if (ha->fw_attributes & BIT_13)
  278. strcat(str, "[Experimental]");
  279. return str;
  280. }
  281. static inline srb_t *
  282. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  283. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  284. {
  285. srb_t *sp;
  286. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  287. if (!sp)
  288. return sp;
  289. atomic_set(&sp->ref_count, 1);
  290. sp->ha = ha;
  291. sp->fcport = fcport;
  292. sp->cmd = cmd;
  293. sp->flags = 0;
  294. CMD_SP(cmd) = (void *)sp;
  295. cmd->scsi_done = done;
  296. return sp;
  297. }
  298. static int
  299. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  300. {
  301. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  302. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  303. srb_t *sp;
  304. int rval;
  305. if (!fcport) {
  306. cmd->result = DID_NO_CONNECT << 16;
  307. goto qc_fail_command;
  308. }
  309. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  310. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  311. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  312. cmd->result = DID_NO_CONNECT << 16;
  313. goto qc_fail_command;
  314. }
  315. goto qc_host_busy;
  316. }
  317. spin_unlock_irq(ha->host->host_lock);
  318. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  319. if (!sp)
  320. goto qc_host_busy_lock;
  321. rval = qla2x00_start_scsi(sp);
  322. if (rval != QLA_SUCCESS)
  323. goto qc_host_busy_free_sp;
  324. /* Manage unprocessed RIO/ZIO commands in response queue. */
  325. if (ha->flags.online && ha->flags.process_response_queue &&
  326. ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
  327. unsigned long flags;
  328. spin_lock_irqsave(&ha->hardware_lock, flags);
  329. qla2x00_process_response_queue(ha);
  330. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  331. }
  332. spin_lock_irq(ha->host->host_lock);
  333. return 0;
  334. qc_host_busy_free_sp:
  335. qla2x00_sp_free_dma(ha, sp);
  336. mempool_free(sp, ha->srb_mempool);
  337. qc_host_busy_lock:
  338. spin_lock_irq(ha->host->host_lock);
  339. qc_host_busy:
  340. return SCSI_MLQUEUE_HOST_BUSY;
  341. qc_fail_command:
  342. done(cmd);
  343. return 0;
  344. }
  345. static int
  346. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  347. {
  348. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  349. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  350. srb_t *sp;
  351. int rval;
  352. if (!fcport) {
  353. cmd->result = DID_NO_CONNECT << 16;
  354. goto qc24_fail_command;
  355. }
  356. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  357. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  358. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  359. cmd->result = DID_NO_CONNECT << 16;
  360. goto qc24_fail_command;
  361. }
  362. goto qc24_host_busy;
  363. }
  364. spin_unlock_irq(ha->host->host_lock);
  365. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  366. if (!sp)
  367. goto qc24_host_busy_lock;
  368. rval = qla24xx_start_scsi(sp);
  369. if (rval != QLA_SUCCESS)
  370. goto qc24_host_busy_free_sp;
  371. spin_lock_irq(ha->host->host_lock);
  372. return 0;
  373. qc24_host_busy_free_sp:
  374. qla2x00_sp_free_dma(ha, sp);
  375. mempool_free(sp, ha->srb_mempool);
  376. qc24_host_busy_lock:
  377. spin_lock_irq(ha->host->host_lock);
  378. qc24_host_busy:
  379. return SCSI_MLQUEUE_HOST_BUSY;
  380. qc24_fail_command:
  381. done(cmd);
  382. return 0;
  383. }
  384. /*
  385. * qla2x00_eh_wait_on_command
  386. * Waits for the command to be returned by the Firmware for some
  387. * max time.
  388. *
  389. * Input:
  390. * ha = actual ha whose done queue will contain the command
  391. * returned by firmware.
  392. * cmd = Scsi Command to wait on.
  393. * flag = Abort/Reset(Bus or Device Reset)
  394. *
  395. * Return:
  396. * Not Found : 0
  397. * Found : 1
  398. */
  399. static int
  400. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  401. {
  402. #define ABORT_POLLING_PERIOD HZ
  403. #define ABORT_WAIT_ITER ((10 * HZ) / (ABORT_POLLING_PERIOD))
  404. unsigned long wait_iter = ABORT_WAIT_ITER;
  405. int ret = QLA_SUCCESS;
  406. while (CMD_SP(cmd)) {
  407. set_current_state(TASK_UNINTERRUPTIBLE);
  408. schedule_timeout(ABORT_POLLING_PERIOD);
  409. if (--wait_iter)
  410. break;
  411. }
  412. if (CMD_SP(cmd))
  413. ret = QLA_FUNCTION_FAILED;
  414. return ret;
  415. }
  416. /*
  417. * qla2x00_wait_for_hba_online
  418. * Wait till the HBA is online after going through
  419. * <= MAX_RETRIES_OF_ISP_ABORT or
  420. * finally HBA is disabled ie marked offline
  421. *
  422. * Input:
  423. * ha - pointer to host adapter structure
  424. *
  425. * Note:
  426. * Does context switching-Release SPIN_LOCK
  427. * (if any) before calling this routine.
  428. *
  429. * Return:
  430. * Success (Adapter is online) : 0
  431. * Failed (Adapter is offline/disabled) : 1
  432. */
  433. static int
  434. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  435. {
  436. int return_status;
  437. unsigned long wait_online;
  438. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  439. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  440. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  441. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  442. ha->dpc_active) && time_before(jiffies, wait_online)) {
  443. msleep(1000);
  444. }
  445. if (ha->flags.online)
  446. return_status = QLA_SUCCESS;
  447. else
  448. return_status = QLA_FUNCTION_FAILED;
  449. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  450. return (return_status);
  451. }
  452. /*
  453. * qla2x00_wait_for_loop_ready
  454. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  455. * to be in LOOP_READY state.
  456. * Input:
  457. * ha - pointer to host adapter structure
  458. *
  459. * Note:
  460. * Does context switching-Release SPIN_LOCK
  461. * (if any) before calling this routine.
  462. *
  463. *
  464. * Return:
  465. * Success (LOOP_READY) : 0
  466. * Failed (LOOP_NOT_READY) : 1
  467. */
  468. static inline int
  469. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  470. {
  471. int return_status = QLA_SUCCESS;
  472. unsigned long loop_timeout ;
  473. /* wait for 5 min at the max for loop to be ready */
  474. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  475. while ((!atomic_read(&ha->loop_down_timer) &&
  476. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  477. atomic_read(&ha->loop_state) != LOOP_READY) {
  478. msleep(1000);
  479. if (time_after_eq(jiffies, loop_timeout)) {
  480. return_status = QLA_FUNCTION_FAILED;
  481. break;
  482. }
  483. }
  484. return (return_status);
  485. }
  486. /**************************************************************************
  487. * qla2xxx_eh_abort
  488. *
  489. * Description:
  490. * The abort function will abort the specified command.
  491. *
  492. * Input:
  493. * cmd = Linux SCSI command packet to be aborted.
  494. *
  495. * Returns:
  496. * Either SUCCESS or FAILED.
  497. *
  498. * Note:
  499. **************************************************************************/
  500. int
  501. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  502. {
  503. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  504. srb_t *sp;
  505. int ret, i;
  506. unsigned int id, lun;
  507. unsigned long serial;
  508. unsigned long flags;
  509. if (!CMD_SP(cmd))
  510. return FAILED;
  511. ret = FAILED;
  512. id = cmd->device->id;
  513. lun = cmd->device->lun;
  514. serial = cmd->serial_number;
  515. /* Check active list for command command. */
  516. spin_lock_irqsave(&ha->hardware_lock, flags);
  517. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  518. sp = ha->outstanding_cmds[i];
  519. if (sp == NULL)
  520. continue;
  521. if (sp->cmd != cmd)
  522. continue;
  523. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
  524. "sp->state=%x\n", __func__, ha->host_no, sp, serial,
  525. sp->state));
  526. DEBUG3(qla2x00_print_scsi_cmd(cmd);)
  527. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  528. if (ha->isp_ops.abort_command(ha, sp)) {
  529. DEBUG2(printk("%s(%ld): abort_command "
  530. "mbx failed.\n", __func__, ha->host_no));
  531. } else {
  532. DEBUG3(printk("%s(%ld): abort_command "
  533. "mbx success.\n", __func__, ha->host_no));
  534. ret = SUCCESS;
  535. }
  536. spin_lock_irqsave(&ha->hardware_lock, flags);
  537. break;
  538. }
  539. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  540. /* Wait for the command to be returned. */
  541. if (ret == SUCCESS) {
  542. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  543. qla_printk(KERN_ERR, ha,
  544. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  545. "%x.\n", ha->host_no, id, lun, serial, ret);
  546. }
  547. }
  548. qla_printk(KERN_INFO, ha,
  549. "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
  550. id, lun, serial, ret);
  551. return ret;
  552. }
  553. /**************************************************************************
  554. * qla2x00_eh_wait_for_pending_target_commands
  555. *
  556. * Description:
  557. * Waits for all the commands to come back from the specified target.
  558. *
  559. * Input:
  560. * ha - pointer to scsi_qla_host structure.
  561. * t - target
  562. * Returns:
  563. * Either SUCCESS or FAILED.
  564. *
  565. * Note:
  566. **************************************************************************/
  567. static int
  568. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  569. {
  570. int cnt;
  571. int status;
  572. srb_t *sp;
  573. struct scsi_cmnd *cmd;
  574. unsigned long flags;
  575. status = 0;
  576. /*
  577. * Waiting for all commands for the designated target in the active
  578. * array
  579. */
  580. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  581. spin_lock_irqsave(&ha->hardware_lock, flags);
  582. sp = ha->outstanding_cmds[cnt];
  583. if (sp) {
  584. cmd = sp->cmd;
  585. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  586. if (cmd->device->id == t) {
  587. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  588. status = 1;
  589. break;
  590. }
  591. }
  592. } else {
  593. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  594. }
  595. }
  596. return (status);
  597. }
  598. /**************************************************************************
  599. * qla2xxx_eh_device_reset
  600. *
  601. * Description:
  602. * The device reset function will reset the target and abort any
  603. * executing commands.
  604. *
  605. * NOTE: The use of SP is undefined within this context. Do *NOT*
  606. * attempt to use this value, even if you determine it is
  607. * non-null.
  608. *
  609. * Input:
  610. * cmd = Linux SCSI command packet of the command that cause the
  611. * bus device reset.
  612. *
  613. * Returns:
  614. * SUCCESS/FAILURE (defined as macro in scsi.h).
  615. *
  616. **************************************************************************/
  617. int
  618. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  619. {
  620. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  621. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  622. srb_t *sp;
  623. int ret;
  624. unsigned int id, lun;
  625. unsigned long serial;
  626. ret = FAILED;
  627. id = cmd->device->id;
  628. lun = cmd->device->lun;
  629. serial = cmd->serial_number;
  630. sp = (srb_t *) CMD_SP(cmd);
  631. if (!sp || !fcport)
  632. return ret;
  633. qla_printk(KERN_INFO, ha,
  634. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  635. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  636. goto eh_dev_reset_done;
  637. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  638. if (qla2x00_device_reset(ha, fcport) == 0)
  639. ret = SUCCESS;
  640. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  641. if (ret == SUCCESS) {
  642. if (fcport->flags & FC_FABRIC_DEVICE) {
  643. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  644. qla2x00_mark_device_lost(ha, fcport);
  645. }
  646. }
  647. #endif
  648. } else {
  649. DEBUG2(printk(KERN_INFO
  650. "%s failed: loop not ready\n",__func__);)
  651. }
  652. if (ret == FAILED) {
  653. DEBUG3(printk("%s(%ld): device reset failed\n",
  654. __func__, ha->host_no));
  655. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  656. __func__);
  657. goto eh_dev_reset_done;
  658. }
  659. /*
  660. * If we are coming down the EH path, wait for all commands to
  661. * complete for the device.
  662. */
  663. if (cmd->device->host->eh_active) {
  664. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  665. ret = FAILED;
  666. if (ret == FAILED) {
  667. DEBUG3(printk("%s(%ld): failed while waiting for "
  668. "commands\n", __func__, ha->host_no));
  669. qla_printk(KERN_INFO, ha,
  670. "%s: failed while waiting for commands\n",
  671. __func__);
  672. goto eh_dev_reset_done;
  673. }
  674. }
  675. qla_printk(KERN_INFO, ha,
  676. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
  677. eh_dev_reset_done:
  678. return ret;
  679. }
  680. /**************************************************************************
  681. * qla2x00_eh_wait_for_pending_commands
  682. *
  683. * Description:
  684. * Waits for all the commands to come back from the specified host.
  685. *
  686. * Input:
  687. * ha - pointer to scsi_qla_host structure.
  688. *
  689. * Returns:
  690. * 1 : SUCCESS
  691. * 0 : FAILED
  692. *
  693. * Note:
  694. **************************************************************************/
  695. static int
  696. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  697. {
  698. int cnt;
  699. int status;
  700. srb_t *sp;
  701. struct scsi_cmnd *cmd;
  702. unsigned long flags;
  703. status = 1;
  704. /*
  705. * Waiting for all commands for the designated target in the active
  706. * array
  707. */
  708. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  709. spin_lock_irqsave(&ha->hardware_lock, flags);
  710. sp = ha->outstanding_cmds[cnt];
  711. if (sp) {
  712. cmd = sp->cmd;
  713. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  714. status = qla2x00_eh_wait_on_command(ha, cmd);
  715. if (status == 0)
  716. break;
  717. }
  718. else {
  719. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  720. }
  721. }
  722. return (status);
  723. }
  724. /**************************************************************************
  725. * qla2xxx_eh_bus_reset
  726. *
  727. * Description:
  728. * The bus reset function will reset the bus and abort any executing
  729. * commands.
  730. *
  731. * Input:
  732. * cmd = Linux SCSI command packet of the command that cause the
  733. * bus reset.
  734. *
  735. * Returns:
  736. * SUCCESS/FAILURE (defined as macro in scsi.h).
  737. *
  738. **************************************************************************/
  739. int
  740. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  741. {
  742. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  743. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  744. srb_t *sp;
  745. int ret;
  746. unsigned int id, lun;
  747. unsigned long serial;
  748. ret = FAILED;
  749. id = cmd->device->id;
  750. lun = cmd->device->lun;
  751. serial = cmd->serial_number;
  752. sp = (srb_t *) CMD_SP(cmd);
  753. if (!sp || !fcport)
  754. return ret;
  755. qla_printk(KERN_INFO, ha,
  756. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  757. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  758. DEBUG2(printk("%s failed:board disabled\n",__func__));
  759. goto eh_bus_reset_done;
  760. }
  761. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  762. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  763. ret = SUCCESS;
  764. }
  765. if (ret == FAILED)
  766. goto eh_bus_reset_done;
  767. /* Waiting for our command in done_queue to be returned to OS.*/
  768. if (cmd->device->host->eh_active)
  769. if (!qla2x00_eh_wait_for_pending_commands(ha))
  770. ret = FAILED;
  771. eh_bus_reset_done:
  772. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  773. (ret == FAILED) ? "failed" : "succeded");
  774. return ret;
  775. }
  776. /**************************************************************************
  777. * qla2xxx_eh_host_reset
  778. *
  779. * Description:
  780. * The reset function will reset the Adapter.
  781. *
  782. * Input:
  783. * cmd = Linux SCSI command packet of the command that cause the
  784. * adapter reset.
  785. *
  786. * Returns:
  787. * Either SUCCESS or FAILED.
  788. *
  789. * Note:
  790. **************************************************************************/
  791. int
  792. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  793. {
  794. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  795. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  796. srb_t *sp;
  797. int ret;
  798. unsigned int id, lun;
  799. unsigned long serial;
  800. ret = FAILED;
  801. id = cmd->device->id;
  802. lun = cmd->device->lun;
  803. serial = cmd->serial_number;
  804. sp = (srb_t *) CMD_SP(cmd);
  805. if (!sp || !fcport)
  806. return ret;
  807. qla_printk(KERN_INFO, ha,
  808. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  809. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  810. goto eh_host_reset_lock;
  811. /*
  812. * Fixme-may be dpc thread is active and processing
  813. * loop_resync,so wait a while for it to
  814. * be completed and then issue big hammer.Otherwise
  815. * it may cause I/O failure as big hammer marks the
  816. * devices as lost kicking of the port_down_timer
  817. * while dpc is stuck for the mailbox to complete.
  818. */
  819. qla2x00_wait_for_loop_ready(ha);
  820. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  821. if (qla2x00_abort_isp(ha)) {
  822. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  823. /* failed. schedule dpc to try */
  824. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  825. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  826. goto eh_host_reset_lock;
  827. }
  828. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  829. /* Waiting for our command in done_queue to be returned to OS.*/
  830. if (qla2x00_eh_wait_for_pending_commands(ha))
  831. ret = SUCCESS;
  832. eh_host_reset_lock:
  833. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  834. (ret == FAILED) ? "failed" : "succeded");
  835. return ret;
  836. }
  837. /*
  838. * qla2x00_loop_reset
  839. * Issue loop reset.
  840. *
  841. * Input:
  842. * ha = adapter block pointer.
  843. *
  844. * Returns:
  845. * 0 = success
  846. */
  847. static int
  848. qla2x00_loop_reset(scsi_qla_host_t *ha)
  849. {
  850. int status = QLA_SUCCESS;
  851. struct fc_port *fcport;
  852. if (ha->flags.enable_lip_reset) {
  853. status = qla2x00_lip_reset(ha);
  854. }
  855. if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
  856. list_for_each_entry(fcport, &ha->fcports, list) {
  857. if (fcport->port_type != FCT_TARGET)
  858. continue;
  859. status = qla2x00_target_reset(ha, fcport);
  860. if (status != QLA_SUCCESS)
  861. break;
  862. }
  863. }
  864. if (status == QLA_SUCCESS &&
  865. ((!ha->flags.enable_target_reset &&
  866. !ha->flags.enable_lip_reset) ||
  867. ha->flags.enable_lip_full_login)) {
  868. status = qla2x00_full_login_lip(ha);
  869. }
  870. /* Issue marker command only when we are going to start the I/O */
  871. ha->marker_needed = 1;
  872. if (status) {
  873. /* Empty */
  874. DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
  875. __func__,
  876. ha->host_no);)
  877. } else {
  878. /* Empty */
  879. DEBUG3(printk("%s(%ld): exiting normally.\n",
  880. __func__,
  881. ha->host_no);)
  882. }
  883. return(status);
  884. }
  885. /*
  886. * qla2x00_device_reset
  887. * Issue bus device reset message to the target.
  888. *
  889. * Input:
  890. * ha = adapter block pointer.
  891. * t = SCSI ID.
  892. * TARGET_QUEUE_LOCK must be released.
  893. * ADAPTER_STATE_LOCK must be released.
  894. *
  895. * Context:
  896. * Kernel context.
  897. */
  898. static int
  899. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  900. {
  901. /* Abort Target command will clear Reservation */
  902. return ha->isp_ops.abort_target(reset_fcport);
  903. }
  904. static int
  905. qla2xxx_slave_alloc(struct scsi_device *sdev)
  906. {
  907. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  908. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  909. fc_port_t *fcport;
  910. int found;
  911. if (!rport)
  912. return -ENXIO;
  913. found = 0;
  914. list_for_each_entry(fcport, &ha->fcports, list) {
  915. if (rport->port_name ==
  916. be64_to_cpu(*(uint64_t *)fcport->port_name)) {
  917. found++;
  918. break;
  919. }
  920. }
  921. if (!found)
  922. return -ENXIO;
  923. sdev->hostdata = fcport;
  924. return 0;
  925. }
  926. static int
  927. qla2xxx_slave_configure(struct scsi_device *sdev)
  928. {
  929. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  930. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  931. if (sdev->tagged_supported)
  932. scsi_activate_tcq(sdev, 32);
  933. else
  934. scsi_deactivate_tcq(sdev, 32);
  935. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  936. return 0;
  937. }
  938. static void
  939. qla2xxx_slave_destroy(struct scsi_device *sdev)
  940. {
  941. sdev->hostdata = NULL;
  942. }
  943. /**
  944. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  945. * @ha: HA context
  946. *
  947. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  948. * supported addressing method.
  949. */
  950. static void
  951. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  952. {
  953. /* Assume 32bit DMA address */
  954. ha->flags.enable_64bit_addressing = 0;
  955. /*
  956. * Given the two variants pci_set_dma_mask(), allow the compiler to
  957. * assist in setting the proper dma mask.
  958. */
  959. if (sizeof(dma_addr_t) > 4) {
  960. if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
  961. ha->flags.enable_64bit_addressing = 1;
  962. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  963. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  964. if (pci_set_consistent_dma_mask(ha->pdev,
  965. DMA_64BIT_MASK)) {
  966. qla_printk(KERN_DEBUG, ha,
  967. "Failed to set 64 bit PCI consistent mask; "
  968. "using 32 bit.\n");
  969. pci_set_consistent_dma_mask(ha->pdev,
  970. DMA_32BIT_MASK);
  971. }
  972. } else {
  973. qla_printk(KERN_DEBUG, ha,
  974. "Failed to set 64 bit PCI DMA mask, falling back "
  975. "to 32 bit MASK.\n");
  976. pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
  977. }
  978. } else {
  979. pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
  980. }
  981. }
  982. static int
  983. qla2x00_iospace_config(scsi_qla_host_t *ha)
  984. {
  985. unsigned long pio, pio_len, pio_flags;
  986. unsigned long mmio, mmio_len, mmio_flags;
  987. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  988. pio = pci_resource_start(ha->pdev, 0);
  989. pio_len = pci_resource_len(ha->pdev, 0);
  990. pio_flags = pci_resource_flags(ha->pdev, 0);
  991. if (pio_flags & IORESOURCE_IO) {
  992. if (pio_len < MIN_IOBASE_LEN) {
  993. qla_printk(KERN_WARNING, ha,
  994. "Invalid PCI I/O region size (%s)...\n",
  995. pci_name(ha->pdev));
  996. pio = 0;
  997. }
  998. } else {
  999. qla_printk(KERN_WARNING, ha,
  1000. "region #0 not a PIO resource (%s)...\n",
  1001. pci_name(ha->pdev));
  1002. pio = 0;
  1003. }
  1004. /* Use MMIO operations for all accesses. */
  1005. mmio = pci_resource_start(ha->pdev, 1);
  1006. mmio_len = pci_resource_len(ha->pdev, 1);
  1007. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1008. if (!(mmio_flags & IORESOURCE_MEM)) {
  1009. qla_printk(KERN_ERR, ha,
  1010. "region #0 not an MMIO resource (%s), aborting\n",
  1011. pci_name(ha->pdev));
  1012. goto iospace_error_exit;
  1013. }
  1014. if (mmio_len < MIN_IOBASE_LEN) {
  1015. qla_printk(KERN_ERR, ha,
  1016. "Invalid PCI mem region size (%s), aborting\n",
  1017. pci_name(ha->pdev));
  1018. goto iospace_error_exit;
  1019. }
  1020. if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
  1021. qla_printk(KERN_WARNING, ha,
  1022. "Failed to reserve PIO/MMIO regions (%s)\n",
  1023. pci_name(ha->pdev));
  1024. goto iospace_error_exit;
  1025. }
  1026. ha->pio_address = pio;
  1027. ha->pio_length = pio_len;
  1028. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1029. if (!ha->iobase) {
  1030. qla_printk(KERN_ERR, ha,
  1031. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1032. goto iospace_error_exit;
  1033. }
  1034. return (0);
  1035. iospace_error_exit:
  1036. return (-ENOMEM);
  1037. }
  1038. static void
  1039. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1040. {
  1041. unsigned long flags = 0;
  1042. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1043. spin_lock_irqsave(&ha->hardware_lock, flags);
  1044. ha->interrupts_on = 1;
  1045. /* enable risc and host interrupts */
  1046. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1047. RD_REG_WORD(&reg->ictrl);
  1048. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1049. }
  1050. static void
  1051. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1052. {
  1053. unsigned long flags = 0;
  1054. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1055. spin_lock_irqsave(&ha->hardware_lock, flags);
  1056. ha->interrupts_on = 0;
  1057. /* disable risc and host interrupts */
  1058. WRT_REG_WORD(&reg->ictrl, 0);
  1059. RD_REG_WORD(&reg->ictrl);
  1060. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1061. }
  1062. static void
  1063. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1064. {
  1065. unsigned long flags = 0;
  1066. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1067. spin_lock_irqsave(&ha->hardware_lock, flags);
  1068. ha->interrupts_on = 1;
  1069. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1070. RD_REG_DWORD(&reg->ictrl);
  1071. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1072. }
  1073. static void
  1074. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1075. {
  1076. unsigned long flags = 0;
  1077. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1078. spin_lock_irqsave(&ha->hardware_lock, flags);
  1079. ha->interrupts_on = 0;
  1080. WRT_REG_DWORD(&reg->ictrl, 0);
  1081. RD_REG_DWORD(&reg->ictrl);
  1082. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1083. }
  1084. /*
  1085. * PCI driver interface
  1086. */
  1087. int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
  1088. {
  1089. int ret = -ENODEV;
  1090. device_reg_t __iomem *reg;
  1091. struct Scsi_Host *host;
  1092. scsi_qla_host_t *ha;
  1093. unsigned long flags = 0;
  1094. unsigned long wait_switch = 0;
  1095. char pci_info[20];
  1096. char fw_str[30];
  1097. fc_port_t *fcport;
  1098. if (pci_enable_device(pdev))
  1099. goto probe_out;
  1100. host = scsi_host_alloc(brd_info->sht ? brd_info->sht:
  1101. &qla2x00_driver_template, sizeof(scsi_qla_host_t));
  1102. if (host == NULL) {
  1103. printk(KERN_WARNING
  1104. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1105. goto probe_disable_device;
  1106. }
  1107. /* Clear our data area */
  1108. ha = (scsi_qla_host_t *)host->hostdata;
  1109. memset(ha, 0, sizeof(scsi_qla_host_t));
  1110. ha->pdev = pdev;
  1111. ha->host = host;
  1112. ha->host_no = host->host_no;
  1113. ha->brd_info = brd_info;
  1114. sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
  1115. /* Configure PCI I/O space */
  1116. ret = qla2x00_iospace_config(ha);
  1117. if (ret)
  1118. goto probe_failed;
  1119. qla_printk(KERN_INFO, ha,
  1120. "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
  1121. pdev->irq, ha->iobase);
  1122. spin_lock_init(&ha->hardware_lock);
  1123. ha->prev_topology = 0;
  1124. ha->ports = MAX_BUSES;
  1125. ha->init_cb_size = sizeof(init_cb_t);
  1126. /* Assign ISP specific operations. */
  1127. ha->isp_ops.pci_config = qla2100_pci_config;
  1128. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1129. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1130. ha->isp_ops.config_rings = qla2x00_config_rings;
  1131. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1132. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1133. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1134. ha->isp_ops.load_risc = qla2x00_load_risc;
  1135. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1136. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1137. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1138. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1139. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1140. ha->isp_ops.abort_command = qla2x00_abort_command;
  1141. ha->isp_ops.abort_target = qla2x00_abort_target;
  1142. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1143. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1144. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1145. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1146. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1147. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1148. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1149. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1150. ha->isp_ops.ascii_fw_dump = qla2100_ascii_fw_dump;
  1151. if (IS_QLA2100(ha)) {
  1152. host->max_id = MAX_TARGETS_2100;
  1153. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1154. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1155. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1156. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1157. host->sg_tablesize = 32;
  1158. ha->gid_list_info_size = 4;
  1159. } else if (IS_QLA2200(ha)) {
  1160. host->max_id = MAX_TARGETS_2200;
  1161. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1162. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1163. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1164. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1165. ha->gid_list_info_size = 4;
  1166. } else if (IS_QLA23XX(ha)) {
  1167. host->max_id = MAX_TARGETS_2200;
  1168. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1169. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1170. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1171. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1172. ha->isp_ops.pci_config = qla2300_pci_config;
  1173. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1174. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1175. ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
  1176. ha->gid_list_info_size = 6;
  1177. } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
  1178. host->max_id = MAX_TARGETS_2200;
  1179. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1180. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1181. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1182. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1183. ha->init_cb_size = sizeof(struct init_cb_24xx);
  1184. ha->isp_ops.pci_config = qla24xx_pci_config;
  1185. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1186. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1187. ha->isp_ops.config_rings = qla24xx_config_rings;
  1188. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1189. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1190. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1191. ha->isp_ops.load_risc = qla24xx_load_risc_flash;
  1192. if (ql2xfwloadbin)
  1193. ha->isp_ops.load_risc = qla24xx_load_risc_hotplug;
  1194. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1195. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1196. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1197. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1198. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1199. ha->isp_ops.abort_command = qla24xx_abort_command;
  1200. ha->isp_ops.abort_target = qla24xx_abort_target;
  1201. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1202. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1203. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1204. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1205. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1206. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1207. ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
  1208. ha->gid_list_info_size = 8;
  1209. }
  1210. host->can_queue = ha->request_q_length + 128;
  1211. /* load the F/W, read paramaters, and init the H/W */
  1212. ha->instance = num_hosts;
  1213. init_MUTEX(&ha->mbx_cmd_sem);
  1214. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1215. INIT_LIST_HEAD(&ha->list);
  1216. INIT_LIST_HEAD(&ha->fcports);
  1217. INIT_LIST_HEAD(&ha->rscn_fcports);
  1218. /*
  1219. * These locks are used to prevent more than one CPU
  1220. * from modifying the queue at the same time. The
  1221. * higher level "host_lock" will reduce most
  1222. * contention for these locks.
  1223. */
  1224. spin_lock_init(&ha->mbx_reg_lock);
  1225. ha->dpc_pid = -1;
  1226. init_completion(&ha->dpc_inited);
  1227. init_completion(&ha->dpc_exited);
  1228. qla2x00_config_dma_addressing(ha);
  1229. if (qla2x00_mem_alloc(ha)) {
  1230. qla_printk(KERN_WARNING, ha,
  1231. "[ERROR] Failed to allocate memory for adapter\n");
  1232. ret = -ENOMEM;
  1233. goto probe_failed;
  1234. }
  1235. if (qla2x00_initialize_adapter(ha) &&
  1236. !(ha->device_flags & DFLG_NO_CABLE)) {
  1237. qla_printk(KERN_WARNING, ha,
  1238. "Failed to initialize adapter\n");
  1239. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1240. "Adapter flags %x.\n",
  1241. ha->host_no, ha->device_flags));
  1242. ret = -ENODEV;
  1243. goto probe_failed;
  1244. }
  1245. /*
  1246. * Startup the kernel thread for this host adapter
  1247. */
  1248. ha->dpc_should_die = 0;
  1249. ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
  1250. if (ha->dpc_pid < 0) {
  1251. qla_printk(KERN_WARNING, ha,
  1252. "Unable to start DPC thread!\n");
  1253. ret = -ENODEV;
  1254. goto probe_failed;
  1255. }
  1256. wait_for_completion(&ha->dpc_inited);
  1257. host->this_id = 255;
  1258. host->cmd_per_lun = 3;
  1259. host->unique_id = ha->instance;
  1260. host->max_cmd_len = MAX_CMDSZ;
  1261. host->max_channel = ha->ports - 1;
  1262. host->max_lun = MAX_LUNS;
  1263. host->transportt = qla2xxx_transport_template;
  1264. ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
  1265. SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
  1266. if (ret) {
  1267. qla_printk(KERN_WARNING, ha,
  1268. "Failed to reserve interrupt %d already in use.\n",
  1269. pdev->irq);
  1270. goto probe_failed;
  1271. }
  1272. host->irq = pdev->irq;
  1273. /* Initialized the timer */
  1274. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1275. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1276. ha->host_no, ha));
  1277. ha->isp_ops.disable_intrs(ha);
  1278. spin_lock_irqsave(&ha->hardware_lock, flags);
  1279. reg = ha->iobase;
  1280. if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
  1281. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1282. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1283. } else {
  1284. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1285. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1286. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1287. /* Enable proper parity */
  1288. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1289. if (IS_QLA2300(ha))
  1290. /* SRAM parity */
  1291. WRT_REG_WORD(&reg->isp.hccr,
  1292. (HCCR_ENABLE_PARITY + 0x1));
  1293. else
  1294. /* SRAM, Instruction RAM and GP RAM parity */
  1295. WRT_REG_WORD(&reg->isp.hccr,
  1296. (HCCR_ENABLE_PARITY + 0x7));
  1297. }
  1298. }
  1299. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1300. ha->isp_ops.enable_intrs(ha);
  1301. /* v2.19.5b6 */
  1302. /*
  1303. * Wait around max loop_reset_delay secs for the devices to come
  1304. * on-line. We don't want Linux scanning before we are ready.
  1305. *
  1306. */
  1307. for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
  1308. time_before(jiffies,wait_switch) &&
  1309. !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
  1310. && (ha->device_flags & SWITCH_FOUND) ;) {
  1311. qla2x00_check_fabric_devices(ha);
  1312. msleep(10);
  1313. }
  1314. pci_set_drvdata(pdev, ha);
  1315. ha->flags.init_done = 1;
  1316. num_hosts++;
  1317. ret = scsi_add_host(host, &pdev->dev);
  1318. if (ret)
  1319. goto probe_failed;
  1320. qla2x00_alloc_sysfs_attr(ha);
  1321. qla2x00_init_host_attr(ha);
  1322. qla_printk(KERN_INFO, ha, "\n"
  1323. " QLogic Fibre Channel HBA Driver: %s\n"
  1324. " QLogic %s - %s\n"
  1325. " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
  1326. ha->model_number, ha->model_desc ? ha->model_desc: "",
  1327. ha->brd_info->isp_name, ha->isp_ops.pci_info_str(ha, pci_info),
  1328. pci_name(pdev), ha->flags.enable_64bit_addressing ? '+': '-',
  1329. ha->host_no, ha->isp_ops.fw_version_str(ha, fw_str));
  1330. /* Go with fc_rport registration. */
  1331. list_for_each_entry(fcport, &ha->fcports, list)
  1332. qla2x00_reg_remote_port(ha, fcport);
  1333. return 0;
  1334. probe_failed:
  1335. fc_remove_host(ha->host);
  1336. qla2x00_free_device(ha);
  1337. scsi_host_put(host);
  1338. probe_disable_device:
  1339. pci_disable_device(pdev);
  1340. probe_out:
  1341. return ret;
  1342. }
  1343. EXPORT_SYMBOL_GPL(qla2x00_probe_one);
  1344. void qla2x00_remove_one(struct pci_dev *pdev)
  1345. {
  1346. scsi_qla_host_t *ha;
  1347. ha = pci_get_drvdata(pdev);
  1348. qla2x00_free_sysfs_attr(ha);
  1349. fc_remove_host(ha->host);
  1350. scsi_remove_host(ha->host);
  1351. qla2x00_free_device(ha);
  1352. scsi_host_put(ha->host);
  1353. pci_set_drvdata(pdev, NULL);
  1354. }
  1355. EXPORT_SYMBOL_GPL(qla2x00_remove_one);
  1356. static void
  1357. qla2x00_free_device(scsi_qla_host_t *ha)
  1358. {
  1359. int ret;
  1360. /* Abort any outstanding IO descriptors. */
  1361. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1362. qla2x00_cancel_io_descriptors(ha);
  1363. /* turn-off interrupts on the card */
  1364. if (ha->interrupts_on)
  1365. ha->isp_ops.disable_intrs(ha);
  1366. /* Disable timer */
  1367. if (ha->timer_active)
  1368. qla2x00_stop_timer(ha);
  1369. /* Kill the kernel thread for this host */
  1370. if (ha->dpc_pid >= 0) {
  1371. ha->dpc_should_die = 1;
  1372. wmb();
  1373. ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
  1374. if (ret) {
  1375. qla_printk(KERN_ERR, ha,
  1376. "Unable to signal DPC thread -- (%d)\n", ret);
  1377. /* TODO: SOMETHING MORE??? */
  1378. } else {
  1379. wait_for_completion(&ha->dpc_exited);
  1380. }
  1381. }
  1382. qla2x00_mem_free(ha);
  1383. ha->flags.online = 0;
  1384. /* Detach interrupts */
  1385. if (ha->pdev->irq)
  1386. free_irq(ha->pdev->irq, ha);
  1387. /* release io space registers */
  1388. if (ha->iobase)
  1389. iounmap(ha->iobase);
  1390. pci_release_regions(ha->pdev);
  1391. pci_disable_device(ha->pdev);
  1392. }
  1393. /*
  1394. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1395. *
  1396. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1397. *
  1398. * Return: None.
  1399. *
  1400. * Context:
  1401. */
  1402. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1403. int do_login)
  1404. {
  1405. if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
  1406. fc_remote_port_block(fcport->rport);
  1407. /*
  1408. * We may need to retry the login, so don't change the state of the
  1409. * port but do the retries.
  1410. */
  1411. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1412. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1413. if (!do_login)
  1414. return;
  1415. if (fcport->login_retry == 0) {
  1416. fcport->login_retry = ha->login_retry_count;
  1417. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1418. DEBUG(printk("scsi(%ld): Port login retry: "
  1419. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1420. "id = 0x%04x retry cnt=%d\n",
  1421. ha->host_no,
  1422. fcport->port_name[0],
  1423. fcport->port_name[1],
  1424. fcport->port_name[2],
  1425. fcport->port_name[3],
  1426. fcport->port_name[4],
  1427. fcport->port_name[5],
  1428. fcport->port_name[6],
  1429. fcport->port_name[7],
  1430. fcport->loop_id,
  1431. fcport->login_retry));
  1432. }
  1433. }
  1434. /*
  1435. * qla2x00_mark_all_devices_lost
  1436. * Updates fcport state when device goes offline.
  1437. *
  1438. * Input:
  1439. * ha = adapter block pointer.
  1440. * fcport = port structure pointer.
  1441. *
  1442. * Return:
  1443. * None.
  1444. *
  1445. * Context:
  1446. */
  1447. void
  1448. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
  1449. {
  1450. fc_port_t *fcport;
  1451. list_for_each_entry(fcport, &ha->fcports, list) {
  1452. if (fcport->port_type != FCT_TARGET)
  1453. continue;
  1454. /*
  1455. * No point in marking the device as lost, if the device is
  1456. * already DEAD.
  1457. */
  1458. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1459. continue;
  1460. if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
  1461. fc_remote_port_block(fcport->rport);
  1462. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1463. }
  1464. }
  1465. /*
  1466. * qla2x00_mem_alloc
  1467. * Allocates adapter memory.
  1468. *
  1469. * Returns:
  1470. * 0 = success.
  1471. * 1 = failure.
  1472. */
  1473. static uint8_t
  1474. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1475. {
  1476. char name[16];
  1477. uint8_t status = 1;
  1478. int retry= 10;
  1479. do {
  1480. /*
  1481. * This will loop only once if everything goes well, else some
  1482. * number of retries will be performed to get around a kernel
  1483. * bug where available mem is not allocated until after a
  1484. * little delay and a retry.
  1485. */
  1486. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1487. (ha->request_q_length + 1) * sizeof(request_t),
  1488. &ha->request_dma, GFP_KERNEL);
  1489. if (ha->request_ring == NULL) {
  1490. qla_printk(KERN_WARNING, ha,
  1491. "Memory Allocation failed - request_ring\n");
  1492. qla2x00_mem_free(ha);
  1493. msleep(100);
  1494. continue;
  1495. }
  1496. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1497. (ha->response_q_length + 1) * sizeof(response_t),
  1498. &ha->response_dma, GFP_KERNEL);
  1499. if (ha->response_ring == NULL) {
  1500. qla_printk(KERN_WARNING, ha,
  1501. "Memory Allocation failed - response_ring\n");
  1502. qla2x00_mem_free(ha);
  1503. msleep(100);
  1504. continue;
  1505. }
  1506. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1507. &ha->gid_list_dma, GFP_KERNEL);
  1508. if (ha->gid_list == NULL) {
  1509. qla_printk(KERN_WARNING, ha,
  1510. "Memory Allocation failed - gid_list\n");
  1511. qla2x00_mem_free(ha);
  1512. msleep(100);
  1513. continue;
  1514. }
  1515. ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
  1516. sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
  1517. if (ha->rlc_rsp == NULL) {
  1518. qla_printk(KERN_WARNING, ha,
  1519. "Memory Allocation failed - rlc");
  1520. qla2x00_mem_free(ha);
  1521. msleep(100);
  1522. continue;
  1523. }
  1524. snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
  1525. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1526. DMA_POOL_SIZE, 8, 0);
  1527. if (ha->s_dma_pool == NULL) {
  1528. qla_printk(KERN_WARNING, ha,
  1529. "Memory Allocation failed - s_dma_pool\n");
  1530. qla2x00_mem_free(ha);
  1531. msleep(100);
  1532. continue;
  1533. }
  1534. /* get consistent memory allocated for init control block */
  1535. ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1536. &ha->init_cb_dma);
  1537. if (ha->init_cb == NULL) {
  1538. qla_printk(KERN_WARNING, ha,
  1539. "Memory Allocation failed - init_cb\n");
  1540. qla2x00_mem_free(ha);
  1541. msleep(100);
  1542. continue;
  1543. }
  1544. memset(ha->init_cb, 0, ha->init_cb_size);
  1545. /* Get consistent memory allocated for Get Port Database cmd */
  1546. ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1547. &ha->iodesc_pd_dma);
  1548. if (ha->iodesc_pd == NULL) {
  1549. /* error */
  1550. qla_printk(KERN_WARNING, ha,
  1551. "Memory Allocation failed - iodesc_pd\n");
  1552. qla2x00_mem_free(ha);
  1553. msleep(100);
  1554. continue;
  1555. }
  1556. memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
  1557. /* Allocate ioctl related memory. */
  1558. if (qla2x00_alloc_ioctl_mem(ha)) {
  1559. qla_printk(KERN_WARNING, ha,
  1560. "Memory Allocation failed - ioctl_mem\n");
  1561. qla2x00_mem_free(ha);
  1562. msleep(100);
  1563. continue;
  1564. }
  1565. if (qla2x00_allocate_sp_pool(ha)) {
  1566. qla_printk(KERN_WARNING, ha,
  1567. "Memory Allocation failed - "
  1568. "qla2x00_allocate_sp_pool()\n");
  1569. qla2x00_mem_free(ha);
  1570. msleep(100);
  1571. continue;
  1572. }
  1573. /* Allocate memory for SNS commands */
  1574. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1575. /* Get consistent memory allocated for SNS commands */
  1576. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1577. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1578. GFP_KERNEL);
  1579. if (ha->sns_cmd == NULL) {
  1580. /* error */
  1581. qla_printk(KERN_WARNING, ha,
  1582. "Memory Allocation failed - sns_cmd\n");
  1583. qla2x00_mem_free(ha);
  1584. msleep(100);
  1585. continue;
  1586. }
  1587. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1588. } else {
  1589. /* Get consistent memory allocated for MS IOCB */
  1590. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1591. &ha->ms_iocb_dma);
  1592. if (ha->ms_iocb == NULL) {
  1593. /* error */
  1594. qla_printk(KERN_WARNING, ha,
  1595. "Memory Allocation failed - ms_iocb\n");
  1596. qla2x00_mem_free(ha);
  1597. msleep(100);
  1598. continue;
  1599. }
  1600. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1601. /*
  1602. * Get consistent memory allocated for CT SNS
  1603. * commands
  1604. */
  1605. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1606. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1607. GFP_KERNEL);
  1608. if (ha->ct_sns == NULL) {
  1609. /* error */
  1610. qla_printk(KERN_WARNING, ha,
  1611. "Memory Allocation failed - ct_sns\n");
  1612. qla2x00_mem_free(ha);
  1613. msleep(100);
  1614. continue;
  1615. }
  1616. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1617. }
  1618. /* Done all allocations without any error. */
  1619. status = 0;
  1620. } while (retry-- && status != 0);
  1621. if (status) {
  1622. printk(KERN_WARNING
  1623. "%s(): **** FAILED ****\n", __func__);
  1624. }
  1625. return(status);
  1626. }
  1627. /*
  1628. * qla2x00_mem_free
  1629. * Frees all adapter allocated memory.
  1630. *
  1631. * Input:
  1632. * ha = adapter block pointer.
  1633. */
  1634. static void
  1635. qla2x00_mem_free(scsi_qla_host_t *ha)
  1636. {
  1637. struct list_head *fcpl, *fcptemp;
  1638. fc_port_t *fcport;
  1639. unsigned long wtime;/* max wait time if mbx cmd is busy. */
  1640. if (ha == NULL) {
  1641. /* error */
  1642. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1643. return;
  1644. }
  1645. /* Make sure all other threads are stopped. */
  1646. wtime = 60 * HZ;
  1647. while (ha->dpc_wait && wtime) {
  1648. set_current_state(TASK_INTERRUPTIBLE);
  1649. wtime = schedule_timeout(wtime);
  1650. }
  1651. /* free ioctl memory */
  1652. qla2x00_free_ioctl_mem(ha);
  1653. /* free sp pool */
  1654. qla2x00_free_sp_pool(ha);
  1655. if (ha->sns_cmd)
  1656. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1657. ha->sns_cmd, ha->sns_cmd_dma);
  1658. if (ha->ct_sns)
  1659. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1660. ha->ct_sns, ha->ct_sns_dma);
  1661. if (ha->ms_iocb)
  1662. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1663. if (ha->iodesc_pd)
  1664. dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
  1665. if (ha->init_cb)
  1666. dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
  1667. if (ha->s_dma_pool)
  1668. dma_pool_destroy(ha->s_dma_pool);
  1669. if (ha->rlc_rsp)
  1670. dma_free_coherent(&ha->pdev->dev,
  1671. sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
  1672. ha->rlc_rsp_dma);
  1673. if (ha->gid_list)
  1674. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1675. ha->gid_list_dma);
  1676. if (ha->response_ring)
  1677. dma_free_coherent(&ha->pdev->dev,
  1678. (ha->response_q_length + 1) * sizeof(response_t),
  1679. ha->response_ring, ha->response_dma);
  1680. if (ha->request_ring)
  1681. dma_free_coherent(&ha->pdev->dev,
  1682. (ha->request_q_length + 1) * sizeof(request_t),
  1683. ha->request_ring, ha->request_dma);
  1684. ha->sns_cmd = NULL;
  1685. ha->sns_cmd_dma = 0;
  1686. ha->ct_sns = NULL;
  1687. ha->ct_sns_dma = 0;
  1688. ha->ms_iocb = NULL;
  1689. ha->ms_iocb_dma = 0;
  1690. ha->iodesc_pd = NULL;
  1691. ha->iodesc_pd_dma = 0;
  1692. ha->init_cb = NULL;
  1693. ha->init_cb_dma = 0;
  1694. ha->s_dma_pool = NULL;
  1695. ha->rlc_rsp = NULL;
  1696. ha->rlc_rsp_dma = 0;
  1697. ha->gid_list = NULL;
  1698. ha->gid_list_dma = 0;
  1699. ha->response_ring = NULL;
  1700. ha->response_dma = 0;
  1701. ha->request_ring = NULL;
  1702. ha->request_dma = 0;
  1703. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1704. fcport = list_entry(fcpl, fc_port_t, list);
  1705. /* fc ports */
  1706. list_del_init(&fcport->list);
  1707. kfree(fcport);
  1708. }
  1709. INIT_LIST_HEAD(&ha->fcports);
  1710. if (ha->fw_dump)
  1711. free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
  1712. vfree(ha->fw_dump24);
  1713. vfree(ha->fw_dump_buffer);
  1714. ha->fw_dump = NULL;
  1715. ha->fw_dump24 = NULL;
  1716. ha->fw_dumped = 0;
  1717. ha->fw_dump_reading = 0;
  1718. ha->fw_dump_buffer = NULL;
  1719. }
  1720. /*
  1721. * qla2x00_allocate_sp_pool
  1722. * This routine is called during initialization to allocate
  1723. * memory for local srb_t.
  1724. *
  1725. * Input:
  1726. * ha = adapter block pointer.
  1727. *
  1728. * Context:
  1729. * Kernel context.
  1730. *
  1731. * Note: Sets the ref_count for non Null sp to one.
  1732. */
  1733. static int
  1734. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1735. {
  1736. int rval;
  1737. rval = QLA_SUCCESS;
  1738. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  1739. mempool_free_slab, srb_cachep);
  1740. if (ha->srb_mempool == NULL) {
  1741. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1742. rval = QLA_FUNCTION_FAILED;
  1743. }
  1744. return (rval);
  1745. }
  1746. /*
  1747. * This routine frees all adapter allocated memory.
  1748. *
  1749. */
  1750. static void
  1751. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1752. {
  1753. if (ha->srb_mempool) {
  1754. mempool_destroy(ha->srb_mempool);
  1755. ha->srb_mempool = NULL;
  1756. }
  1757. }
  1758. /**************************************************************************
  1759. * qla2x00_do_dpc
  1760. * This kernel thread is a task that is schedule by the interrupt handler
  1761. * to perform the background processing for interrupts.
  1762. *
  1763. * Notes:
  1764. * This task always run in the context of a kernel thread. It
  1765. * is kick-off by the driver's detect code and starts up
  1766. * up one per adapter. It immediately goes to sleep and waits for
  1767. * some fibre event. When either the interrupt handler or
  1768. * the timer routine detects a event it will one of the task
  1769. * bits then wake us up.
  1770. **************************************************************************/
  1771. static int
  1772. qla2x00_do_dpc(void *data)
  1773. {
  1774. DECLARE_MUTEX_LOCKED(sem);
  1775. scsi_qla_host_t *ha;
  1776. fc_port_t *fcport;
  1777. uint8_t status;
  1778. uint16_t next_loopid;
  1779. ha = (scsi_qla_host_t *)data;
  1780. lock_kernel();
  1781. daemonize("%s_dpc", ha->host_str);
  1782. allow_signal(SIGHUP);
  1783. ha->dpc_wait = &sem;
  1784. set_user_nice(current, -20);
  1785. unlock_kernel();
  1786. complete(&ha->dpc_inited);
  1787. while (1) {
  1788. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1789. if (down_interruptible(&sem))
  1790. break;
  1791. if (ha->dpc_should_die)
  1792. break;
  1793. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1794. /* Initialization not yet finished. Don't do anything yet. */
  1795. if (!ha->flags.init_done || ha->dpc_active)
  1796. continue;
  1797. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1798. ha->dpc_active = 1;
  1799. if (ha->flags.mbox_busy) {
  1800. ha->dpc_active = 0;
  1801. continue;
  1802. }
  1803. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1804. DEBUG(printk("scsi(%ld): dpc: sched "
  1805. "qla2x00_abort_isp ha = %p\n",
  1806. ha->host_no, ha));
  1807. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1808. &ha->dpc_flags))) {
  1809. if (qla2x00_abort_isp(ha)) {
  1810. /* failed. retry later */
  1811. set_bit(ISP_ABORT_NEEDED,
  1812. &ha->dpc_flags);
  1813. }
  1814. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1815. }
  1816. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1817. ha->host_no));
  1818. }
  1819. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1820. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1821. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1822. ha->host_no));
  1823. qla2x00_rst_aen(ha);
  1824. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1825. }
  1826. /* Retry each device up to login retry count */
  1827. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1828. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1829. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1830. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1831. ha->host_no));
  1832. next_loopid = 0;
  1833. list_for_each_entry(fcport, &ha->fcports, list) {
  1834. if (fcport->port_type != FCT_TARGET)
  1835. continue;
  1836. /*
  1837. * If the port is not ONLINE then try to login
  1838. * to it if we haven't run out of retries.
  1839. */
  1840. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1841. fcport->login_retry) {
  1842. fcport->login_retry--;
  1843. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1844. if (fcport->flags &
  1845. FCF_TAPE_PRESENT)
  1846. ha->isp_ops.fabric_logout(
  1847. ha, fcport->loop_id,
  1848. fcport->d_id.b.domain,
  1849. fcport->d_id.b.area,
  1850. fcport->d_id.b.al_pa);
  1851. status = qla2x00_fabric_login(
  1852. ha, fcport, &next_loopid);
  1853. } else
  1854. status =
  1855. qla2x00_local_device_login(
  1856. ha, fcport->loop_id);
  1857. if (status == QLA_SUCCESS) {
  1858. fcport->old_loop_id = fcport->loop_id;
  1859. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1860. ha->host_no, fcport->loop_id));
  1861. fcport->port_login_retry_count =
  1862. ha->port_down_retry_count * PORT_RETRY_TIME;
  1863. atomic_set(&fcport->state, FCS_ONLINE);
  1864. atomic_set(&fcport->port_down_timer,
  1865. ha->port_down_retry_count * PORT_RETRY_TIME);
  1866. fcport->login_retry = 0;
  1867. } else if (status == 1) {
  1868. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1869. /* retry the login again */
  1870. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1871. ha->host_no,
  1872. fcport->login_retry, fcport->loop_id));
  1873. } else {
  1874. fcport->login_retry = 0;
  1875. }
  1876. }
  1877. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1878. break;
  1879. }
  1880. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1881. ha->host_no));
  1882. }
  1883. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1884. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1885. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1886. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1887. ha->host_no));
  1888. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1889. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1890. ha->host_no));
  1891. }
  1892. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1893. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1894. ha->host_no));
  1895. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  1896. &ha->dpc_flags))) {
  1897. qla2x00_loop_resync(ha);
  1898. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  1899. }
  1900. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  1901. ha->host_no));
  1902. }
  1903. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  1904. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  1905. ha->host_no));
  1906. qla2x00_rescan_fcports(ha);
  1907. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  1908. "end.\n",
  1909. ha->host_no));
  1910. }
  1911. if (!ha->interrupts_on)
  1912. ha->isp_ops.enable_intrs(ha);
  1913. ha->dpc_active = 0;
  1914. } /* End of while(1) */
  1915. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  1916. /*
  1917. * Make sure that nobody tries to wake us up again.
  1918. */
  1919. ha->dpc_wait = NULL;
  1920. ha->dpc_active = 0;
  1921. complete_and_exit(&ha->dpc_exited, 0);
  1922. }
  1923. /*
  1924. * qla2x00_rst_aen
  1925. * Processes asynchronous reset.
  1926. *
  1927. * Input:
  1928. * ha = adapter block pointer.
  1929. */
  1930. static void
  1931. qla2x00_rst_aen(scsi_qla_host_t *ha)
  1932. {
  1933. if (ha->flags.online && !ha->flags.reset_active &&
  1934. !atomic_read(&ha->loop_down_timer) &&
  1935. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  1936. do {
  1937. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  1938. /*
  1939. * Issue marker command only when we are going to start
  1940. * the I/O.
  1941. */
  1942. ha->marker_needed = 1;
  1943. } while (!atomic_read(&ha->loop_down_timer) &&
  1944. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  1945. }
  1946. }
  1947. static void
  1948. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  1949. {
  1950. struct scsi_cmnd *cmd = sp->cmd;
  1951. if (sp->flags & SRB_DMA_VALID) {
  1952. if (cmd->use_sg) {
  1953. dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
  1954. cmd->use_sg, cmd->sc_data_direction);
  1955. } else if (cmd->request_bufflen) {
  1956. dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
  1957. cmd->request_bufflen, cmd->sc_data_direction);
  1958. }
  1959. sp->flags &= ~SRB_DMA_VALID;
  1960. }
  1961. CMD_SP(cmd) = NULL;
  1962. }
  1963. void
  1964. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  1965. {
  1966. struct scsi_cmnd *cmd = sp->cmd;
  1967. qla2x00_sp_free_dma(ha, sp);
  1968. mempool_free(sp, ha->srb_mempool);
  1969. cmd->scsi_done(cmd);
  1970. }
  1971. /**************************************************************************
  1972. * qla2x00_timer
  1973. *
  1974. * Description:
  1975. * One second timer
  1976. *
  1977. * Context: Interrupt
  1978. ***************************************************************************/
  1979. static void
  1980. qla2x00_timer(scsi_qla_host_t *ha)
  1981. {
  1982. unsigned long cpu_flags = 0;
  1983. fc_port_t *fcport;
  1984. int start_dpc = 0;
  1985. int index;
  1986. srb_t *sp;
  1987. int t;
  1988. /*
  1989. * Ports - Port down timer.
  1990. *
  1991. * Whenever, a port is in the LOST state we start decrementing its port
  1992. * down timer every second until it reaches zero. Once it reaches zero
  1993. * the port it marked DEAD.
  1994. */
  1995. t = 0;
  1996. list_for_each_entry(fcport, &ha->fcports, list) {
  1997. if (fcport->port_type != FCT_TARGET)
  1998. continue;
  1999. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2000. if (atomic_read(&fcport->port_down_timer) == 0)
  2001. continue;
  2002. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  2003. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  2004. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  2005. "%d remaining\n",
  2006. ha->host_no,
  2007. t, atomic_read(&fcport->port_down_timer)));
  2008. }
  2009. t++;
  2010. } /* End of for fcport */
  2011. /* Loop down handler. */
  2012. if (atomic_read(&ha->loop_down_timer) > 0 &&
  2013. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  2014. if (atomic_read(&ha->loop_down_timer) ==
  2015. ha->loop_down_abort_time) {
  2016. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  2017. "queues before time expire\n",
  2018. ha->host_no));
  2019. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  2020. atomic_set(&ha->loop_state, LOOP_DEAD);
  2021. /* Schedule an ISP abort to return any tape commands. */
  2022. spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
  2023. for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
  2024. index++) {
  2025. fc_port_t *sfcp;
  2026. sp = ha->outstanding_cmds[index];
  2027. if (!sp)
  2028. continue;
  2029. sfcp = sp->fcport;
  2030. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  2031. continue;
  2032. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2033. break;
  2034. }
  2035. spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
  2036. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  2037. start_dpc++;
  2038. }
  2039. /* if the loop has been down for 4 minutes, reinit adapter */
  2040. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  2041. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  2042. "restarting queues.\n",
  2043. ha->host_no));
  2044. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  2045. start_dpc++;
  2046. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  2047. DEBUG(printk("scsi(%ld): Loop down - "
  2048. "aborting ISP.\n",
  2049. ha->host_no));
  2050. qla_printk(KERN_WARNING, ha,
  2051. "Loop down - aborting ISP.\n");
  2052. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2053. }
  2054. }
  2055. DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
  2056. ha->host_no,
  2057. atomic_read(&ha->loop_down_timer)));
  2058. }
  2059. /* Schedule the DPC routine if needed */
  2060. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  2061. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  2062. start_dpc ||
  2063. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  2064. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  2065. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  2066. ha->dpc_wait && !ha->dpc_active) {
  2067. up(ha->dpc_wait);
  2068. }
  2069. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  2070. }
  2071. /* XXX(hch): crude hack to emulate a down_timeout() */
  2072. int
  2073. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  2074. {
  2075. const unsigned int step = HZ/10;
  2076. do {
  2077. if (!down_trylock(sema))
  2078. return 0;
  2079. set_current_state(TASK_INTERRUPTIBLE);
  2080. if (schedule_timeout(step))
  2081. break;
  2082. } while ((timeout -= step) > 0);
  2083. return -ETIMEDOUT;
  2084. }
  2085. static struct qla_board_info qla_board_tbl[] = {
  2086. {
  2087. .drv_name = "qla2400",
  2088. .isp_name = "ISP2422",
  2089. .fw_fname = "ql2400_fw.bin",
  2090. .sht = &qla24xx_driver_template,
  2091. },
  2092. {
  2093. .drv_name = "qla2400",
  2094. .isp_name = "ISP2432",
  2095. .fw_fname = "ql2400_fw.bin",
  2096. .sht = &qla24xx_driver_template,
  2097. },
  2098. };
  2099. static struct pci_device_id qla2xxx_pci_tbl[] = {
  2100. {
  2101. .vendor = PCI_VENDOR_ID_QLOGIC,
  2102. .device = PCI_DEVICE_ID_QLOGIC_ISP2422,
  2103. .subvendor = PCI_ANY_ID,
  2104. .subdevice = PCI_ANY_ID,
  2105. .driver_data = (unsigned long)&qla_board_tbl[0],
  2106. },
  2107. {
  2108. .vendor = PCI_VENDOR_ID_QLOGIC,
  2109. .device = PCI_DEVICE_ID_QLOGIC_ISP2432,
  2110. .subvendor = PCI_ANY_ID,
  2111. .subdevice = PCI_ANY_ID,
  2112. .driver_data = (unsigned long)&qla_board_tbl[1],
  2113. },
  2114. {0, 0},
  2115. };
  2116. MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
  2117. static int __devinit
  2118. qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2119. {
  2120. return qla2x00_probe_one(pdev,
  2121. (struct qla_board_info *)id->driver_data);
  2122. }
  2123. static void __devexit
  2124. qla2xxx_remove_one(struct pci_dev *pdev)
  2125. {
  2126. qla2x00_remove_one(pdev);
  2127. }
  2128. static struct pci_driver qla2xxx_pci_driver = {
  2129. .name = "qla2xxx",
  2130. .id_table = qla2xxx_pci_tbl,
  2131. .probe = qla2xxx_probe_one,
  2132. .remove = __devexit_p(qla2xxx_remove_one),
  2133. };
  2134. /**
  2135. * qla2x00_module_init - Module initialization.
  2136. **/
  2137. static int __init
  2138. qla2x00_module_init(void)
  2139. {
  2140. int ret = 0;
  2141. /* Allocate cache for SRBs. */
  2142. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  2143. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2144. if (srb_cachep == NULL) {
  2145. printk(KERN_ERR
  2146. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  2147. return -ENOMEM;
  2148. }
  2149. /* Derive version string. */
  2150. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  2151. #if DEBUG_QLA2100
  2152. strcat(qla2x00_version_str, "-debug");
  2153. #endif
  2154. qla2xxx_transport_template =
  2155. fc_attach_transport(&qla2xxx_transport_functions);
  2156. if (!qla2xxx_transport_template)
  2157. return -ENODEV;
  2158. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  2159. ret = pci_module_init(&qla2xxx_pci_driver);
  2160. if (ret) {
  2161. kmem_cache_destroy(srb_cachep);
  2162. fc_release_transport(qla2xxx_transport_template);
  2163. }
  2164. return ret;
  2165. }
  2166. /**
  2167. * qla2x00_module_exit - Module cleanup.
  2168. **/
  2169. static void __exit
  2170. qla2x00_module_exit(void)
  2171. {
  2172. pci_unregister_driver(&qla2xxx_pci_driver);
  2173. kmem_cache_destroy(srb_cachep);
  2174. fc_release_transport(qla2xxx_transport_template);
  2175. }
  2176. module_init(qla2x00_module_init);
  2177. module_exit(qla2x00_module_exit);
  2178. MODULE_AUTHOR("QLogic Corporation");
  2179. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  2180. MODULE_LICENSE("GPL");
  2181. MODULE_VERSION(QLA2XXX_VERSION);