qla_os.c 55 KB

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