ql4_os.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdontresethba = 0;
  28. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdontresethba,
  30. "Don't reset the HBA for driver recovery \n"
  31. " 0 - It will reset HBA (Default)\n"
  32. " 1 - It will NOT reset HBA");
  33. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  34. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(ql4xextended_error_logging,
  36. "Option to enable extended error logging, "
  37. "Default is 0 - no logging, 1 - debug logging");
  38. int ql4xenablemsix = 1;
  39. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(ql4xenablemsix,
  41. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  42. " 0 = enable INTx interrupt mechanism.\n"
  43. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  44. " 2 = enable MSI interrupt mechanism.");
  45. #define QL4_DEF_QDEPTH 32
  46. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  47. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(ql4xmaxqdepth,
  49. "Maximum queue depth to report for target devices.\n"
  50. " Default: 32.");
  51. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  52. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  53. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  54. "Target Session Recovery Timeout.\n"
  55. " Default: 30 sec.");
  56. /*
  57. * SCSI host template entry points
  58. */
  59. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  60. /*
  61. * iSCSI template entry points
  62. */
  63. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  64. enum iscsi_tgt_dscvr type, uint32_t enable,
  65. struct sockaddr *dst_addr);
  66. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  67. enum iscsi_param param, char *buf);
  68. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  69. enum iscsi_param param, char *buf);
  70. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  71. enum iscsi_host_param param, char *buf);
  72. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  73. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  74. /*
  75. * SCSI host template entry points
  76. */
  77. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  78. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  79. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  80. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  81. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  82. static int qla4xxx_slave_alloc(struct scsi_device *device);
  83. static int qla4xxx_slave_configure(struct scsi_device *device);
  84. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  85. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  86. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  87. QLA82XX_LEGACY_INTR_CONFIG;
  88. static struct scsi_host_template qla4xxx_driver_template = {
  89. .module = THIS_MODULE,
  90. .name = DRIVER_NAME,
  91. .proc_name = DRIVER_NAME,
  92. .queuecommand = qla4xxx_queuecommand,
  93. .eh_abort_handler = qla4xxx_eh_abort,
  94. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  95. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  96. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  97. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  98. .slave_configure = qla4xxx_slave_configure,
  99. .slave_alloc = qla4xxx_slave_alloc,
  100. .slave_destroy = qla4xxx_slave_destroy,
  101. .scan_finished = iscsi_scan_finished,
  102. .scan_start = qla4xxx_scan_start,
  103. .this_id = -1,
  104. .cmd_per_lun = 3,
  105. .use_clustering = ENABLE_CLUSTERING,
  106. .sg_tablesize = SG_ALL,
  107. .max_sectors = 0xFFFF,
  108. .shost_attrs = qla4xxx_host_attrs,
  109. };
  110. static struct iscsi_transport qla4xxx_iscsi_transport = {
  111. .owner = THIS_MODULE,
  112. .name = DRIVER_NAME,
  113. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  114. CAP_DATA_PATH_OFFLOAD,
  115. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  116. ISCSI_TARGET_NAME | ISCSI_TPGT |
  117. ISCSI_TARGET_ALIAS,
  118. .host_param_mask = ISCSI_HOST_HWADDRESS |
  119. ISCSI_HOST_IPADDRESS |
  120. ISCSI_HOST_INITIATOR_NAME,
  121. .tgt_dscvr = qla4xxx_tgt_dscvr,
  122. .get_conn_param = qla4xxx_conn_get_param,
  123. .get_session_param = qla4xxx_sess_get_param,
  124. .get_host_param = qla4xxx_host_get_param,
  125. .session_recovery_timedout = qla4xxx_recovery_timedout,
  126. };
  127. static struct scsi_transport_template *qla4xxx_scsi_transport;
  128. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  129. {
  130. struct iscsi_cls_session *session;
  131. struct ddb_entry *ddb_entry;
  132. session = starget_to_session(scsi_target(sc->device));
  133. ddb_entry = session->dd_data;
  134. /* if we are not logged in then the LLD is going to clean up the cmd */
  135. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  136. return BLK_EH_RESET_TIMER;
  137. else
  138. return BLK_EH_NOT_HANDLED;
  139. }
  140. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  141. {
  142. struct ddb_entry *ddb_entry = session->dd_data;
  143. struct scsi_qla_host *ha = ddb_entry->ha;
  144. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  145. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  146. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  147. "of (%d) secs exhausted, marking device DEAD.\n",
  148. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  149. ddb_entry->sess->recovery_tmo));
  150. }
  151. }
  152. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  153. enum iscsi_host_param param, char *buf)
  154. {
  155. struct scsi_qla_host *ha = to_qla_host(shost);
  156. int len;
  157. switch (param) {
  158. case ISCSI_HOST_PARAM_HWADDRESS:
  159. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  160. break;
  161. case ISCSI_HOST_PARAM_IPADDRESS:
  162. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  163. ha->ip_address[1], ha->ip_address[2],
  164. ha->ip_address[3]);
  165. break;
  166. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  167. len = sprintf(buf, "%s\n", ha->name_string);
  168. break;
  169. default:
  170. return -ENOSYS;
  171. }
  172. return len;
  173. }
  174. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  175. enum iscsi_param param, char *buf)
  176. {
  177. struct ddb_entry *ddb_entry = sess->dd_data;
  178. int len;
  179. switch (param) {
  180. case ISCSI_PARAM_TARGET_NAME:
  181. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  182. ddb_entry->iscsi_name);
  183. break;
  184. case ISCSI_PARAM_TPGT:
  185. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  186. break;
  187. case ISCSI_PARAM_TARGET_ALIAS:
  188. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  189. ddb_entry->iscsi_alias);
  190. break;
  191. default:
  192. return -ENOSYS;
  193. }
  194. return len;
  195. }
  196. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  197. enum iscsi_param param, char *buf)
  198. {
  199. struct iscsi_cls_session *session;
  200. struct ddb_entry *ddb_entry;
  201. int len;
  202. session = iscsi_dev_to_session(conn->dev.parent);
  203. ddb_entry = session->dd_data;
  204. switch (param) {
  205. case ISCSI_PARAM_CONN_PORT:
  206. len = sprintf(buf, "%hu\n", ddb_entry->port);
  207. break;
  208. case ISCSI_PARAM_CONN_ADDRESS:
  209. /* TODO: what are the ipv6 bits */
  210. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  211. break;
  212. default:
  213. return -ENOSYS;
  214. }
  215. return len;
  216. }
  217. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  218. enum iscsi_tgt_dscvr type, uint32_t enable,
  219. struct sockaddr *dst_addr)
  220. {
  221. struct scsi_qla_host *ha;
  222. struct sockaddr_in *addr;
  223. struct sockaddr_in6 *addr6;
  224. int ret = 0;
  225. ha = (struct scsi_qla_host *) shost->hostdata;
  226. switch (type) {
  227. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  228. if (dst_addr->sa_family == AF_INET) {
  229. addr = (struct sockaddr_in *)dst_addr;
  230. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  231. addr->sin_port) != QLA_SUCCESS)
  232. ret = -EIO;
  233. } else if (dst_addr->sa_family == AF_INET6) {
  234. /*
  235. * TODO: fix qla4xxx_send_tgts
  236. */
  237. addr6 = (struct sockaddr_in6 *)dst_addr;
  238. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  239. addr6->sin6_port) != QLA_SUCCESS)
  240. ret = -EIO;
  241. } else
  242. ret = -ENOSYS;
  243. break;
  244. default:
  245. ret = -ENOSYS;
  246. }
  247. return ret;
  248. }
  249. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  250. {
  251. if (!ddb_entry->sess)
  252. return;
  253. if (ddb_entry->conn) {
  254. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  255. iscsi_remove_session(ddb_entry->sess);
  256. }
  257. iscsi_free_session(ddb_entry->sess);
  258. }
  259. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  260. {
  261. int err;
  262. ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
  263. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  264. if (err) {
  265. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  266. return err;
  267. }
  268. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  269. if (!ddb_entry->conn) {
  270. iscsi_remove_session(ddb_entry->sess);
  271. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  272. return -ENOMEM;
  273. }
  274. /* finally ready to go */
  275. iscsi_unblock_session(ddb_entry->sess);
  276. return 0;
  277. }
  278. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  279. {
  280. struct ddb_entry *ddb_entry;
  281. struct iscsi_cls_session *sess;
  282. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  283. sizeof(struct ddb_entry));
  284. if (!sess)
  285. return NULL;
  286. ddb_entry = sess->dd_data;
  287. memset(ddb_entry, 0, sizeof(*ddb_entry));
  288. ddb_entry->ha = ha;
  289. ddb_entry->sess = sess;
  290. return ddb_entry;
  291. }
  292. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  293. {
  294. struct scsi_qla_host *ha = shost_priv(shost);
  295. struct ddb_entry *ddb_entry, *ddbtemp;
  296. /* finish setup of sessions that were already setup in firmware */
  297. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  298. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  299. qla4xxx_add_sess(ddb_entry);
  300. }
  301. }
  302. /*
  303. * Timer routines
  304. */
  305. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  306. unsigned long interval)
  307. {
  308. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  309. __func__, ha->host->host_no));
  310. init_timer(&ha->timer);
  311. ha->timer.expires = jiffies + interval * HZ;
  312. ha->timer.data = (unsigned long)ha;
  313. ha->timer.function = (void (*)(unsigned long))func;
  314. add_timer(&ha->timer);
  315. ha->timer_active = 1;
  316. }
  317. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  318. {
  319. del_timer_sync(&ha->timer);
  320. ha->timer_active = 0;
  321. }
  322. /***
  323. * qla4xxx_mark_device_missing - mark a device as missing.
  324. * @ha: Pointer to host adapter structure.
  325. * @ddb_entry: Pointer to device database entry
  326. *
  327. * This routine marks a device missing and close connection.
  328. **/
  329. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  330. struct ddb_entry *ddb_entry)
  331. {
  332. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  333. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  334. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  335. ha->host_no, ddb_entry->fw_ddb_index));
  336. } else
  337. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  338. ddb_entry->fw_ddb_index))
  339. iscsi_block_session(ddb_entry->sess);
  340. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  341. }
  342. /**
  343. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  344. * @ha: Pointer to host adapter structure.
  345. *
  346. * This routine marks a device missing and resets the relogin retry count.
  347. **/
  348. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  349. {
  350. struct ddb_entry *ddb_entry, *ddbtemp;
  351. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  352. qla4xxx_mark_device_missing(ha, ddb_entry);
  353. }
  354. }
  355. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  356. struct ddb_entry *ddb_entry,
  357. struct scsi_cmnd *cmd)
  358. {
  359. struct srb *srb;
  360. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  361. if (!srb)
  362. return srb;
  363. kref_init(&srb->srb_ref);
  364. srb->ha = ha;
  365. srb->ddb = ddb_entry;
  366. srb->cmd = cmd;
  367. srb->flags = 0;
  368. CMD_SP(cmd) = (void *)srb;
  369. return srb;
  370. }
  371. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  372. {
  373. struct scsi_cmnd *cmd = srb->cmd;
  374. if (srb->flags & SRB_DMA_VALID) {
  375. scsi_dma_unmap(cmd);
  376. srb->flags &= ~SRB_DMA_VALID;
  377. }
  378. CMD_SP(cmd) = NULL;
  379. }
  380. void qla4xxx_srb_compl(struct kref *ref)
  381. {
  382. struct srb *srb = container_of(ref, struct srb, srb_ref);
  383. struct scsi_cmnd *cmd = srb->cmd;
  384. struct scsi_qla_host *ha = srb->ha;
  385. qla4xxx_srb_free_dma(ha, srb);
  386. mempool_free(srb, ha->srb_mempool);
  387. cmd->scsi_done(cmd);
  388. }
  389. /**
  390. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  391. * @host: scsi host
  392. * @cmd: Pointer to Linux's SCSI command structure
  393. *
  394. * Remarks:
  395. * This routine is invoked by Linux to send a SCSI command to the driver.
  396. * The mid-level driver tries to ensure that queuecommand never gets
  397. * invoked concurrently with itself or the interrupt handler (although
  398. * the interrupt handler may call this routine as part of request-
  399. * completion handling). Unfortunely, it sometimes calls the scheduler
  400. * in interrupt context which is a big NO! NO!.
  401. **/
  402. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  403. {
  404. struct scsi_qla_host *ha = to_qla_host(host);
  405. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  406. struct iscsi_cls_session *sess = ddb_entry->sess;
  407. struct srb *srb;
  408. int rval;
  409. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  410. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  411. cmd->result = DID_NO_CONNECT << 16;
  412. else
  413. cmd->result = DID_REQUEUE << 16;
  414. goto qc_fail_command;
  415. }
  416. if (!sess) {
  417. cmd->result = DID_IMM_RETRY << 16;
  418. goto qc_fail_command;
  419. }
  420. rval = iscsi_session_chkready(sess);
  421. if (rval) {
  422. cmd->result = rval;
  423. goto qc_fail_command;
  424. }
  425. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  426. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  427. cmd->result = DID_NO_CONNECT << 16;
  428. goto qc_fail_command;
  429. }
  430. return SCSI_MLQUEUE_TARGET_BUSY;
  431. }
  432. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  433. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  434. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  435. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  436. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  437. !test_bit(AF_ONLINE, &ha->flags) ||
  438. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  439. goto qc_host_busy;
  440. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  441. if (!srb)
  442. goto qc_host_busy;
  443. rval = qla4xxx_send_command_to_isp(ha, srb);
  444. if (rval != QLA_SUCCESS)
  445. goto qc_host_busy_free_sp;
  446. return 0;
  447. qc_host_busy_free_sp:
  448. qla4xxx_srb_free_dma(ha, srb);
  449. mempool_free(srb, ha->srb_mempool);
  450. qc_host_busy:
  451. return SCSI_MLQUEUE_HOST_BUSY;
  452. qc_fail_command:
  453. cmd->scsi_done(cmd);
  454. return 0;
  455. }
  456. /**
  457. * qla4xxx_mem_free - frees memory allocated to adapter
  458. * @ha: Pointer to host adapter structure.
  459. *
  460. * Frees memory previously allocated by qla4xxx_mem_alloc
  461. **/
  462. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  463. {
  464. if (ha->queues)
  465. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  466. ha->queues_dma);
  467. ha->queues_len = 0;
  468. ha->queues = NULL;
  469. ha->queues_dma = 0;
  470. ha->request_ring = NULL;
  471. ha->request_dma = 0;
  472. ha->response_ring = NULL;
  473. ha->response_dma = 0;
  474. ha->shadow_regs = NULL;
  475. ha->shadow_regs_dma = 0;
  476. /* Free srb pool. */
  477. if (ha->srb_mempool)
  478. mempool_destroy(ha->srb_mempool);
  479. ha->srb_mempool = NULL;
  480. /* release io space registers */
  481. if (is_qla8022(ha)) {
  482. if (ha->nx_pcibase)
  483. iounmap(
  484. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  485. } else if (ha->reg)
  486. iounmap(ha->reg);
  487. pci_release_regions(ha->pdev);
  488. }
  489. /**
  490. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  491. * @ha: Pointer to host adapter structure
  492. *
  493. * Allocates DMA memory for request and response queues. Also allocates memory
  494. * for srbs.
  495. **/
  496. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  497. {
  498. unsigned long align;
  499. /* Allocate contiguous block of DMA memory for queues. */
  500. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  501. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  502. sizeof(struct shadow_regs) +
  503. MEM_ALIGN_VALUE +
  504. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  505. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  506. &ha->queues_dma, GFP_KERNEL);
  507. if (ha->queues == NULL) {
  508. ql4_printk(KERN_WARNING, ha,
  509. "Memory Allocation failed - queues.\n");
  510. goto mem_alloc_error_exit;
  511. }
  512. memset(ha->queues, 0, ha->queues_len);
  513. /*
  514. * As per RISC alignment requirements -- the bus-address must be a
  515. * multiple of the request-ring size (in bytes).
  516. */
  517. align = 0;
  518. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  519. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  520. (MEM_ALIGN_VALUE - 1));
  521. /* Update request and response queue pointers. */
  522. ha->request_dma = ha->queues_dma + align;
  523. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  524. ha->response_dma = ha->queues_dma + align +
  525. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  526. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  527. (REQUEST_QUEUE_DEPTH *
  528. QUEUE_SIZE));
  529. ha->shadow_regs_dma = ha->queues_dma + align +
  530. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  531. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  532. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  533. (REQUEST_QUEUE_DEPTH *
  534. QUEUE_SIZE) +
  535. (RESPONSE_QUEUE_DEPTH *
  536. QUEUE_SIZE));
  537. /* Allocate memory for srb pool. */
  538. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  539. mempool_free_slab, srb_cachep);
  540. if (ha->srb_mempool == NULL) {
  541. ql4_printk(KERN_WARNING, ha,
  542. "Memory Allocation failed - SRB Pool.\n");
  543. goto mem_alloc_error_exit;
  544. }
  545. return QLA_SUCCESS;
  546. mem_alloc_error_exit:
  547. qla4xxx_mem_free(ha);
  548. return QLA_ERROR;
  549. }
  550. /**
  551. * qla4_8xxx_check_fw_alive - Check firmware health
  552. * @ha: Pointer to host adapter structure.
  553. *
  554. * Context: Interrupt
  555. **/
  556. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  557. {
  558. uint32_t fw_heartbeat_counter, halt_status;
  559. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  560. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  561. if (fw_heartbeat_counter == 0xffffffff) {
  562. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  563. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  564. ha->host_no, __func__));
  565. return;
  566. }
  567. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  568. ha->seconds_since_last_heartbeat++;
  569. /* FW not alive after 2 seconds */
  570. if (ha->seconds_since_last_heartbeat == 2) {
  571. ha->seconds_since_last_heartbeat = 0;
  572. halt_status = qla4_8xxx_rd_32(ha,
  573. QLA82XX_PEG_HALT_STATUS1);
  574. ql4_printk(KERN_INFO, ha,
  575. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  576. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  577. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  578. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  579. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  580. ha->host_no, __func__, halt_status,
  581. qla4_8xxx_rd_32(ha,
  582. QLA82XX_PEG_HALT_STATUS2),
  583. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  584. 0x3c),
  585. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  586. 0x3c),
  587. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  588. 0x3c),
  589. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  590. 0x3c),
  591. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  592. 0x3c));
  593. /* Since we cannot change dev_state in interrupt
  594. * context, set appropriate DPC flag then wakeup
  595. * DPC */
  596. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  597. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  598. else {
  599. printk("scsi%ld: %s: detect abort needed!\n",
  600. ha->host_no, __func__);
  601. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  602. }
  603. qla4xxx_wake_dpc(ha);
  604. qla4xxx_mailbox_premature_completion(ha);
  605. }
  606. } else
  607. ha->seconds_since_last_heartbeat = 0;
  608. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  609. }
  610. /**
  611. * qla4_8xxx_watchdog - Poll dev state
  612. * @ha: Pointer to host adapter structure.
  613. *
  614. * Context: Interrupt
  615. **/
  616. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  617. {
  618. uint32_t dev_state;
  619. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  620. /* don't poll if reset is going on */
  621. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  622. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  623. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  624. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  625. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  626. if (!ql4xdontresethba) {
  627. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  628. "NEED RESET!\n", __func__);
  629. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  630. qla4xxx_wake_dpc(ha);
  631. qla4xxx_mailbox_premature_completion(ha);
  632. }
  633. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  634. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  635. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  636. __func__);
  637. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  638. qla4xxx_wake_dpc(ha);
  639. } else {
  640. /* Check firmware health */
  641. qla4_8xxx_check_fw_alive(ha);
  642. }
  643. }
  644. }
  645. /**
  646. * qla4xxx_timer - checks every second for work to do.
  647. * @ha: Pointer to host adapter structure.
  648. **/
  649. static void qla4xxx_timer(struct scsi_qla_host *ha)
  650. {
  651. struct ddb_entry *ddb_entry, *dtemp;
  652. int start_dpc = 0;
  653. uint16_t w;
  654. /* If we are in the middle of AER/EEH processing
  655. * skip any processing and reschedule the timer
  656. */
  657. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  658. mod_timer(&ha->timer, jiffies + HZ);
  659. return;
  660. }
  661. /* Hardware read to trigger an EEH error during mailbox waits. */
  662. if (!pci_channel_offline(ha->pdev))
  663. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  664. if (is_qla8022(ha)) {
  665. qla4_8xxx_watchdog(ha);
  666. }
  667. /* Search for relogin's to time-out and port down retry. */
  668. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  669. /* Count down time between sending relogins */
  670. if (adapter_up(ha) &&
  671. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  672. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  673. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  674. INVALID_ENTRY) {
  675. if (atomic_read(&ddb_entry->retry_relogin_timer)
  676. == 0) {
  677. atomic_set(&ddb_entry->
  678. retry_relogin_timer,
  679. INVALID_ENTRY);
  680. set_bit(DPC_RELOGIN_DEVICE,
  681. &ha->dpc_flags);
  682. set_bit(DF_RELOGIN, &ddb_entry->flags);
  683. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  684. " login device\n",
  685. ha->host_no, __func__,
  686. ddb_entry->fw_ddb_index));
  687. } else
  688. atomic_dec(&ddb_entry->
  689. retry_relogin_timer);
  690. }
  691. }
  692. /* Wait for relogin to timeout */
  693. if (atomic_read(&ddb_entry->relogin_timer) &&
  694. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  695. /*
  696. * If the relogin times out and the device is
  697. * still NOT ONLINE then try and relogin again.
  698. */
  699. if (atomic_read(&ddb_entry->state) !=
  700. DDB_STATE_ONLINE &&
  701. ddb_entry->fw_ddb_device_state ==
  702. DDB_DS_SESSION_FAILED) {
  703. /* Reset retry relogin timer */
  704. atomic_inc(&ddb_entry->relogin_retry_count);
  705. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  706. " timed out-retrying"
  707. " relogin (%d)\n",
  708. ha->host_no,
  709. ddb_entry->fw_ddb_index,
  710. atomic_read(&ddb_entry->
  711. relogin_retry_count))
  712. );
  713. start_dpc++;
  714. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  715. "initiate relogin after"
  716. " %d seconds\n",
  717. ha->host_no, ddb_entry->bus,
  718. ddb_entry->target,
  719. ddb_entry->fw_ddb_index,
  720. ddb_entry->default_time2wait + 4)
  721. );
  722. atomic_set(&ddb_entry->retry_relogin_timer,
  723. ddb_entry->default_time2wait + 4);
  724. }
  725. }
  726. }
  727. if (!is_qla8022(ha)) {
  728. /* Check for heartbeat interval. */
  729. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  730. ha->heartbeat_interval != 0) {
  731. ha->seconds_since_last_heartbeat++;
  732. if (ha->seconds_since_last_heartbeat >
  733. ha->heartbeat_interval + 2)
  734. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  735. }
  736. }
  737. /* Wakeup the dpc routine for this adapter, if needed. */
  738. if (start_dpc ||
  739. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  740. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  741. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  742. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  743. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  744. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  745. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  746. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  747. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  748. test_bit(DPC_AEN, &ha->dpc_flags)) {
  749. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  750. " - dpc flags = 0x%lx\n",
  751. ha->host_no, __func__, ha->dpc_flags));
  752. qla4xxx_wake_dpc(ha);
  753. }
  754. /* Reschedule timer thread to call us back in one second */
  755. mod_timer(&ha->timer, jiffies + HZ);
  756. DEBUG2(ha->seconds_since_last_intr++);
  757. }
  758. /**
  759. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  760. * @ha: Pointer to host adapter structure.
  761. *
  762. * This routine stalls the driver until all outstanding commands are returned.
  763. * Caller must release the Hardware Lock prior to calling this routine.
  764. **/
  765. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  766. {
  767. uint32_t index = 0;
  768. unsigned long flags;
  769. struct scsi_cmnd *cmd;
  770. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  771. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  772. "complete\n", WAIT_CMD_TOV));
  773. while (!time_after_eq(jiffies, wtime)) {
  774. spin_lock_irqsave(&ha->hardware_lock, flags);
  775. /* Find a command that hasn't completed. */
  776. for (index = 0; index < ha->host->can_queue; index++) {
  777. cmd = scsi_host_find_tag(ha->host, index);
  778. /*
  779. * We cannot just check if the index is valid,
  780. * becase if we are run from the scsi eh, then
  781. * the scsi/block layer is going to prevent
  782. * the tag from being released.
  783. */
  784. if (cmd != NULL && CMD_SP(cmd))
  785. break;
  786. }
  787. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  788. /* If No Commands are pending, wait is complete */
  789. if (index == ha->host->can_queue)
  790. return QLA_SUCCESS;
  791. msleep(1000);
  792. }
  793. /* If we timed out on waiting for commands to come back
  794. * return ERROR. */
  795. return QLA_ERROR;
  796. }
  797. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  798. {
  799. uint32_t ctrl_status;
  800. unsigned long flags = 0;
  801. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  802. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  803. return QLA_ERROR;
  804. spin_lock_irqsave(&ha->hardware_lock, flags);
  805. /*
  806. * If the SCSI Reset Interrupt bit is set, clear it.
  807. * Otherwise, the Soft Reset won't work.
  808. */
  809. ctrl_status = readw(&ha->reg->ctrl_status);
  810. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  811. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  812. /* Issue Soft Reset */
  813. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  814. readl(&ha->reg->ctrl_status);
  815. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  816. return QLA_SUCCESS;
  817. }
  818. /**
  819. * qla4xxx_soft_reset - performs soft reset.
  820. * @ha: Pointer to host adapter structure.
  821. **/
  822. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  823. {
  824. uint32_t max_wait_time;
  825. unsigned long flags = 0;
  826. int status;
  827. uint32_t ctrl_status;
  828. status = qla4xxx_hw_reset(ha);
  829. if (status != QLA_SUCCESS)
  830. return status;
  831. status = QLA_ERROR;
  832. /* Wait until the Network Reset Intr bit is cleared */
  833. max_wait_time = RESET_INTR_TOV;
  834. do {
  835. spin_lock_irqsave(&ha->hardware_lock, flags);
  836. ctrl_status = readw(&ha->reg->ctrl_status);
  837. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  838. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  839. break;
  840. msleep(1000);
  841. } while ((--max_wait_time));
  842. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  843. DEBUG2(printk(KERN_WARNING
  844. "scsi%ld: Network Reset Intr not cleared by "
  845. "Network function, clearing it now!\n",
  846. ha->host_no));
  847. spin_lock_irqsave(&ha->hardware_lock, flags);
  848. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  849. readl(&ha->reg->ctrl_status);
  850. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  851. }
  852. /* Wait until the firmware tells us the Soft Reset is done */
  853. max_wait_time = SOFT_RESET_TOV;
  854. do {
  855. spin_lock_irqsave(&ha->hardware_lock, flags);
  856. ctrl_status = readw(&ha->reg->ctrl_status);
  857. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  858. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  859. status = QLA_SUCCESS;
  860. break;
  861. }
  862. msleep(1000);
  863. } while ((--max_wait_time));
  864. /*
  865. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  866. * after the soft reset has taken place.
  867. */
  868. spin_lock_irqsave(&ha->hardware_lock, flags);
  869. ctrl_status = readw(&ha->reg->ctrl_status);
  870. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  871. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  872. readl(&ha->reg->ctrl_status);
  873. }
  874. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  875. /* If soft reset fails then most probably the bios on other
  876. * function is also enabled.
  877. * Since the initialization is sequential the other fn
  878. * wont be able to acknowledge the soft reset.
  879. * Issue a force soft reset to workaround this scenario.
  880. */
  881. if (max_wait_time == 0) {
  882. /* Issue Force Soft Reset */
  883. spin_lock_irqsave(&ha->hardware_lock, flags);
  884. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  885. readl(&ha->reg->ctrl_status);
  886. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  887. /* Wait until the firmware tells us the Soft Reset is done */
  888. max_wait_time = SOFT_RESET_TOV;
  889. do {
  890. spin_lock_irqsave(&ha->hardware_lock, flags);
  891. ctrl_status = readw(&ha->reg->ctrl_status);
  892. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  893. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  894. status = QLA_SUCCESS;
  895. break;
  896. }
  897. msleep(1000);
  898. } while ((--max_wait_time));
  899. }
  900. return status;
  901. }
  902. /**
  903. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  904. * @ha: Pointer to host adapter structure.
  905. * @res: returned scsi status
  906. *
  907. * This routine is called just prior to a HARD RESET to return all
  908. * outstanding commands back to the Operating System.
  909. * Caller should make sure that the following locks are released
  910. * before this calling routine: Hardware lock, and io_request_lock.
  911. **/
  912. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  913. {
  914. struct srb *srb;
  915. int i;
  916. unsigned long flags;
  917. spin_lock_irqsave(&ha->hardware_lock, flags);
  918. for (i = 0; i < ha->host->can_queue; i++) {
  919. srb = qla4xxx_del_from_active_array(ha, i);
  920. if (srb != NULL) {
  921. srb->cmd->result = res;
  922. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  923. }
  924. }
  925. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  926. }
  927. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  928. {
  929. clear_bit(AF_ONLINE, &ha->flags);
  930. /* Disable the board */
  931. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  932. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  933. qla4xxx_mark_all_devices_missing(ha);
  934. clear_bit(AF_INIT_DONE, &ha->flags);
  935. }
  936. /**
  937. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  938. * @ha: Pointer to host adapter structure.
  939. **/
  940. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  941. {
  942. int status = QLA_ERROR;
  943. uint8_t reset_chip = 0;
  944. /* Stall incoming I/O until we are done */
  945. scsi_block_requests(ha->host);
  946. clear_bit(AF_ONLINE, &ha->flags);
  947. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  948. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  949. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  950. reset_chip = 1;
  951. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  952. * do not reset adapter, jump to initialize_adapter */
  953. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  954. status = QLA_SUCCESS;
  955. goto recover_ha_init_adapter;
  956. }
  957. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  958. * from eh_host_reset or ioctl module */
  959. if (is_qla8022(ha) && !reset_chip &&
  960. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  961. DEBUG2(ql4_printk(KERN_INFO, ha,
  962. "scsi%ld: %s - Performing stop_firmware...\n",
  963. ha->host_no, __func__));
  964. status = ha->isp_ops->reset_firmware(ha);
  965. if (status == QLA_SUCCESS) {
  966. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  967. qla4xxx_cmd_wait(ha);
  968. ha->isp_ops->disable_intrs(ha);
  969. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  970. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  971. } else {
  972. /* If the stop_firmware fails then
  973. * reset the entire chip */
  974. reset_chip = 1;
  975. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  976. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  977. }
  978. }
  979. /* Issue full chip reset if recovering from a catastrophic error,
  980. * or if stop_firmware fails for ISP-82xx.
  981. * This is the default case for ISP-4xxx */
  982. if (!is_qla8022(ha) || reset_chip) {
  983. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  984. qla4xxx_cmd_wait(ha);
  985. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  986. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  987. DEBUG2(ql4_printk(KERN_INFO, ha,
  988. "scsi%ld: %s - Performing chip reset..\n",
  989. ha->host_no, __func__));
  990. status = ha->isp_ops->reset_chip(ha);
  991. }
  992. /* Flush any pending ddb changed AENs */
  993. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  994. recover_ha_init_adapter:
  995. /* Upon successful firmware/chip reset, re-initialize the adapter */
  996. if (status == QLA_SUCCESS) {
  997. /* For ISP-4xxx, force function 1 to always initialize
  998. * before function 3 to prevent both funcions from
  999. * stepping on top of the other */
  1000. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1001. ssleep(6);
  1002. /* NOTE: AF_ONLINE flag set upon successful completion of
  1003. * qla4xxx_initialize_adapter */
  1004. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  1005. }
  1006. /* Retry failed adapter initialization, if necessary
  1007. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1008. * case to prevent ping-pong resets between functions */
  1009. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1010. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1011. /* Adapter initialization failed, see if we can retry
  1012. * resetting the ha.
  1013. * Since we don't want to block the DPC for too long
  1014. * with multiple resets in the same thread,
  1015. * utilize DPC to retry */
  1016. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1017. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1018. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1019. "(%d) more times\n", ha->host_no,
  1020. ha->retry_reset_ha_cnt));
  1021. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1022. status = QLA_ERROR;
  1023. } else {
  1024. if (ha->retry_reset_ha_cnt > 0) {
  1025. /* Schedule another Reset HA--DPC will retry */
  1026. ha->retry_reset_ha_cnt--;
  1027. DEBUG2(printk("scsi%ld: recover adapter - "
  1028. "retry remaining %d\n",
  1029. ha->host_no,
  1030. ha->retry_reset_ha_cnt));
  1031. status = QLA_ERROR;
  1032. }
  1033. if (ha->retry_reset_ha_cnt == 0) {
  1034. /* Recover adapter retries have been exhausted.
  1035. * Adapter DEAD */
  1036. DEBUG2(printk("scsi%ld: recover adapter "
  1037. "failed - board disabled\n",
  1038. ha->host_no));
  1039. qla4xxx_dead_adapter_cleanup(ha);
  1040. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1041. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1042. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1043. &ha->dpc_flags);
  1044. status = QLA_ERROR;
  1045. }
  1046. }
  1047. } else {
  1048. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1049. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1050. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1051. }
  1052. ha->adapter_error_count++;
  1053. if (test_bit(AF_ONLINE, &ha->flags))
  1054. ha->isp_ops->enable_intrs(ha);
  1055. scsi_unblock_requests(ha->host);
  1056. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1057. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1058. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1059. return status;
  1060. }
  1061. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1062. {
  1063. struct ddb_entry *ddb_entry, *dtemp;
  1064. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  1065. if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
  1066. (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
  1067. if (ddb_entry->fw_ddb_device_state ==
  1068. DDB_DS_SESSION_ACTIVE) {
  1069. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1070. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1071. " marked ONLINE\n", ha->host_no, __func__,
  1072. ddb_entry->fw_ddb_index);
  1073. iscsi_unblock_session(ddb_entry->sess);
  1074. } else
  1075. qla4xxx_relogin_device(ha, ddb_entry);
  1076. }
  1077. }
  1078. }
  1079. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1080. {
  1081. if (ha->dpc_thread)
  1082. queue_work(ha->dpc_thread, &ha->dpc_work);
  1083. }
  1084. /**
  1085. * qla4xxx_do_dpc - dpc routine
  1086. * @data: in our case pointer to adapter structure
  1087. *
  1088. * This routine is a task that is schedule by the interrupt handler
  1089. * to perform the background processing for interrupts. We put it
  1090. * on a task queue that is consumed whenever the scheduler runs; that's
  1091. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1092. * the mid-level tries to sleep when it reaches the driver threshold
  1093. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1094. **/
  1095. static void qla4xxx_do_dpc(struct work_struct *work)
  1096. {
  1097. struct scsi_qla_host *ha =
  1098. container_of(work, struct scsi_qla_host, dpc_work);
  1099. struct ddb_entry *ddb_entry, *dtemp;
  1100. int status = QLA_ERROR;
  1101. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1102. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1103. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1104. /* Initialization not yet finished. Don't do anything yet. */
  1105. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1106. return;
  1107. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1108. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1109. ha->host_no, __func__, ha->flags));
  1110. return;
  1111. }
  1112. if (is_qla8022(ha)) {
  1113. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1114. qla4_8xxx_idc_lock(ha);
  1115. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1116. QLA82XX_DEV_FAILED);
  1117. qla4_8xxx_idc_unlock(ha);
  1118. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1119. qla4_8xxx_device_state_handler(ha);
  1120. }
  1121. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1122. qla4_8xxx_need_qsnt_handler(ha);
  1123. }
  1124. }
  1125. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1126. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1127. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1128. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1129. if (ql4xdontresethba) {
  1130. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1131. ha->host_no, __func__));
  1132. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1133. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1134. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1135. goto dpc_post_reset_ha;
  1136. }
  1137. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1138. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1139. qla4xxx_recover_adapter(ha);
  1140. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1141. uint8_t wait_time = RESET_INTR_TOV;
  1142. while ((readw(&ha->reg->ctrl_status) &
  1143. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1144. if (--wait_time == 0)
  1145. break;
  1146. msleep(1000);
  1147. }
  1148. if (wait_time == 0)
  1149. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1150. "bit not cleared-- resetting\n",
  1151. ha->host_no, __func__));
  1152. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1153. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1154. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1155. status = qla4xxx_recover_adapter(ha);
  1156. }
  1157. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1158. if (status == QLA_SUCCESS)
  1159. ha->isp_ops->enable_intrs(ha);
  1160. }
  1161. }
  1162. dpc_post_reset_ha:
  1163. /* ---- process AEN? --- */
  1164. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1165. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1166. /* ---- Get DHCP IP Address? --- */
  1167. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1168. qla4xxx_get_dhcp_ip_address(ha);
  1169. /* ---- link change? --- */
  1170. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1171. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1172. /* ---- link down? --- */
  1173. qla4xxx_mark_all_devices_missing(ha);
  1174. } else {
  1175. /* ---- link up? --- *
  1176. * F/W will auto login to all devices ONLY ONCE after
  1177. * link up during driver initialization and runtime
  1178. * fatal error recovery. Therefore, the driver must
  1179. * manually relogin to devices when recovering from
  1180. * connection failures, logouts, expired KATO, etc. */
  1181. qla4xxx_relogin_all_devices(ha);
  1182. }
  1183. }
  1184. /* ---- relogin device? --- */
  1185. if (adapter_up(ha) &&
  1186. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1187. list_for_each_entry_safe(ddb_entry, dtemp,
  1188. &ha->ddb_list, list) {
  1189. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1190. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1191. qla4xxx_relogin_device(ha, ddb_entry);
  1192. /*
  1193. * If mbx cmd times out there is no point
  1194. * in continuing further.
  1195. * With large no of targets this can hang
  1196. * the system.
  1197. */
  1198. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1199. printk(KERN_WARNING "scsi%ld: %s: "
  1200. "need to reset hba\n",
  1201. ha->host_no, __func__);
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. }
  1207. /**
  1208. * qla4xxx_free_adapter - release the adapter
  1209. * @ha: pointer to adapter structure
  1210. **/
  1211. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1212. {
  1213. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1214. /* Turn-off interrupts on the card. */
  1215. ha->isp_ops->disable_intrs(ha);
  1216. }
  1217. /* Remove timer thread, if present */
  1218. if (ha->timer_active)
  1219. qla4xxx_stop_timer(ha);
  1220. /* Kill the kernel thread for this host */
  1221. if (ha->dpc_thread)
  1222. destroy_workqueue(ha->dpc_thread);
  1223. /* Put firmware in known state */
  1224. ha->isp_ops->reset_firmware(ha);
  1225. if (is_qla8022(ha)) {
  1226. qla4_8xxx_idc_lock(ha);
  1227. qla4_8xxx_clear_drv_active(ha);
  1228. qla4_8xxx_idc_unlock(ha);
  1229. }
  1230. /* Detach interrupts */
  1231. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1232. qla4xxx_free_irqs(ha);
  1233. /* free extra memory */
  1234. qla4xxx_mem_free(ha);
  1235. }
  1236. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1237. {
  1238. int status = 0;
  1239. uint8_t revision_id;
  1240. unsigned long mem_base, mem_len, db_base, db_len;
  1241. struct pci_dev *pdev = ha->pdev;
  1242. status = pci_request_regions(pdev, DRIVER_NAME);
  1243. if (status) {
  1244. printk(KERN_WARNING
  1245. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1246. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1247. goto iospace_error_exit;
  1248. }
  1249. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1250. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1251. __func__, revision_id));
  1252. ha->revision_id = revision_id;
  1253. /* remap phys address */
  1254. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1255. mem_len = pci_resource_len(pdev, 0);
  1256. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1257. __func__, mem_base, mem_len));
  1258. /* mapping of pcibase pointer */
  1259. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1260. if (!ha->nx_pcibase) {
  1261. printk(KERN_ERR
  1262. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1263. pci_release_regions(ha->pdev);
  1264. goto iospace_error_exit;
  1265. }
  1266. /* Mapping of IO base pointer, door bell read and write pointer */
  1267. /* mapping of IO base pointer */
  1268. ha->qla4_8xxx_reg =
  1269. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1270. 0xbc000 + (ha->pdev->devfn << 11));
  1271. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1272. db_len = pci_resource_len(pdev, 4);
  1273. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  1274. QLA82XX_CAM_RAM_DB2);
  1275. return 0;
  1276. iospace_error_exit:
  1277. return -ENOMEM;
  1278. }
  1279. /***
  1280. * qla4xxx_iospace_config - maps registers
  1281. * @ha: pointer to adapter structure
  1282. *
  1283. * This routines maps HBA's registers from the pci address space
  1284. * into the kernel virtual address space for memory mapped i/o.
  1285. **/
  1286. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1287. {
  1288. unsigned long pio, pio_len, pio_flags;
  1289. unsigned long mmio, mmio_len, mmio_flags;
  1290. pio = pci_resource_start(ha->pdev, 0);
  1291. pio_len = pci_resource_len(ha->pdev, 0);
  1292. pio_flags = pci_resource_flags(ha->pdev, 0);
  1293. if (pio_flags & IORESOURCE_IO) {
  1294. if (pio_len < MIN_IOBASE_LEN) {
  1295. ql4_printk(KERN_WARNING, ha,
  1296. "Invalid PCI I/O region size\n");
  1297. pio = 0;
  1298. }
  1299. } else {
  1300. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1301. pio = 0;
  1302. }
  1303. /* Use MMIO operations for all accesses. */
  1304. mmio = pci_resource_start(ha->pdev, 1);
  1305. mmio_len = pci_resource_len(ha->pdev, 1);
  1306. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1307. if (!(mmio_flags & IORESOURCE_MEM)) {
  1308. ql4_printk(KERN_ERR, ha,
  1309. "region #0 not an MMIO resource, aborting\n");
  1310. goto iospace_error_exit;
  1311. }
  1312. if (mmio_len < MIN_IOBASE_LEN) {
  1313. ql4_printk(KERN_ERR, ha,
  1314. "Invalid PCI mem region size, aborting\n");
  1315. goto iospace_error_exit;
  1316. }
  1317. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1318. ql4_printk(KERN_WARNING, ha,
  1319. "Failed to reserve PIO/MMIO regions\n");
  1320. goto iospace_error_exit;
  1321. }
  1322. ha->pio_address = pio;
  1323. ha->pio_length = pio_len;
  1324. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1325. if (!ha->reg) {
  1326. ql4_printk(KERN_ERR, ha,
  1327. "cannot remap MMIO, aborting\n");
  1328. goto iospace_error_exit;
  1329. }
  1330. return 0;
  1331. iospace_error_exit:
  1332. return -ENOMEM;
  1333. }
  1334. static struct isp_operations qla4xxx_isp_ops = {
  1335. .iospace_config = qla4xxx_iospace_config,
  1336. .pci_config = qla4xxx_pci_config,
  1337. .disable_intrs = qla4xxx_disable_intrs,
  1338. .enable_intrs = qla4xxx_enable_intrs,
  1339. .start_firmware = qla4xxx_start_firmware,
  1340. .intr_handler = qla4xxx_intr_handler,
  1341. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1342. .reset_chip = qla4xxx_soft_reset,
  1343. .reset_firmware = qla4xxx_hw_reset,
  1344. .queue_iocb = qla4xxx_queue_iocb,
  1345. .complete_iocb = qla4xxx_complete_iocb,
  1346. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1347. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1348. .get_sys_info = qla4xxx_get_sys_info,
  1349. };
  1350. static struct isp_operations qla4_8xxx_isp_ops = {
  1351. .iospace_config = qla4_8xxx_iospace_config,
  1352. .pci_config = qla4_8xxx_pci_config,
  1353. .disable_intrs = qla4_8xxx_disable_intrs,
  1354. .enable_intrs = qla4_8xxx_enable_intrs,
  1355. .start_firmware = qla4_8xxx_load_risc,
  1356. .intr_handler = qla4_8xxx_intr_handler,
  1357. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1358. .reset_chip = qla4_8xxx_isp_reset,
  1359. .reset_firmware = qla4_8xxx_stop_firmware,
  1360. .queue_iocb = qla4_8xxx_queue_iocb,
  1361. .complete_iocb = qla4_8xxx_complete_iocb,
  1362. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1363. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1364. .get_sys_info = qla4_8xxx_get_sys_info,
  1365. };
  1366. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1367. {
  1368. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1369. }
  1370. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1371. {
  1372. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1373. }
  1374. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1375. {
  1376. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1377. }
  1378. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1379. {
  1380. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1381. }
  1382. /**
  1383. * qla4xxx_probe_adapter - callback function to probe HBA
  1384. * @pdev: pointer to pci_dev structure
  1385. * @pci_device_id: pointer to pci_device entry
  1386. *
  1387. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1388. * It returns zero if successful. It also initializes all data necessary for
  1389. * the driver.
  1390. **/
  1391. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1392. const struct pci_device_id *ent)
  1393. {
  1394. int ret = -ENODEV, status;
  1395. struct Scsi_Host *host;
  1396. struct scsi_qla_host *ha;
  1397. uint8_t init_retry_count = 0;
  1398. char buf[34];
  1399. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1400. uint32_t dev_state;
  1401. if (pci_enable_device(pdev))
  1402. return -1;
  1403. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1404. if (host == NULL) {
  1405. printk(KERN_WARNING
  1406. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1407. goto probe_disable_device;
  1408. }
  1409. /* Clear our data area */
  1410. ha = (struct scsi_qla_host *) host->hostdata;
  1411. memset(ha, 0, sizeof(*ha));
  1412. /* Save the information from PCI BIOS. */
  1413. ha->pdev = pdev;
  1414. ha->host = host;
  1415. ha->host_no = host->host_no;
  1416. pci_enable_pcie_error_reporting(pdev);
  1417. /* Setup Runtime configurable options */
  1418. if (is_qla8022(ha)) {
  1419. ha->isp_ops = &qla4_8xxx_isp_ops;
  1420. rwlock_init(&ha->hw_lock);
  1421. ha->qdr_sn_window = -1;
  1422. ha->ddr_mn_window = -1;
  1423. ha->curr_window = 255;
  1424. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1425. nx_legacy_intr = &legacy_intr[ha->func_num];
  1426. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1427. ha->nx_legacy_intr.tgt_status_reg =
  1428. nx_legacy_intr->tgt_status_reg;
  1429. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1430. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1431. } else {
  1432. ha->isp_ops = &qla4xxx_isp_ops;
  1433. }
  1434. /* Set EEH reset type to fundamental if required by hba */
  1435. if (is_qla8022(ha))
  1436. pdev->needs_freset = 1;
  1437. /* Configure PCI I/O space. */
  1438. ret = ha->isp_ops->iospace_config(ha);
  1439. if (ret)
  1440. goto probe_failed_ioconfig;
  1441. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1442. pdev->device, pdev->irq, ha->reg);
  1443. qla4xxx_config_dma_addressing(ha);
  1444. /* Initialize lists and spinlocks. */
  1445. INIT_LIST_HEAD(&ha->ddb_list);
  1446. INIT_LIST_HEAD(&ha->free_srb_q);
  1447. mutex_init(&ha->mbox_sem);
  1448. init_completion(&ha->mbx_intr_comp);
  1449. spin_lock_init(&ha->hardware_lock);
  1450. /* Allocate dma buffers */
  1451. if (qla4xxx_mem_alloc(ha)) {
  1452. ql4_printk(KERN_WARNING, ha,
  1453. "[ERROR] Failed to allocate memory for adapter\n");
  1454. ret = -ENOMEM;
  1455. goto probe_failed;
  1456. }
  1457. if (is_qla8022(ha))
  1458. (void) qla4_8xxx_get_flash_info(ha);
  1459. /*
  1460. * Initialize the Host adapter request/response queues and
  1461. * firmware
  1462. * NOTE: interrupts enabled upon successful completion
  1463. */
  1464. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1465. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1466. init_retry_count++ < MAX_INIT_RETRIES) {
  1467. if (is_qla8022(ha)) {
  1468. qla4_8xxx_idc_lock(ha);
  1469. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1470. qla4_8xxx_idc_unlock(ha);
  1471. if (dev_state == QLA82XX_DEV_FAILED) {
  1472. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  1473. "initialize adapter. H/W is in failed state\n",
  1474. __func__);
  1475. break;
  1476. }
  1477. }
  1478. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1479. "(%d)\n", __func__, init_retry_count));
  1480. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1481. continue;
  1482. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1483. }
  1484. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1485. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1486. if (is_qla8022(ha) && ql4xdontresethba) {
  1487. /* Put the device in failed state. */
  1488. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  1489. qla4_8xxx_idc_lock(ha);
  1490. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1491. QLA82XX_DEV_FAILED);
  1492. qla4_8xxx_idc_unlock(ha);
  1493. }
  1494. ret = -ENODEV;
  1495. goto probe_failed;
  1496. }
  1497. host->cmd_per_lun = 3;
  1498. host->max_channel = 0;
  1499. host->max_lun = MAX_LUNS - 1;
  1500. host->max_id = MAX_TARGETS;
  1501. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1502. host->can_queue = MAX_SRBS ;
  1503. host->transportt = qla4xxx_scsi_transport;
  1504. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1505. if (ret) {
  1506. ql4_printk(KERN_WARNING, ha,
  1507. "scsi_init_shared_tag_map failed\n");
  1508. goto probe_failed;
  1509. }
  1510. /* Startup the kernel thread for this host adapter. */
  1511. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1512. "qla4xxx_dpc\n", __func__));
  1513. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1514. ha->dpc_thread = create_singlethread_workqueue(buf);
  1515. if (!ha->dpc_thread) {
  1516. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1517. ret = -ENODEV;
  1518. goto probe_failed;
  1519. }
  1520. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1521. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1522. * (which is called indirectly by qla4xxx_initialize_adapter),
  1523. * so that irqs will be registered after crbinit but before
  1524. * mbx_intr_enable.
  1525. */
  1526. if (!is_qla8022(ha)) {
  1527. ret = qla4xxx_request_irqs(ha);
  1528. if (ret) {
  1529. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1530. "interrupt %d already in use.\n", pdev->irq);
  1531. goto probe_failed;
  1532. }
  1533. }
  1534. pci_save_state(ha->pdev);
  1535. ha->isp_ops->enable_intrs(ha);
  1536. /* Start timer thread. */
  1537. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1538. set_bit(AF_INIT_DONE, &ha->flags);
  1539. pci_set_drvdata(pdev, ha);
  1540. ret = scsi_add_host(host, &pdev->dev);
  1541. if (ret)
  1542. goto probe_failed;
  1543. printk(KERN_INFO
  1544. " QLogic iSCSI HBA Driver version: %s\n"
  1545. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1546. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1547. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1548. ha->patch_number, ha->build_number);
  1549. scsi_scan_host(host);
  1550. return 0;
  1551. probe_failed:
  1552. qla4xxx_free_adapter(ha);
  1553. probe_failed_ioconfig:
  1554. pci_disable_pcie_error_reporting(pdev);
  1555. scsi_host_put(ha->host);
  1556. probe_disable_device:
  1557. pci_disable_device(pdev);
  1558. return ret;
  1559. }
  1560. /**
  1561. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  1562. * @ha: pointer to adapter structure
  1563. *
  1564. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  1565. * so that the other port will not re-initialize while in the process of
  1566. * removing the ha due to driver unload or hba hotplug.
  1567. **/
  1568. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  1569. {
  1570. struct scsi_qla_host *other_ha = NULL;
  1571. struct pci_dev *other_pdev = NULL;
  1572. int fn = ISP4XXX_PCI_FN_2;
  1573. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  1574. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  1575. fn = ISP4XXX_PCI_FN_1;
  1576. other_pdev =
  1577. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1578. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1579. fn));
  1580. /* Get other_ha if other_pdev is valid and state is enable*/
  1581. if (other_pdev) {
  1582. if (atomic_read(&other_pdev->enable_cnt)) {
  1583. other_ha = pci_get_drvdata(other_pdev);
  1584. if (other_ha) {
  1585. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  1586. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  1587. "Prevent %s reinit\n", __func__,
  1588. dev_name(&other_ha->pdev->dev)));
  1589. }
  1590. }
  1591. pci_dev_put(other_pdev);
  1592. }
  1593. }
  1594. /**
  1595. * qla4xxx_remove_adapter - calback function to remove adapter.
  1596. * @pci_dev: PCI device pointer
  1597. **/
  1598. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1599. {
  1600. struct scsi_qla_host *ha;
  1601. ha = pci_get_drvdata(pdev);
  1602. if (!is_qla8022(ha))
  1603. qla4xxx_prevent_other_port_reinit(ha);
  1604. /* remove devs from iscsi_sessions to scsi_devices */
  1605. qla4xxx_free_ddb_list(ha);
  1606. scsi_remove_host(ha->host);
  1607. qla4xxx_free_adapter(ha);
  1608. scsi_host_put(ha->host);
  1609. pci_disable_pcie_error_reporting(pdev);
  1610. pci_disable_device(pdev);
  1611. pci_set_drvdata(pdev, NULL);
  1612. }
  1613. /**
  1614. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1615. * @ha: HA context
  1616. *
  1617. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1618. * supported addressing method.
  1619. */
  1620. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1621. {
  1622. int retval;
  1623. /* Update our PCI device dma_mask for full 64 bit mask */
  1624. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  1625. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  1626. dev_dbg(&ha->pdev->dev,
  1627. "Failed to set 64 bit PCI consistent mask; "
  1628. "using 32 bit.\n");
  1629. retval = pci_set_consistent_dma_mask(ha->pdev,
  1630. DMA_BIT_MASK(32));
  1631. }
  1632. } else
  1633. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  1634. }
  1635. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1636. {
  1637. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1638. struct ddb_entry *ddb = sess->dd_data;
  1639. int queue_depth = QL4_DEF_QDEPTH;
  1640. sdev->hostdata = ddb;
  1641. sdev->tagged_supported = 1;
  1642. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  1643. queue_depth = ql4xmaxqdepth;
  1644. scsi_activate_tcq(sdev, queue_depth);
  1645. return 0;
  1646. }
  1647. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1648. {
  1649. sdev->tagged_supported = 1;
  1650. return 0;
  1651. }
  1652. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1653. {
  1654. scsi_deactivate_tcq(sdev, 1);
  1655. }
  1656. /**
  1657. * qla4xxx_del_from_active_array - returns an active srb
  1658. * @ha: Pointer to host adapter structure.
  1659. * @index: index into the active_array
  1660. *
  1661. * This routine removes and returns the srb at the specified index
  1662. **/
  1663. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  1664. uint32_t index)
  1665. {
  1666. struct srb *srb = NULL;
  1667. struct scsi_cmnd *cmd = NULL;
  1668. cmd = scsi_host_find_tag(ha->host, index);
  1669. if (!cmd)
  1670. return srb;
  1671. srb = (struct srb *)CMD_SP(cmd);
  1672. if (!srb)
  1673. return srb;
  1674. /* update counters */
  1675. if (srb->flags & SRB_DMA_VALID) {
  1676. ha->req_q_count += srb->iocb_cnt;
  1677. ha->iocb_cnt -= srb->iocb_cnt;
  1678. if (srb->cmd)
  1679. srb->cmd->host_scribble =
  1680. (unsigned char *)(unsigned long) MAX_SRBS;
  1681. }
  1682. return srb;
  1683. }
  1684. /**
  1685. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1686. * @ha: Pointer to host adapter structure.
  1687. * @cmd: Scsi Command to wait on.
  1688. *
  1689. * This routine waits for the command to be returned by the Firmware
  1690. * for some max time.
  1691. **/
  1692. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1693. struct scsi_cmnd *cmd)
  1694. {
  1695. int done = 0;
  1696. struct srb *rp;
  1697. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1698. int ret = SUCCESS;
  1699. /* Dont wait on command if PCI error is being handled
  1700. * by PCI AER driver
  1701. */
  1702. if (unlikely(pci_channel_offline(ha->pdev)) ||
  1703. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  1704. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  1705. ha->host_no, __func__);
  1706. return ret;
  1707. }
  1708. do {
  1709. /* Checking to see if its returned to OS */
  1710. rp = (struct srb *) CMD_SP(cmd);
  1711. if (rp == NULL) {
  1712. done++;
  1713. break;
  1714. }
  1715. msleep(2000);
  1716. } while (max_wait_time--);
  1717. return done;
  1718. }
  1719. /**
  1720. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1721. * @ha: Pointer to host adapter structure
  1722. **/
  1723. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1724. {
  1725. unsigned long wait_online;
  1726. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  1727. while (time_before(jiffies, wait_online)) {
  1728. if (adapter_up(ha))
  1729. return QLA_SUCCESS;
  1730. msleep(2000);
  1731. }
  1732. return QLA_ERROR;
  1733. }
  1734. /**
  1735. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  1736. * @ha: pointer to HBA
  1737. * @t: target id
  1738. * @l: lun id
  1739. *
  1740. * This function waits for all outstanding commands to a lun to complete. It
  1741. * returns 0 if all pending commands are returned and 1 otherwise.
  1742. **/
  1743. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  1744. struct scsi_target *stgt,
  1745. struct scsi_device *sdev)
  1746. {
  1747. int cnt;
  1748. int status = 0;
  1749. struct scsi_cmnd *cmd;
  1750. /*
  1751. * Waiting for all commands for the designated target or dev
  1752. * in the active array
  1753. */
  1754. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1755. cmd = scsi_host_find_tag(ha->host, cnt);
  1756. if (cmd && stgt == scsi_target(cmd->device) &&
  1757. (!sdev || sdev == cmd->device)) {
  1758. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1759. status++;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. return status;
  1765. }
  1766. /**
  1767. * qla4xxx_eh_abort - callback for abort task.
  1768. * @cmd: Pointer to Linux's SCSI command structure
  1769. *
  1770. * This routine is called by the Linux OS to abort the specified
  1771. * command.
  1772. **/
  1773. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  1774. {
  1775. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1776. unsigned int id = cmd->device->id;
  1777. unsigned int lun = cmd->device->lun;
  1778. unsigned long flags;
  1779. struct srb *srb = NULL;
  1780. int ret = SUCCESS;
  1781. int wait = 0;
  1782. ql4_printk(KERN_INFO, ha,
  1783. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  1784. ha->host_no, id, lun, cmd);
  1785. spin_lock_irqsave(&ha->hardware_lock, flags);
  1786. srb = (struct srb *) CMD_SP(cmd);
  1787. if (!srb) {
  1788. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1789. return SUCCESS;
  1790. }
  1791. kref_get(&srb->srb_ref);
  1792. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1793. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  1794. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  1795. ha->host_no, id, lun));
  1796. ret = FAILED;
  1797. } else {
  1798. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  1799. ha->host_no, id, lun));
  1800. wait = 1;
  1801. }
  1802. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1803. /* Wait for command to complete */
  1804. if (wait) {
  1805. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1806. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  1807. ha->host_no, id, lun));
  1808. ret = FAILED;
  1809. }
  1810. }
  1811. ql4_printk(KERN_INFO, ha,
  1812. "scsi%ld:%d:%d: Abort command - %s\n",
  1813. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  1814. return ret;
  1815. }
  1816. /**
  1817. * qla4xxx_eh_device_reset - callback for target reset.
  1818. * @cmd: Pointer to Linux's SCSI command structure
  1819. *
  1820. * This routine is called by the Linux OS to reset all luns on the
  1821. * specified target.
  1822. **/
  1823. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1824. {
  1825. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1826. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1827. int ret = FAILED, stat;
  1828. if (!ddb_entry)
  1829. return ret;
  1830. ret = iscsi_block_scsi_eh(cmd);
  1831. if (ret)
  1832. return ret;
  1833. ret = FAILED;
  1834. ql4_printk(KERN_INFO, ha,
  1835. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1836. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1837. DEBUG2(printk(KERN_INFO
  1838. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1839. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1840. cmd, jiffies, cmd->request->timeout / HZ,
  1841. ha->dpc_flags, cmd->result, cmd->allowed));
  1842. /* FIXME: wait for hba to go online */
  1843. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1844. if (stat != QLA_SUCCESS) {
  1845. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  1846. goto eh_dev_reset_done;
  1847. }
  1848. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1849. cmd->device)) {
  1850. ql4_printk(KERN_INFO, ha,
  1851. "DEVICE RESET FAILED - waiting for "
  1852. "commands.\n");
  1853. goto eh_dev_reset_done;
  1854. }
  1855. /* Send marker. */
  1856. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1857. MM_LUN_RESET) != QLA_SUCCESS)
  1858. goto eh_dev_reset_done;
  1859. ql4_printk(KERN_INFO, ha,
  1860. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1861. ha->host_no, cmd->device->channel, cmd->device->id,
  1862. cmd->device->lun);
  1863. ret = SUCCESS;
  1864. eh_dev_reset_done:
  1865. return ret;
  1866. }
  1867. /**
  1868. * qla4xxx_eh_target_reset - callback for target reset.
  1869. * @cmd: Pointer to Linux's SCSI command structure
  1870. *
  1871. * This routine is called by the Linux OS to reset the target.
  1872. **/
  1873. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  1874. {
  1875. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1876. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1877. int stat, ret;
  1878. if (!ddb_entry)
  1879. return FAILED;
  1880. ret = iscsi_block_scsi_eh(cmd);
  1881. if (ret)
  1882. return ret;
  1883. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1884. "WARM TARGET RESET ISSUED.\n");
  1885. DEBUG2(printk(KERN_INFO
  1886. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  1887. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  1888. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  1889. ha->dpc_flags, cmd->result, cmd->allowed));
  1890. stat = qla4xxx_reset_target(ha, ddb_entry);
  1891. if (stat != QLA_SUCCESS) {
  1892. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1893. "WARM TARGET RESET FAILED.\n");
  1894. return FAILED;
  1895. }
  1896. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1897. NULL)) {
  1898. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1899. "WARM TARGET DEVICE RESET FAILED - "
  1900. "waiting for commands.\n");
  1901. return FAILED;
  1902. }
  1903. /* Send marker. */
  1904. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1905. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  1906. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1907. "WARM TARGET DEVICE RESET FAILED - "
  1908. "marker iocb failed.\n");
  1909. return FAILED;
  1910. }
  1911. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1912. "WARM TARGET RESET SUCCEEDED.\n");
  1913. return SUCCESS;
  1914. }
  1915. /**
  1916. * qla4xxx_eh_host_reset - kernel callback
  1917. * @cmd: Pointer to Linux's SCSI command structure
  1918. *
  1919. * This routine is invoked by the Linux kernel to perform fatal error
  1920. * recovery on the specified adapter.
  1921. **/
  1922. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1923. {
  1924. int return_status = FAILED;
  1925. struct scsi_qla_host *ha;
  1926. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1927. if (ql4xdontresethba) {
  1928. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1929. ha->host_no, __func__));
  1930. return FAILED;
  1931. }
  1932. ql4_printk(KERN_INFO, ha,
  1933. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  1934. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1935. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1936. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1937. "DEAD.\n", ha->host_no, cmd->device->channel,
  1938. __func__));
  1939. return FAILED;
  1940. }
  1941. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1942. if (is_qla8022(ha))
  1943. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1944. else
  1945. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1946. }
  1947. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  1948. return_status = SUCCESS;
  1949. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  1950. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  1951. return return_status;
  1952. }
  1953. /* PCI AER driver recovers from all correctable errors w/o
  1954. * driver intervention. For uncorrectable errors PCI AER
  1955. * driver calls the following device driver's callbacks
  1956. *
  1957. * - Fatal Errors - link_reset
  1958. * - Non-Fatal Errors - driver's pci_error_detected() which
  1959. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  1960. *
  1961. * PCI AER driver calls
  1962. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  1963. * returns RECOVERED or NEED_RESET if fw_hung
  1964. * NEED_RESET - driver's slot_reset()
  1965. * DISCONNECT - device is dead & cannot recover
  1966. * RECOVERED - driver's pci_resume()
  1967. */
  1968. static pci_ers_result_t
  1969. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  1970. {
  1971. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1972. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  1973. ha->host_no, __func__, state);
  1974. if (!is_aer_supported(ha))
  1975. return PCI_ERS_RESULT_NONE;
  1976. switch (state) {
  1977. case pci_channel_io_normal:
  1978. clear_bit(AF_EEH_BUSY, &ha->flags);
  1979. return PCI_ERS_RESULT_CAN_RECOVER;
  1980. case pci_channel_io_frozen:
  1981. set_bit(AF_EEH_BUSY, &ha->flags);
  1982. qla4xxx_mailbox_premature_completion(ha);
  1983. qla4xxx_free_irqs(ha);
  1984. pci_disable_device(pdev);
  1985. /* Return back all IOs */
  1986. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1987. return PCI_ERS_RESULT_NEED_RESET;
  1988. case pci_channel_io_perm_failure:
  1989. set_bit(AF_EEH_BUSY, &ha->flags);
  1990. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  1991. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1992. return PCI_ERS_RESULT_DISCONNECT;
  1993. }
  1994. return PCI_ERS_RESULT_NEED_RESET;
  1995. }
  1996. /**
  1997. * qla4xxx_pci_mmio_enabled() gets called if
  1998. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  1999. * and read/write to the device still works.
  2000. **/
  2001. static pci_ers_result_t
  2002. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  2003. {
  2004. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2005. if (!is_aer_supported(ha))
  2006. return PCI_ERS_RESULT_NONE;
  2007. return PCI_ERS_RESULT_RECOVERED;
  2008. }
  2009. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  2010. {
  2011. uint32_t rval = QLA_ERROR;
  2012. uint32_t ret = 0;
  2013. int fn;
  2014. struct pci_dev *other_pdev = NULL;
  2015. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  2016. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2017. if (test_bit(AF_ONLINE, &ha->flags)) {
  2018. clear_bit(AF_ONLINE, &ha->flags);
  2019. qla4xxx_mark_all_devices_missing(ha);
  2020. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2021. }
  2022. fn = PCI_FUNC(ha->pdev->devfn);
  2023. while (fn > 0) {
  2024. fn--;
  2025. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  2026. "func %x\n", ha->host_no, __func__, fn);
  2027. /* Get the pci device given the domain, bus,
  2028. * slot/function number */
  2029. other_pdev =
  2030. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2031. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2032. fn));
  2033. if (!other_pdev)
  2034. continue;
  2035. if (atomic_read(&other_pdev->enable_cnt)) {
  2036. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  2037. "func in enabled state%x\n", ha->host_no,
  2038. __func__, fn);
  2039. pci_dev_put(other_pdev);
  2040. break;
  2041. }
  2042. pci_dev_put(other_pdev);
  2043. }
  2044. /* The first function on the card, the reset owner will
  2045. * start & initialize the firmware. The other functions
  2046. * on the card will reset the firmware context
  2047. */
  2048. if (!fn) {
  2049. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2050. "0x%x is the owner\n", ha->host_no, __func__,
  2051. ha->pdev->devfn);
  2052. qla4_8xxx_idc_lock(ha);
  2053. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2054. QLA82XX_DEV_COLD);
  2055. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2056. QLA82XX_IDC_VERSION);
  2057. qla4_8xxx_idc_unlock(ha);
  2058. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2059. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2060. qla4_8xxx_idc_lock(ha);
  2061. if (rval != QLA_SUCCESS) {
  2062. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2063. "FAILED\n", ha->host_no, __func__);
  2064. qla4_8xxx_clear_drv_active(ha);
  2065. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2066. QLA82XX_DEV_FAILED);
  2067. } else {
  2068. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2069. "READY\n", ha->host_no, __func__);
  2070. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2071. QLA82XX_DEV_READY);
  2072. /* Clear driver state register */
  2073. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2074. qla4_8xxx_set_drv_active(ha);
  2075. ret = qla4xxx_request_irqs(ha);
  2076. if (ret) {
  2077. ql4_printk(KERN_WARNING, ha, "Failed to "
  2078. "reserve interrupt %d already in use.\n",
  2079. ha->pdev->irq);
  2080. rval = QLA_ERROR;
  2081. } else {
  2082. ha->isp_ops->enable_intrs(ha);
  2083. rval = QLA_SUCCESS;
  2084. }
  2085. }
  2086. qla4_8xxx_idc_unlock(ha);
  2087. } else {
  2088. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2089. "the reset owner\n", ha->host_no, __func__,
  2090. ha->pdev->devfn);
  2091. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2092. QLA82XX_DEV_READY)) {
  2093. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2094. rval = qla4xxx_initialize_adapter(ha,
  2095. PRESERVE_DDB_LIST);
  2096. if (rval == QLA_SUCCESS) {
  2097. ret = qla4xxx_request_irqs(ha);
  2098. if (ret) {
  2099. ql4_printk(KERN_WARNING, ha, "Failed to"
  2100. " reserve interrupt %d already in"
  2101. " use.\n", ha->pdev->irq);
  2102. rval = QLA_ERROR;
  2103. } else {
  2104. ha->isp_ops->enable_intrs(ha);
  2105. rval = QLA_SUCCESS;
  2106. }
  2107. }
  2108. qla4_8xxx_idc_lock(ha);
  2109. qla4_8xxx_set_drv_active(ha);
  2110. qla4_8xxx_idc_unlock(ha);
  2111. }
  2112. }
  2113. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2114. return rval;
  2115. }
  2116. static pci_ers_result_t
  2117. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2118. {
  2119. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2120. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2121. int rc;
  2122. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2123. ha->host_no, __func__);
  2124. if (!is_aer_supported(ha))
  2125. return PCI_ERS_RESULT_NONE;
  2126. /* Restore the saved state of PCIe device -
  2127. * BAR registers, PCI Config space, PCIX, MSI,
  2128. * IOV states
  2129. */
  2130. pci_restore_state(pdev);
  2131. /* pci_restore_state() clears the saved_state flag of the device
  2132. * save restored state which resets saved_state flag
  2133. */
  2134. pci_save_state(pdev);
  2135. /* Initialize device or resume if in suspended state */
  2136. rc = pci_enable_device(pdev);
  2137. if (rc) {
  2138. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  2139. "device after reset\n", ha->host_no, __func__);
  2140. goto exit_slot_reset;
  2141. }
  2142. ha->isp_ops->disable_intrs(ha);
  2143. if (is_qla8022(ha)) {
  2144. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2145. ret = PCI_ERS_RESULT_RECOVERED;
  2146. goto exit_slot_reset;
  2147. } else
  2148. goto exit_slot_reset;
  2149. }
  2150. exit_slot_reset:
  2151. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2152. "device after reset\n", ha->host_no, __func__, ret);
  2153. return ret;
  2154. }
  2155. static void
  2156. qla4xxx_pci_resume(struct pci_dev *pdev)
  2157. {
  2158. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2159. int ret;
  2160. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2161. ha->host_no, __func__);
  2162. ret = qla4xxx_wait_for_hba_online(ha);
  2163. if (ret != QLA_SUCCESS) {
  2164. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2165. "resume I/O from slot/link_reset\n", ha->host_no,
  2166. __func__);
  2167. }
  2168. pci_cleanup_aer_uncorrect_error_status(pdev);
  2169. clear_bit(AF_EEH_BUSY, &ha->flags);
  2170. }
  2171. static struct pci_error_handlers qla4xxx_err_handler = {
  2172. .error_detected = qla4xxx_pci_error_detected,
  2173. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2174. .slot_reset = qla4xxx_pci_slot_reset,
  2175. .resume = qla4xxx_pci_resume,
  2176. };
  2177. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2178. {
  2179. .vendor = PCI_VENDOR_ID_QLOGIC,
  2180. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2181. .subvendor = PCI_ANY_ID,
  2182. .subdevice = PCI_ANY_ID,
  2183. },
  2184. {
  2185. .vendor = PCI_VENDOR_ID_QLOGIC,
  2186. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2187. .subvendor = PCI_ANY_ID,
  2188. .subdevice = PCI_ANY_ID,
  2189. },
  2190. {
  2191. .vendor = PCI_VENDOR_ID_QLOGIC,
  2192. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2193. .subvendor = PCI_ANY_ID,
  2194. .subdevice = PCI_ANY_ID,
  2195. },
  2196. {
  2197. .vendor = PCI_VENDOR_ID_QLOGIC,
  2198. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2199. .subvendor = PCI_ANY_ID,
  2200. .subdevice = PCI_ANY_ID,
  2201. },
  2202. {0, 0},
  2203. };
  2204. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2205. static struct pci_driver qla4xxx_pci_driver = {
  2206. .name = DRIVER_NAME,
  2207. .id_table = qla4xxx_pci_tbl,
  2208. .probe = qla4xxx_probe_adapter,
  2209. .remove = qla4xxx_remove_adapter,
  2210. .err_handler = &qla4xxx_err_handler,
  2211. };
  2212. static int __init qla4xxx_module_init(void)
  2213. {
  2214. int ret;
  2215. /* Allocate cache for SRBs. */
  2216. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2217. SLAB_HWCACHE_ALIGN, NULL);
  2218. if (srb_cachep == NULL) {
  2219. printk(KERN_ERR
  2220. "%s: Unable to allocate SRB cache..."
  2221. "Failing load!\n", DRIVER_NAME);
  2222. ret = -ENOMEM;
  2223. goto no_srp_cache;
  2224. }
  2225. /* Derive version string. */
  2226. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2227. if (ql4xextended_error_logging)
  2228. strcat(qla4xxx_version_str, "-debug");
  2229. qla4xxx_scsi_transport =
  2230. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2231. if (!qla4xxx_scsi_transport){
  2232. ret = -ENODEV;
  2233. goto release_srb_cache;
  2234. }
  2235. ret = pci_register_driver(&qla4xxx_pci_driver);
  2236. if (ret)
  2237. goto unregister_transport;
  2238. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2239. return 0;
  2240. unregister_transport:
  2241. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2242. release_srb_cache:
  2243. kmem_cache_destroy(srb_cachep);
  2244. no_srp_cache:
  2245. return ret;
  2246. }
  2247. static void __exit qla4xxx_module_exit(void)
  2248. {
  2249. pci_unregister_driver(&qla4xxx_pci_driver);
  2250. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2251. kmem_cache_destroy(srb_cachep);
  2252. }
  2253. module_init(qla4xxx_module_init);
  2254. module_exit(qla4xxx_module_exit);
  2255. MODULE_AUTHOR("QLogic Corporation");
  2256. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2257. MODULE_LICENSE("GPL");
  2258. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);