qla_os.c 57 KB

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