qla_os.c 68 KB

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