qla_os.c 70 KB

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