qla_os.c 64 KB

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