fc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /* fc.c: Generic Fibre Channel and FC4 SCSI driver.
  2. *
  3. * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
  4. * Copyright (C) 1997,1998 Jirka Hanika (geo@ff.cuni.cz)
  5. *
  6. * There are two kinds of Fibre Channel adapters used in Linux. Either
  7. * the adapter is "smart" and does all FC bookkeeping by itself and
  8. * just presents a standard SCSI interface to the operating system
  9. * (that's e.g. the case with Qlogic FC cards), or leaves most of the FC
  10. * bookkeeping to the OS (e.g. soc, socal). Drivers for the former adapters
  11. * will look like normal SCSI drivers (with the exception of max_id will be
  12. * usually 127), the latter on the other side allows SCSI, IP over FC and other
  13. * protocols. This driver tree is for the latter adapters.
  14. *
  15. * This file should support both Point-to-Point and Arbitrated Loop topologies.
  16. *
  17. * Sources:
  18. * Fibre Channel Physical & Signaling Interface (FC-PH), dpANS, 1994
  19. * dpANS Fibre Channel Protocol for SCSI (X3.269-199X), Rev. 012, 1995
  20. * Fibre Channel Arbitrated Loop (FC-AL), Rev. 4.5, 1995
  21. * Fibre Channel Private Loop SCSI Direct Attach (FC-PLDA), Rev. 2.1, 1997
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/types.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/ioport.h>
  31. #include <linux/in.h>
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/init.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/irq.h>
  37. #include <asm/semaphore.h>
  38. #include "fcp_impl.h"
  39. #include <scsi/scsi_host.h>
  40. /* #define FCDEBUG */
  41. #define fc_printk printk ("%s: ", fc->name); printk
  42. #ifdef FCDEBUG
  43. #define FCD(x) fc_printk x;
  44. #define FCND(x) printk ("FC: "); printk x;
  45. #else
  46. #define FCD(x)
  47. #define FCND(x)
  48. #endif
  49. #ifdef __sparc__
  50. #define dma_alloc_consistent(d,s,p) sbus_alloc_consistent(d,s,p)
  51. #define dma_free_consistent(d,s,v,h) sbus_free_consistent(d,s,v,h)
  52. #define dma_map_single(d,v,s,dir) sbus_map_single(d,v,s,dir)
  53. #define dma_unmap_single(d,h,s,dir) sbus_unmap_single(d,h,s,dir)
  54. #define dma_map_sg(d,s,n,dir) sbus_map_sg(d,s,n,dir)
  55. #define dma_unmap_sg(d,s,n,dir) sbus_unmap_sg(d,s,n,dir)
  56. #else
  57. #define dma_alloc_consistent(d,s,p) pci_alloc_consistent(d,s,p)
  58. #define dma_free_consistent(d,s,v,h) pci_free_consistent(d,s,v,h)
  59. #define dma_map_single(d,v,s,dir) pci_map_single(d,v,s,dir)
  60. #define dma_unmap_single(d,h,s,dir) pci_unmap_single(d,h,s,dir)
  61. #define dma_map_sg(d,s,n,dir) pci_map_sg(d,s,n,dir)
  62. #define dma_unmap_sg(d,s,n,dir) pci_unmap_sg(d,s,n,dir)
  63. #endif
  64. #define FCP_CMND(SCpnt) ((fcp_cmnd *)&(SCpnt->SCp))
  65. #define FC_SCMND(SCpnt) ((fc_channel *)(SCpnt->device->host->hostdata[0]))
  66. #define SC_FCMND(fcmnd) ((Scsi_Cmnd *)((long)fcmnd - (long)&(((Scsi_Cmnd *)0)->SCp)))
  67. static int fcp_scsi_queue_it(fc_channel *, Scsi_Cmnd *, fcp_cmnd *, int);
  68. void fcp_queue_empty(fc_channel *);
  69. static void fcp_scsi_insert_queue (fc_channel *fc, fcp_cmnd *fcmd)
  70. {
  71. if (!fc->scsi_que) {
  72. fc->scsi_que = fcmd;
  73. fcmd->next = fcmd;
  74. fcmd->prev = fcmd;
  75. } else {
  76. fc->scsi_que->prev->next = fcmd;
  77. fcmd->prev = fc->scsi_que->prev;
  78. fc->scsi_que->prev = fcmd;
  79. fcmd->next = fc->scsi_que;
  80. }
  81. }
  82. static void fcp_scsi_remove_queue (fc_channel *fc, fcp_cmnd *fcmd)
  83. {
  84. if (fcmd == fcmd->next) {
  85. fc->scsi_que = NULL;
  86. return;
  87. }
  88. if (fcmd == fc->scsi_que)
  89. fc->scsi_que = fcmd->next;
  90. fcmd->prev->next = fcmd->next;
  91. fcmd->next->prev = fcmd->prev;
  92. }
  93. fc_channel *fc_channels = NULL;
  94. #define LSMAGIC 620829043
  95. typedef struct {
  96. /* Must be first */
  97. struct semaphore sem;
  98. int magic;
  99. int count;
  100. logi *logi;
  101. fcp_cmnd *fcmds;
  102. atomic_t todo;
  103. struct timer_list timer;
  104. unsigned char grace[0];
  105. } ls;
  106. #define LSOMAGIC 654907799
  107. typedef struct {
  108. /* Must be first */
  109. struct semaphore sem;
  110. int magic;
  111. int count;
  112. fcp_cmnd *fcmds;
  113. atomic_t todo;
  114. struct timer_list timer;
  115. } lso;
  116. #define LSEMAGIC 84482456
  117. typedef struct {
  118. /* Must be first */
  119. struct semaphore sem;
  120. int magic;
  121. int status;
  122. struct timer_list timer;
  123. } lse;
  124. static void fcp_login_timeout(unsigned long data)
  125. {
  126. ls *l = (ls *)data;
  127. FCND(("Login timeout\n"))
  128. up(&l->sem);
  129. }
  130. static void fcp_login_done(fc_channel *fc, int i, int status)
  131. {
  132. fcp_cmnd *fcmd;
  133. logi *plogi;
  134. fc_hdr *fch;
  135. ls *l = (ls *)fc->ls;
  136. FCD(("Login done %d %d\n", i, status))
  137. if (i < l->count) {
  138. if (fc->state == FC_STATE_FPORT_OK) {
  139. FCD(("Additional FPORT_OK received with status %d\n", status))
  140. return;
  141. }
  142. switch (status) {
  143. case FC_STATUS_OK: /* Oh, we found a fabric */
  144. case FC_STATUS_P_RJT: /* Oh, we haven't found any */
  145. fc->state = FC_STATE_FPORT_OK;
  146. fcmd = l->fcmds + i;
  147. plogi = l->logi + 3 * i;
  148. dma_unmap_single (fc->dev, fcmd->cmd, 3 * sizeof(logi),
  149. DMA_BIDIRECTIONAL);
  150. plogi->code = LS_PLOGI;
  151. memcpy (&plogi->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  152. memcpy (&plogi->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  153. memcpy (&plogi->common, fc->common_svc, sizeof(common_svc_parm));
  154. memcpy (&plogi->class1, fc->class_svcs, 3*sizeof(svc_parm));
  155. fch = &fcmd->fch;
  156. fcmd->token += l->count;
  157. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, fc->did);
  158. FILL_FCHDR_SID(fch, fc->sid);
  159. #ifdef FCDEBUG
  160. {
  161. int i;
  162. unsigned *x = (unsigned *)plogi;
  163. printk ("logi: ");
  164. for (i = 0; i < 21; i++)
  165. printk ("%08x ", x[i]);
  166. printk ("\n");
  167. }
  168. #endif
  169. fcmd->cmd = dma_map_single (fc->dev, plogi, 3 * sizeof(logi),
  170. DMA_BIDIRECTIONAL);
  171. fcmd->rsp = fcmd->cmd + 2 * sizeof(logi);
  172. if (fc->hw_enque (fc, fcmd))
  173. printk ("FC: Cannot enque PLOGI packet on %s\n", fc->name);
  174. break;
  175. case FC_STATUS_ERR_OFFLINE:
  176. fc->state = FC_STATE_MAYBEOFFLINE;
  177. FCD (("FC is offline %d\n", l->grace[i]))
  178. break;
  179. default:
  180. printk ("FLOGI failed for %s with status %d\n", fc->name, status);
  181. /* Do some sort of error recovery here */
  182. break;
  183. }
  184. } else {
  185. i -= l->count;
  186. if (fc->state != FC_STATE_FPORT_OK) {
  187. FCD(("Unexpected N-PORT rsp received"))
  188. return;
  189. }
  190. switch (status) {
  191. case FC_STATUS_OK:
  192. plogi = l->logi + 3 * i;
  193. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  194. DMA_BIDIRECTIONAL);
  195. if (!fc->wwn_dest.lo && !fc->wwn_dest.hi) {
  196. memcpy (&fc->wwn_dest, &plogi[1].node_wwn, sizeof(fc_wwn));
  197. FCD(("Dest WWN %08x%08x\n", *(u32 *)&fc->wwn_dest, fc->wwn_dest.lo))
  198. } else if (fc->wwn_dest.lo != plogi[1].node_wwn.lo ||
  199. fc->wwn_dest.hi != plogi[1].node_wwn.hi) {
  200. printk ("%s: mismatch in wwns. Got %08x%08x, expected %08x%08x\n",
  201. fc->name,
  202. *(u32 *)&plogi[1].node_wwn, plogi[1].node_wwn.lo,
  203. *(u32 *)&fc->wwn_dest, fc->wwn_dest.lo);
  204. }
  205. fc->state = FC_STATE_ONLINE;
  206. printk ("%s: ONLINE\n", fc->name);
  207. if (atomic_dec_and_test (&l->todo))
  208. up(&l->sem);
  209. break;
  210. case FC_STATUS_ERR_OFFLINE:
  211. fc->state = FC_STATE_OFFLINE;
  212. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  213. DMA_BIDIRECTIONAL);
  214. printk ("%s: FC is offline\n", fc->name);
  215. if (atomic_dec_and_test (&l->todo))
  216. up(&l->sem);
  217. break;
  218. default:
  219. printk ("PLOGI failed for %s with status %d\n", fc->name, status);
  220. /* Do some sort of error recovery here */
  221. break;
  222. }
  223. }
  224. }
  225. static void fcp_report_map_done(fc_channel *fc, int i, int status)
  226. {
  227. fcp_cmnd *fcmd;
  228. fc_hdr *fch;
  229. unsigned char j;
  230. ls *l = (ls *)fc->ls;
  231. fc_al_posmap *p;
  232. FCD(("Report map done %d %d\n", i, status))
  233. switch (status) {
  234. case FC_STATUS_OK: /* Ok, let's have a fun on a loop */
  235. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  236. DMA_BIDIRECTIONAL);
  237. p = (fc_al_posmap *)(l->logi + 3 * i);
  238. #ifdef FCDEBUG
  239. {
  240. u32 *u = (u32 *)p;
  241. FCD(("%08x\n", u[0]))
  242. u ++;
  243. FCD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n", u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
  244. }
  245. #endif
  246. if ((p->magic & 0xffff0000) != FC_AL_LILP || !p->len) {
  247. printk ("FC: Bad magic from REPORT_AL_MAP on %s - %08x\n", fc->name, p->magic);
  248. fc->state = FC_STATE_OFFLINE;
  249. } else {
  250. fc->posmap = (fcp_posmap *)kzalloc(sizeof(fcp_posmap)+p->len, GFP_KERNEL);
  251. if (!fc->posmap) {
  252. printk("FC: Not enough memory, offlining channel\n");
  253. fc->state = FC_STATE_OFFLINE;
  254. } else {
  255. int k;
  256. /* FIXME: This is where SOCAL transfers our AL-PA.
  257. Keep it here till we found out what other cards do... */
  258. fc->sid = (p->magic & 0xff);
  259. for (i = 0; i < p->len; i++)
  260. if (p->alpa[i] == fc->sid)
  261. break;
  262. k = p->len;
  263. if (i == p->len)
  264. i = 0;
  265. else {
  266. p->len--;
  267. i++;
  268. }
  269. fc->posmap->len = p->len;
  270. for (j = 0; j < p->len; j++) {
  271. if (i == k) i = 0;
  272. fc->posmap->list[j] = p->alpa[i++];
  273. }
  274. fc->state = FC_STATE_ONLINE;
  275. }
  276. }
  277. printk ("%s: ONLINE\n", fc->name);
  278. if (atomic_dec_and_test (&l->todo))
  279. up(&l->sem);
  280. break;
  281. case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
  282. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  283. fcmd = l->fcmds + i;
  284. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
  285. DMA_BIDIRECTIONAL);
  286. fch = &fcmd->fch;
  287. memset(l->logi + 3 * i, 0, 3 * sizeof(logi));
  288. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  289. FILL_FCHDR_SID(fch, 0);
  290. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  291. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  292. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  293. fch->param = 0;
  294. l->logi [3 * i].code = LS_FLOGI;
  295. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  296. DMA_BIDIRECTIONAL);
  297. fcmd->rsp = fcmd->cmd + sizeof(logi);
  298. fcmd->cmdlen = sizeof(logi);
  299. fcmd->rsplen = sizeof(logi);
  300. fcmd->data = (dma_addr_t)NULL;
  301. fcmd->class = FC_CLASS_SIMPLE;
  302. fcmd->proto = TYPE_EXTENDED_LS;
  303. if (fc->hw_enque (fc, fcmd))
  304. printk ("FC: Cannot enque FLOGI packet on %s\n", fc->name);
  305. break;
  306. case FC_STATUS_ERR_OFFLINE:
  307. fc->state = FC_STATE_MAYBEOFFLINE;
  308. FCD (("FC is offline %d\n", l->grace[i]))
  309. break;
  310. default:
  311. printk ("FLOGI failed for %s with status %d\n", fc->name, status);
  312. /* Do some sort of error recovery here */
  313. break;
  314. }
  315. }
  316. void fcp_register(fc_channel *fc, u8 type, int unregister)
  317. {
  318. int size, i;
  319. int slots = (fc->can_queue * 3) >> 1;
  320. FCND(("Going to %sregister\n", unregister ? "un" : ""))
  321. if (type == TYPE_SCSI_FCP) {
  322. if (!unregister) {
  323. fc->scsi_cmd_pool = (fcp_cmd *)
  324. dma_alloc_consistent (fc->dev,
  325. slots * (sizeof (fcp_cmd) + fc->rsp_size),
  326. &fc->dma_scsi_cmd);
  327. fc->scsi_rsp_pool = (char *)(fc->scsi_cmd_pool + slots);
  328. fc->dma_scsi_rsp = fc->dma_scsi_cmd + slots * sizeof (fcp_cmd);
  329. fc->scsi_bitmap_end = (slots + 63) & ~63;
  330. size = fc->scsi_bitmap_end / 8;
  331. fc->scsi_bitmap = kzalloc (size, GFP_KERNEL);
  332. set_bit (0, fc->scsi_bitmap);
  333. for (i = fc->can_queue; i < fc->scsi_bitmap_end; i++)
  334. set_bit (i, fc->scsi_bitmap);
  335. fc->scsi_free = fc->can_queue;
  336. fc->cmd_slots = (fcp_cmnd **)kzalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
  337. fc->abort_count = 0;
  338. } else {
  339. fc->scsi_name[0] = 0;
  340. kfree (fc->scsi_bitmap);
  341. kfree (fc->cmd_slots);
  342. FCND(("Unregistering\n"));
  343. #if 0
  344. if (fc->rst_pkt) {
  345. if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
  346. kfree(fc->rst_pkt);
  347. else {
  348. /* Can't happen. Some memory would be lost. */
  349. printk("FC: Reset in progress. Now?!");
  350. }
  351. }
  352. #endif
  353. FCND(("Unregistered\n"));
  354. }
  355. } else
  356. printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
  357. }
  358. static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
  359. static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
  360. {
  361. fcp_cmnd *fcmd;
  362. fcp_rsp *rsp;
  363. int host_status;
  364. Scsi_Cmnd *SCpnt;
  365. int sense_len;
  366. int rsp_status;
  367. fcmd = fc->cmd_slots[token];
  368. if (!fcmd) return;
  369. rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
  370. SCpnt = SC_FCMND(fcmd);
  371. if (SCpnt->done != fcp_scsi_done)
  372. return;
  373. rsp_status = rsp->fcp_status;
  374. FCD(("rsp_status %08x status %08x\n", rsp_status, status))
  375. switch (status) {
  376. case FC_STATUS_OK:
  377. host_status=DID_OK;
  378. if (rsp_status & FCP_STATUS_RESID) {
  379. #ifdef FCDEBUG
  380. FCD(("Resid %d\n", rsp->fcp_resid))
  381. {
  382. fcp_cmd *cmd = fc->scsi_cmd_pool + token;
  383. int i;
  384. printk ("Command ");
  385. for (i = 0; i < sizeof(fcp_cmd); i+=4)
  386. printk ("%08x ", *(u32 *)(((char *)cmd)+i));
  387. printk ("\nResponse ");
  388. for (i = 0; i < fc->rsp_size; i+=4)
  389. printk ("%08x ", *(u32 *)(((char *)rsp)+i));
  390. printk ("\n");
  391. }
  392. #endif
  393. }
  394. if (rsp_status & FCP_STATUS_SENSE_LEN) {
  395. sense_len = rsp->fcp_sense_len;
  396. if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
  397. memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
  398. }
  399. if (fcmd->data) {
  400. if (SCpnt->use_sg)
  401. dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->buffer,
  402. SCpnt->use_sg,
  403. SCpnt->sc_data_direction);
  404. else
  405. dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
  406. SCpnt->sc_data_direction);
  407. }
  408. break;
  409. default:
  410. host_status=DID_ERROR; /* FIXME */
  411. FCD(("Wrong FC status %d for token %d\n", status, token))
  412. break;
  413. }
  414. if (status_byte(rsp_status) == QUEUE_FULL) {
  415. printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
  416. }
  417. SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
  418. #ifdef FCDEBUG
  419. if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
  420. host_status, SCpnt->result);
  421. #endif
  422. SCpnt->done = fcmd->done;
  423. fcmd->done=NULL;
  424. clear_bit(token, fc->scsi_bitmap);
  425. fc->scsi_free++;
  426. FCD(("Calling scsi_done with %08x\n", SCpnt->result))
  427. SCpnt->scsi_done(SCpnt);
  428. }
  429. void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
  430. {
  431. int magic;
  432. FCD(("receive_solicited %d %d %d\n", proto, token, status))
  433. switch (proto) {
  434. case TYPE_SCSI_FCP:
  435. fcp_scsi_receive(fc, token, status, fch); break;
  436. case TYPE_EXTENDED_LS:
  437. case PROTO_REPORT_AL_MAP:
  438. magic = 0;
  439. if (fc->ls)
  440. magic = ((ls *)(fc->ls))->magic;
  441. if (magic == LSMAGIC) {
  442. ls *l = (ls *)fc->ls;
  443. int i = (token >= l->count) ? token - l->count : token;
  444. /* Let's be sure */
  445. if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
  446. if (proto == TYPE_EXTENDED_LS)
  447. fcp_login_done(fc, token, status);
  448. else
  449. fcp_report_map_done(fc, token, status);
  450. break;
  451. }
  452. }
  453. FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
  454. if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
  455. fcp_cmnd *fcmd;
  456. fcmd = fc->cmd_slots[token];
  457. if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
  458. lse *l = (lse *)fcmd->ls;
  459. l->status = status;
  460. up (&l->sem);
  461. }
  462. }
  463. break;
  464. case PROTO_OFFLINE:
  465. if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
  466. lso *l = (lso *)fc->ls;
  467. if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
  468. /* Wow, OFFLINE response arrived :) */
  469. FCD(("OFFLINE Response arrived\n"))
  470. fc->state = FC_STATE_OFFLINE;
  471. if (atomic_dec_and_test (&l->todo))
  472. up(&l->sem);
  473. }
  474. }
  475. break;
  476. default:
  477. break;
  478. }
  479. }
  480. void fcp_state_change(fc_channel *fc, int state)
  481. {
  482. FCD(("state_change %d %d\n", state, fc->state))
  483. if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
  484. fc->state = FC_STATE_UNINITED;
  485. else if (state == FC_STATE_ONLINE)
  486. printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
  487. else
  488. printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
  489. }
  490. int fcp_initialize(fc_channel *fcchain, int count)
  491. {
  492. fc_channel *fc;
  493. fcp_cmnd *fcmd;
  494. int i, retry, ret;
  495. ls *l;
  496. FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
  497. FCND(("fc_channels %08lx\n", (long)fc_channels))
  498. FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
  499. l = kzalloc(sizeof (ls) + count, GFP_KERNEL);
  500. if (!l) {
  501. printk ("FC: Cannot allocate memory for initialization\n");
  502. return -ENOMEM;
  503. }
  504. l->magic = LSMAGIC;
  505. l->count = count;
  506. FCND(("FCP Init for %d channels\n", count))
  507. init_MUTEX_LOCKED(&l->sem);
  508. init_timer(&l->timer);
  509. l->timer.function = fcp_login_timeout;
  510. l->timer.data = (unsigned long)l;
  511. atomic_set (&l->todo, count);
  512. l->logi = kzalloc (count * 3 * sizeof(logi), GFP_KERNEL);
  513. l->fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  514. if (!l->logi || !l->fcmds) {
  515. kfree (l->logi);
  516. kfree (l->fcmds);
  517. kfree (l);
  518. printk ("FC: Cannot allocate DMA memory for initialization\n");
  519. return -ENOMEM;
  520. }
  521. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  522. fc->state = FC_STATE_UNINITED;
  523. fc->rst_pkt = NULL; /* kmalloc when first used */
  524. }
  525. /* First try if we are in a AL topology */
  526. FCND(("Initializing REPORT_MAP packets\n"))
  527. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  528. fcmd = l->fcmds + i;
  529. fc->login = fcmd;
  530. fc->ls = (void *)l;
  531. /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
  532. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  533. DMA_BIDIRECTIONAL);
  534. fcmd->proto = PROTO_REPORT_AL_MAP;
  535. fcmd->token = i;
  536. fcmd->fc = fc;
  537. }
  538. for (retry = 0; retry < 8; retry++) {
  539. int nqueued = 0;
  540. FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
  541. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  542. if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
  543. continue;
  544. disable_irq(fc->irq);
  545. if (fc->state == FC_STATE_MAYBEOFFLINE) {
  546. if (!l->grace[i]) {
  547. l->grace[i]++;
  548. FCD(("Grace\n"))
  549. } else {
  550. fc->state = FC_STATE_OFFLINE;
  551. enable_irq(fc->irq);
  552. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  553. if (atomic_dec_and_test (&l->todo))
  554. goto all_done;
  555. }
  556. }
  557. ret = fc->hw_enque (fc, fc->login);
  558. enable_irq(fc->irq);
  559. if (!ret) {
  560. nqueued++;
  561. continue;
  562. }
  563. if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
  564. /* Oh yes, this card handles Point-to-Point only, so let's try that. */
  565. fc_hdr *fch;
  566. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  567. fcmd = l->fcmds + i;
  568. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  569. fch = &fcmd->fch;
  570. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  571. FILL_FCHDR_SID(fch, 0);
  572. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  573. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  574. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  575. fch->param = 0;
  576. l->logi [3 * i].code = LS_FLOGI;
  577. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  578. fcmd->rsp = fcmd->cmd + sizeof(logi);
  579. fcmd->cmdlen = sizeof(logi);
  580. fcmd->rsplen = sizeof(logi);
  581. fcmd->data = (dma_addr_t)NULL;
  582. fcmd->class = FC_CLASS_SIMPLE;
  583. fcmd->proto = TYPE_EXTENDED_LS;
  584. } else
  585. printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
  586. }
  587. if (nqueued) {
  588. l->timer.expires = jiffies + 5 * HZ;
  589. add_timer(&l->timer);
  590. down(&l->sem);
  591. if (!atomic_read(&l->todo)) {
  592. FCND(("All channels answered in time\n"))
  593. break; /* All fc channels have answered us */
  594. }
  595. }
  596. }
  597. all_done:
  598. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  599. fc->ls = NULL;
  600. switch (fc->state) {
  601. case FC_STATE_ONLINE: break;
  602. case FC_STATE_OFFLINE: break;
  603. default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  604. break;
  605. }
  606. }
  607. del_timer(&l->timer);
  608. kfree (l->logi);
  609. kfree (l->fcmds);
  610. kfree (l);
  611. return 0;
  612. }
  613. int fcp_forceoffline(fc_channel *fcchain, int count)
  614. {
  615. fc_channel *fc;
  616. fcp_cmnd *fcmd;
  617. int i, ret;
  618. lso l;
  619. memset (&l, 0, sizeof(lso));
  620. l.count = count;
  621. l.magic = LSOMAGIC;
  622. FCND(("FCP Force Offline for %d channels\n", count))
  623. init_MUTEX_LOCKED(&l.sem);
  624. init_timer(&l.timer);
  625. l.timer.function = fcp_login_timeout;
  626. l.timer.data = (unsigned long)&l;
  627. atomic_set (&l.todo, count);
  628. l.fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  629. if (!l.fcmds) {
  630. printk ("FC: Cannot allocate memory for forcing offline\n");
  631. return -ENOMEM;
  632. }
  633. FCND(("Initializing OFFLINE packets\n"))
  634. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  635. fc->state = FC_STATE_UNINITED;
  636. fcmd = l.fcmds + i;
  637. fc->login = fcmd;
  638. fc->ls = (void *)&l;
  639. fcmd->did = fc->did;
  640. fcmd->class = FC_CLASS_OFFLINE;
  641. fcmd->proto = PROTO_OFFLINE;
  642. fcmd->token = i;
  643. fcmd->fc = fc;
  644. disable_irq(fc->irq);
  645. ret = fc->hw_enque (fc, fc->login);
  646. enable_irq(fc->irq);
  647. if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
  648. }
  649. l.timer.expires = jiffies + 5 * HZ;
  650. add_timer(&l.timer);
  651. down(&l.sem);
  652. del_timer(&l.timer);
  653. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
  654. fc->ls = NULL;
  655. kfree (l.fcmds);
  656. return 0;
  657. }
  658. int fcp_init(fc_channel *fcchain)
  659. {
  660. fc_channel *fc;
  661. int count=0;
  662. int ret;
  663. for (fc = fcchain; fc; fc = fc->next) {
  664. fc->fcp_register = fcp_register;
  665. count++;
  666. }
  667. ret = fcp_initialize (fcchain, count);
  668. if (ret)
  669. return ret;
  670. if (!fc_channels)
  671. fc_channels = fcchain;
  672. else {
  673. for (fc = fc_channels; fc->next; fc = fc->next);
  674. fc->next = fcchain;
  675. }
  676. return ret;
  677. }
  678. void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
  679. {
  680. fc_channel *fc;
  681. fc_channel *fcx;
  682. for (fc = fcchain; --count && fc->next; fc = fc->next);
  683. if (count) {
  684. printk("FC: nothing to release\n");
  685. return;
  686. }
  687. if (fc_channels == fcchain)
  688. fc_channels = fc->next;
  689. else {
  690. for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
  691. fcx->next = fc->next;
  692. }
  693. fc->next = NULL;
  694. /*
  695. * We've just grabbed fcchain out of the fc_channel list
  696. * and zero-terminated it, while destroying the count.
  697. *
  698. * Freeing the fc's is the low level driver's responsibility.
  699. */
  700. }
  701. static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
  702. {
  703. if (FCP_CMND(SCpnt)->done)
  704. FCP_CMND(SCpnt)->done(SCpnt);
  705. }
  706. static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
  707. {
  708. long i;
  709. fcp_cmd *cmd;
  710. u32 fcp_cntl;
  711. if (prepare) {
  712. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  713. set_bit (i, fc->scsi_bitmap);
  714. fcmd->token = i;
  715. cmd = fc->scsi_cmd_pool + i;
  716. if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
  717. /* Invalid channel/id/lun and couldn't map it into fcp_addr */
  718. clear_bit (i, fc->scsi_bitmap);
  719. SCpnt->result = (DID_BAD_TARGET << 16);
  720. SCpnt->scsi_done(SCpnt);
  721. return 0;
  722. }
  723. fc->scsi_free--;
  724. fc->cmd_slots[fcmd->token] = fcmd;
  725. if (SCpnt->device->tagged_supported) {
  726. if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
  727. fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
  728. fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
  729. } else
  730. fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
  731. } else
  732. fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
  733. if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
  734. cmd->fcp_cntl = fcp_cntl;
  735. fcmd->data = (dma_addr_t)NULL;
  736. } else {
  737. switch (SCpnt->cmnd[0]) {
  738. case WRITE_6:
  739. case WRITE_10:
  740. case WRITE_12:
  741. cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
  742. default:
  743. cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
  744. }
  745. if (!SCpnt->use_sg) {
  746. cmd->fcp_data_len = SCpnt->request_bufflen;
  747. fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
  748. SCpnt->request_bufflen,
  749. SCpnt->sc_data_direction);
  750. } else {
  751. struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
  752. int nents;
  753. FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
  754. nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
  755. SCpnt->sc_data_direction);
  756. if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
  757. fcmd->data = sg_dma_address(sg);
  758. cmd->fcp_data_len = sg_dma_len(sg);
  759. }
  760. }
  761. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  762. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  763. FCD(("XXX: %04x.%04x.%04x.%04x - %08x%08x%08x\n", cmd->fcp_addr[0], cmd->fcp_addr[1], cmd->fcp_addr[2], cmd->fcp_addr[3], *(u32 *)SCpnt->cmnd, *(u32 *)(SCpnt->cmnd+4), *(u32 *)(SCpnt->cmnd+8)))
  764. }
  765. FCD(("Trying to enque %p\n", fcmd))
  766. if (!fc->scsi_que) {
  767. if (!fc->hw_enque (fc, fcmd)) {
  768. FCD(("hw_enque succeeded for %p\n", fcmd))
  769. return 0;
  770. }
  771. }
  772. FCD(("Putting into que1 %p\n", fcmd))
  773. fcp_scsi_insert_queue (fc, fcmd);
  774. return 0;
  775. }
  776. int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
  777. {
  778. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  779. fc_channel *fc = FC_SCMND(SCpnt);
  780. FCD(("Entering SCSI queuecommand %p\n", fcmd))
  781. if (SCpnt->done != fcp_scsi_done) {
  782. fcmd->done = SCpnt->done;
  783. SCpnt->done = fcp_scsi_done;
  784. SCpnt->scsi_done = done;
  785. fcmd->proto = TYPE_SCSI_FCP;
  786. if (!fc->scsi_free) {
  787. FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
  788. #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
  789. printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
  790. #endif
  791. return 1;
  792. }
  793. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
  794. }
  795. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
  796. }
  797. void fcp_queue_empty(fc_channel *fc)
  798. {
  799. fcp_cmnd *fcmd;
  800. FCD(("Queue empty\n"))
  801. while ((fcmd = fc->scsi_que)) {
  802. /* The hw told us we can try again queue some packet */
  803. if (fc->hw_enque (fc, fcmd))
  804. break;
  805. fcp_scsi_remove_queue (fc, fcmd);
  806. }
  807. }
  808. int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
  809. {
  810. /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
  811. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  812. fc_channel *fc = FC_SCMND(SCpnt);
  813. /*
  814. * We react to abort requests by simply forgetting
  815. * about the command and pretending everything's sweet.
  816. * This may or may not be silly. We can't, however,
  817. * immediately reuse the command's cmd_slots slot,
  818. * as its result may arrive later and we cannot
  819. * check whether it is the aborted one, can't we?
  820. *
  821. * Therefore, after the first few aborts are done,
  822. * we tell the scsi error handler to do something clever.
  823. * It will eventually call host reset, refreshing
  824. * cmd_slots for us.
  825. *
  826. * There is a theoretical chance that we sometimes allow
  827. * more than can_queue packets to the jungle this way,
  828. * but the worst outcome possible is a series of
  829. * more aborts and eventually the dev_reset catharsis.
  830. */
  831. if (++fc->abort_count < (fc->can_queue >> 1)) {
  832. SCpnt->result = DID_ABORT;
  833. fcmd->done(SCpnt);
  834. printk("FC: soft abort\n");
  835. return SUCCESS;
  836. } else {
  837. printk("FC: hard abort refused\n");
  838. return FAILED;
  839. }
  840. }
  841. #if 0
  842. void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
  843. {
  844. fc_channel *fc = FC_SCMND(SCpnt);
  845. fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
  846. up(fc->rst_pkt->device->host->eh_action);
  847. }
  848. #endif
  849. #define FCP_RESET_TIMEOUT (2*HZ)
  850. int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
  851. {
  852. #if 0 /* broken junk, but if davem wants to compile this driver, let him.. */
  853. unsigned long flags;
  854. fcp_cmd *cmd;
  855. fcp_cmnd *fcmd;
  856. fc_channel *fc = FC_SCMND(SCpnt);
  857. DECLARE_MUTEX_LOCKED(sem);
  858. if (!fc->rst_pkt) {
  859. fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
  860. if (!fc->rst_pkt) return FAILED;
  861. fcmd = FCP_CMND(fc->rst_pkt);
  862. fcmd->token = 0;
  863. cmd = fc->scsi_cmd_pool + 0;
  864. FCD(("Preparing rst packet\n"))
  865. fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
  866. fc->rst_pkt->device = SCpnt->device;
  867. fc->rst_pkt->cmd_len = 0;
  868. fc->cmd_slots[0] = fcmd;
  869. cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
  870. fcmd->data = (dma_addr_t)NULL;
  871. fcmd->proto = TYPE_SCSI_FCP;
  872. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  873. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  874. FCD(("XXX: %04x.%04x.%04x.%04x - %08x%08x%08x\n", cmd->fcp_addr[0], cmd->fcp_addr[1], cmd->fcp_addr[2], cmd->fcp_addr[3], *(u32 *)SCpnt->cmnd, *(u32 *)(SCpnt->cmnd+4), *(u32 *)(SCpnt->cmnd+8)))
  875. } else {
  876. fcmd = FCP_CMND(fc->rst_pkt);
  877. if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
  878. return FAILED; /* or SUCCESS. Only these */
  879. }
  880. fc->rst_pkt->done = NULL;
  881. fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
  882. init_timer(&fc->rst_pkt->eh_timeout);
  883. fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
  884. fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
  885. fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
  886. add_timer(&fc->rst_pkt->eh_timeout);
  887. /*
  888. * Set up the semaphore so we wait for the command to complete.
  889. */
  890. fc->rst_pkt->device->host->eh_action = &sem;
  891. fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
  892. fc->rst_pkt->done = fcp_scsi_reset_done;
  893. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  894. fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
  895. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  896. down(&sem);
  897. fc->rst_pkt->device->host->eh_action = NULL;
  898. del_timer(&fc->rst_pkt->eh_timeout);
  899. /*
  900. * See if timeout. If so, tell the host to forget about it.
  901. * In other words, we don't want a callback any more.
  902. */
  903. if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
  904. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  905. return FAILED;
  906. }
  907. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  908. #endif
  909. return SUCCESS;
  910. }
  911. static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  912. {
  913. fc_channel *fc = FC_SCMND(SCpnt);
  914. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  915. int i;
  916. printk ("FC: host reset\n");
  917. for (i=0; i < fc->can_queue; i++) {
  918. if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
  919. SCpnt->result = DID_RESET;
  920. fcmd->done(SCpnt);
  921. fc->cmd_slots[i] = NULL;
  922. }
  923. }
  924. fc->reset(fc);
  925. fc->abort_count = 0;
  926. if (fcp_initialize(fc, 1)) return SUCCESS;
  927. else return FAILED;
  928. }
  929. int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  930. {
  931. unsigned long flags;
  932. int rc;
  933. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  934. rc = __fcp_scsi_host_reset(SCpnt);
  935. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  936. return rc;
  937. }
  938. static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
  939. {
  940. long i;
  941. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  942. set_bit (i, fc->scsi_bitmap);
  943. fcmd->token = i;
  944. fc->scsi_free--;
  945. fc->cmd_slots[fcmd->token] = fcmd;
  946. return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  947. }
  948. static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
  949. {
  950. fcp_cmnd _fcmd, *fcmd;
  951. fc_hdr *fch;
  952. lse l;
  953. int i;
  954. fcmd = &_fcmd;
  955. memset(fcmd, 0, sizeof(fcp_cmnd));
  956. FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
  957. fch = &fcmd->fch;
  958. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
  959. FILL_FCHDR_SID(fch, fc->sid);
  960. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  961. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  962. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  963. fch->param = 0;
  964. fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
  965. fcmd->rsp = fcmd->cmd + len;
  966. fcmd->cmdlen = len;
  967. fcmd->rsplen = len;
  968. fcmd->data = (dma_addr_t)NULL;
  969. fcmd->fc = fc;
  970. fcmd->class = FC_CLASS_SIMPLE;
  971. fcmd->proto = TYPE_EXTENDED_LS;
  972. memset (&l, 0, sizeof(lse));
  973. l.magic = LSEMAGIC;
  974. init_MUTEX_LOCKED(&l.sem);
  975. l.timer.function = fcp_login_timeout;
  976. l.timer.data = (unsigned long)&l;
  977. l.status = FC_STATUS_TIMED_OUT;
  978. fcmd->ls = (void *)&l;
  979. disable_irq(fc->irq);
  980. fcp_els_queue_it(fc, fcmd);
  981. enable_irq(fc->irq);
  982. for (i = 0;;) {
  983. l.timer.expires = jiffies + 5 * HZ;
  984. add_timer(&l.timer);
  985. down(&l.sem);
  986. del_timer(&l.timer);
  987. if (l.status != FC_STATUS_TIMED_OUT) break;
  988. if (++i == 3) break;
  989. disable_irq(fc->irq);
  990. fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  991. enable_irq(fc->irq);
  992. }
  993. clear_bit(fcmd->token, fc->scsi_bitmap);
  994. fc->scsi_free++;
  995. dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
  996. return l.status;
  997. }
  998. int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
  999. {
  1000. logi *l;
  1001. int status;
  1002. l = (logi *)kzalloc(2 * sizeof(logi), GFP_KERNEL);
  1003. if (!l) return -ENOMEM;
  1004. l->code = LS_PLOGI;
  1005. memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  1006. memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  1007. memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
  1008. memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
  1009. status = fc_do_els(fc, alpa, l, sizeof(logi));
  1010. if (status == FC_STATUS_OK) {
  1011. if (l[1].code == LS_ACC) {
  1012. #ifdef FCDEBUG
  1013. u32 *u = (u32 *)&l[1].nport_wwn;
  1014. FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
  1015. u[0], u[1], u[2], u[3]))
  1016. #endif
  1017. memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
  1018. memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
  1019. } else
  1020. status = FC_STATUS_BAD_RSP;
  1021. }
  1022. kfree(l);
  1023. return status;
  1024. }
  1025. typedef struct {
  1026. unsigned int code;
  1027. unsigned params[4];
  1028. } prli;
  1029. int fc_do_prli(fc_channel *fc, unsigned char alpa)
  1030. {
  1031. prli *p;
  1032. int status;
  1033. p = (prli *)kzalloc(2 * sizeof(prli), GFP_KERNEL);
  1034. if (!p) return -ENOMEM;
  1035. p->code = LS_PRLI;
  1036. p->params[0] = 0x08002000;
  1037. p->params[3] = 0x00000022;
  1038. status = fc_do_els(fc, alpa, p, sizeof(prli));
  1039. if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
  1040. status = FC_STATUS_BAD_RSP;
  1041. kfree(p);
  1042. return status;
  1043. }
  1044. MODULE_LICENSE("GPL");