qla_os.c 64 KB

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