qla_os.c 67 KB

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