qla_os.c 63 KB

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