qla_os.c 66 KB

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