qla_os.c 57 KB

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