qla_os.c 67 KB

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