qla_os.c 64 KB

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