qla_os.c 57 KB

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