fc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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 *)kmalloc(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. memset(fc->posmap, 0, sizeof(fcp_posmap)+p->len);
  257. /* FIXME: This is where SOCAL transfers our AL-PA.
  258. Keep it here till we found out what other cards do... */
  259. fc->sid = (p->magic & 0xff);
  260. for (i = 0; i < p->len; i++)
  261. if (p->alpa[i] == fc->sid)
  262. break;
  263. k = p->len;
  264. if (i == p->len)
  265. i = 0;
  266. else {
  267. p->len--;
  268. i++;
  269. }
  270. fc->posmap->len = p->len;
  271. for (j = 0; j < p->len; j++) {
  272. if (i == k) i = 0;
  273. fc->posmap->list[j] = p->alpa[i++];
  274. }
  275. fc->state = FC_STATE_ONLINE;
  276. }
  277. }
  278. printk ("%s: ONLINE\n", fc->name);
  279. if (atomic_dec_and_test (&l->todo))
  280. up(&l->sem);
  281. break;
  282. case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
  283. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  284. fcmd = l->fcmds + i;
  285. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
  286. DMA_BIDIRECTIONAL);
  287. fch = &fcmd->fch;
  288. memset(l->logi + 3 * i, 0, 3 * sizeof(logi));
  289. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  290. FILL_FCHDR_SID(fch, 0);
  291. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  292. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  293. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  294. fch->param = 0;
  295. l->logi [3 * i].code = LS_FLOGI;
  296. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  297. DMA_BIDIRECTIONAL);
  298. fcmd->rsp = fcmd->cmd + sizeof(logi);
  299. fcmd->cmdlen = sizeof(logi);
  300. fcmd->rsplen = sizeof(logi);
  301. fcmd->data = (dma_addr_t)NULL;
  302. fcmd->class = FC_CLASS_SIMPLE;
  303. fcmd->proto = TYPE_EXTENDED_LS;
  304. if (fc->hw_enque (fc, fcmd))
  305. printk ("FC: Cannot enque FLOGI packet on %s\n", fc->name);
  306. break;
  307. case FC_STATUS_ERR_OFFLINE:
  308. fc->state = FC_STATE_MAYBEOFFLINE;
  309. FCD (("FC is offline %d\n", l->grace[i]))
  310. break;
  311. default:
  312. printk ("FLOGI failed for %s with status %d\n", fc->name, status);
  313. /* Do some sort of error recovery here */
  314. break;
  315. }
  316. }
  317. void fcp_register(fc_channel *fc, u8 type, int unregister)
  318. {
  319. int size, i;
  320. int slots = (fc->can_queue * 3) >> 1;
  321. FCND(("Going to %sregister\n", unregister ? "un" : ""))
  322. if (type == TYPE_SCSI_FCP) {
  323. if (!unregister) {
  324. fc->scsi_cmd_pool = (fcp_cmd *)
  325. dma_alloc_consistent (fc->dev,
  326. slots * (sizeof (fcp_cmd) + fc->rsp_size),
  327. &fc->dma_scsi_cmd);
  328. fc->scsi_rsp_pool = (char *)(fc->scsi_cmd_pool + slots);
  329. fc->dma_scsi_rsp = fc->dma_scsi_cmd + slots * sizeof (fcp_cmd);
  330. fc->scsi_bitmap_end = (slots + 63) & ~63;
  331. size = fc->scsi_bitmap_end / 8;
  332. fc->scsi_bitmap = kmalloc (size, GFP_KERNEL);
  333. memset (fc->scsi_bitmap, 0, size);
  334. set_bit (0, fc->scsi_bitmap);
  335. for (i = fc->can_queue; i < fc->scsi_bitmap_end; i++)
  336. set_bit (i, fc->scsi_bitmap);
  337. fc->scsi_free = fc->can_queue;
  338. fc->cmd_slots = (fcp_cmnd **)kmalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
  339. memset(fc->cmd_slots, 0, slots * sizeof(fcp_cmnd*));
  340. fc->abort_count = 0;
  341. } else {
  342. fc->scsi_name[0] = 0;
  343. kfree (fc->scsi_bitmap);
  344. kfree (fc->cmd_slots);
  345. FCND(("Unregistering\n"));
  346. if (fc->rst_pkt) {
  347. if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
  348. kfree(fc->rst_pkt);
  349. else {
  350. /* Can't happen. Some memory would be lost. */
  351. printk("FC: Reset in progress. Now?!");
  352. }
  353. }
  354. FCND(("Unregistered\n"));
  355. }
  356. } else
  357. printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
  358. }
  359. static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
  360. static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
  361. {
  362. fcp_cmnd *fcmd;
  363. fcp_rsp *rsp;
  364. int host_status;
  365. Scsi_Cmnd *SCpnt;
  366. int sense_len;
  367. int rsp_status;
  368. fcmd = fc->cmd_slots[token];
  369. if (!fcmd) return;
  370. rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
  371. SCpnt = SC_FCMND(fcmd);
  372. if (SCpnt->done != fcp_scsi_done)
  373. return;
  374. rsp_status = rsp->fcp_status;
  375. FCD(("rsp_status %08x status %08x\n", rsp_status, status))
  376. switch (status) {
  377. case FC_STATUS_OK:
  378. host_status=DID_OK;
  379. if (rsp_status & FCP_STATUS_RESID) {
  380. #ifdef FCDEBUG
  381. FCD(("Resid %d\n", rsp->fcp_resid))
  382. {
  383. fcp_cmd *cmd = fc->scsi_cmd_pool + token;
  384. int i;
  385. printk ("Command ");
  386. for (i = 0; i < sizeof(fcp_cmd); i+=4)
  387. printk ("%08x ", *(u32 *)(((char *)cmd)+i));
  388. printk ("\nResponse ");
  389. for (i = 0; i < fc->rsp_size; i+=4)
  390. printk ("%08x ", *(u32 *)(((char *)rsp)+i));
  391. printk ("\n");
  392. }
  393. #endif
  394. }
  395. if (rsp_status & FCP_STATUS_SENSE_LEN) {
  396. sense_len = rsp->fcp_sense_len;
  397. if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
  398. memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
  399. }
  400. if (fcmd->data) {
  401. if (SCpnt->use_sg)
  402. dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->buffer,
  403. SCpnt->use_sg,
  404. SCpnt->sc_data_direction);
  405. else
  406. dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
  407. SCpnt->sc_data_direction);
  408. }
  409. break;
  410. default:
  411. host_status=DID_ERROR; /* FIXME */
  412. FCD(("Wrong FC status %d for token %d\n", status, token))
  413. break;
  414. }
  415. if (status_byte(rsp_status) == QUEUE_FULL) {
  416. printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
  417. }
  418. SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
  419. #ifdef FCDEBUG
  420. if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
  421. host_status, SCpnt->result);
  422. #endif
  423. SCpnt->done = fcmd->done;
  424. fcmd->done=NULL;
  425. clear_bit(token, fc->scsi_bitmap);
  426. fc->scsi_free++;
  427. FCD(("Calling scsi_done with %08x\n", SCpnt->result))
  428. SCpnt->scsi_done(SCpnt);
  429. }
  430. void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
  431. {
  432. int magic;
  433. FCD(("receive_solicited %d %d %d\n", proto, token, status))
  434. switch (proto) {
  435. case TYPE_SCSI_FCP:
  436. fcp_scsi_receive(fc, token, status, fch); break;
  437. case TYPE_EXTENDED_LS:
  438. case PROTO_REPORT_AL_MAP:
  439. magic = 0;
  440. if (fc->ls)
  441. magic = ((ls *)(fc->ls))->magic;
  442. if (magic == LSMAGIC) {
  443. ls *l = (ls *)fc->ls;
  444. int i = (token >= l->count) ? token - l->count : token;
  445. /* Let's be sure */
  446. if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
  447. if (proto == TYPE_EXTENDED_LS)
  448. fcp_login_done(fc, token, status);
  449. else
  450. fcp_report_map_done(fc, token, status);
  451. break;
  452. }
  453. }
  454. FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
  455. if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
  456. fcp_cmnd *fcmd;
  457. fcmd = fc->cmd_slots[token];
  458. if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
  459. lse *l = (lse *)fcmd->ls;
  460. l->status = status;
  461. up (&l->sem);
  462. }
  463. }
  464. break;
  465. case PROTO_OFFLINE:
  466. if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
  467. lso *l = (lso *)fc->ls;
  468. if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
  469. /* Wow, OFFLINE response arrived :) */
  470. FCD(("OFFLINE Response arrived\n"))
  471. fc->state = FC_STATE_OFFLINE;
  472. if (atomic_dec_and_test (&l->todo))
  473. up(&l->sem);
  474. }
  475. }
  476. break;
  477. default:
  478. break;
  479. }
  480. }
  481. void fcp_state_change(fc_channel *fc, int state)
  482. {
  483. FCD(("state_change %d %d\n", state, fc->state))
  484. if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
  485. fc->state = FC_STATE_UNINITED;
  486. else if (state == FC_STATE_ONLINE)
  487. printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
  488. else
  489. printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
  490. }
  491. int fcp_initialize(fc_channel *fcchain, int count)
  492. {
  493. fc_channel *fc;
  494. fcp_cmnd *fcmd;
  495. int i, retry, ret;
  496. ls *l;
  497. FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
  498. FCND(("fc_channels %08lx\n", (long)fc_channels))
  499. FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
  500. l = kmalloc(sizeof (ls) + count, GFP_KERNEL);
  501. if (!l) {
  502. printk ("FC: Cannot allocate memory for initialization\n");
  503. return -ENOMEM;
  504. }
  505. memset (l, 0, sizeof(ls) + count);
  506. l->magic = LSMAGIC;
  507. l->count = count;
  508. FCND(("FCP Init for %d channels\n", count))
  509. init_MUTEX_LOCKED(&l->sem);
  510. init_timer(&l->timer);
  511. l->timer.function = fcp_login_timeout;
  512. l->timer.data = (unsigned long)l;
  513. atomic_set (&l->todo, count);
  514. l->logi = kmalloc (count * 3 * sizeof(logi), GFP_KERNEL);
  515. l->fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  516. if (!l->logi || !l->fcmds) {
  517. if (l->logi) kfree (l->logi);
  518. if (l->fcmds) kfree (l->fcmds);
  519. kfree (l);
  520. printk ("FC: Cannot allocate DMA memory for initialization\n");
  521. return -ENOMEM;
  522. }
  523. memset (l->logi, 0, count * 3 * sizeof(logi));
  524. memset (l->fcmds, 0, count * sizeof(fcp_cmnd));
  525. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  526. fc->state = FC_STATE_UNINITED;
  527. fc->rst_pkt = NULL; /* kmalloc when first used */
  528. }
  529. /* First try if we are in a AL topology */
  530. FCND(("Initializing REPORT_MAP packets\n"))
  531. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  532. fcmd = l->fcmds + i;
  533. fc->login = fcmd;
  534. fc->ls = (void *)l;
  535. /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
  536. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  537. DMA_BIDIRECTIONAL);
  538. fcmd->proto = PROTO_REPORT_AL_MAP;
  539. fcmd->token = i;
  540. fcmd->fc = fc;
  541. }
  542. for (retry = 0; retry < 8; retry++) {
  543. int nqueued = 0;
  544. FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
  545. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  546. if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
  547. continue;
  548. disable_irq(fc->irq);
  549. if (fc->state == FC_STATE_MAYBEOFFLINE) {
  550. if (!l->grace[i]) {
  551. l->grace[i]++;
  552. FCD(("Grace\n"))
  553. } else {
  554. fc->state = FC_STATE_OFFLINE;
  555. enable_irq(fc->irq);
  556. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  557. if (atomic_dec_and_test (&l->todo))
  558. goto all_done;
  559. }
  560. }
  561. ret = fc->hw_enque (fc, fc->login);
  562. enable_irq(fc->irq);
  563. if (!ret) {
  564. nqueued++;
  565. continue;
  566. }
  567. if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
  568. /* Oh yes, this card handles Point-to-Point only, so let's try that. */
  569. fc_hdr *fch;
  570. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  571. fcmd = l->fcmds + i;
  572. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  573. fch = &fcmd->fch;
  574. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  575. FILL_FCHDR_SID(fch, 0);
  576. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  577. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  578. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  579. fch->param = 0;
  580. l->logi [3 * i].code = LS_FLOGI;
  581. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  582. fcmd->rsp = fcmd->cmd + sizeof(logi);
  583. fcmd->cmdlen = sizeof(logi);
  584. fcmd->rsplen = sizeof(logi);
  585. fcmd->data = (dma_addr_t)NULL;
  586. fcmd->class = FC_CLASS_SIMPLE;
  587. fcmd->proto = TYPE_EXTENDED_LS;
  588. } else
  589. printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
  590. }
  591. if (nqueued) {
  592. l->timer.expires = jiffies + 5 * HZ;
  593. add_timer(&l->timer);
  594. down(&l->sem);
  595. if (!atomic_read(&l->todo)) {
  596. FCND(("All channels answered in time\n"))
  597. break; /* All fc channels have answered us */
  598. }
  599. }
  600. }
  601. all_done:
  602. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  603. fc->ls = NULL;
  604. switch (fc->state) {
  605. case FC_STATE_ONLINE: break;
  606. case FC_STATE_OFFLINE: break;
  607. default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  608. break;
  609. }
  610. }
  611. del_timer(&l->timer);
  612. kfree (l->logi);
  613. kfree (l->fcmds);
  614. kfree (l);
  615. return 0;
  616. }
  617. int fcp_forceoffline(fc_channel *fcchain, int count)
  618. {
  619. fc_channel *fc;
  620. fcp_cmnd *fcmd;
  621. int i, ret;
  622. lso l;
  623. memset (&l, 0, sizeof(lso));
  624. l.count = count;
  625. l.magic = LSOMAGIC;
  626. FCND(("FCP Force Offline for %d channels\n", count))
  627. init_MUTEX_LOCKED(&l.sem);
  628. init_timer(&l.timer);
  629. l.timer.function = fcp_login_timeout;
  630. l.timer.data = (unsigned long)&l;
  631. atomic_set (&l.todo, count);
  632. l.fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  633. if (!l.fcmds) {
  634. kfree (l.fcmds);
  635. printk ("FC: Cannot allocate memory for forcing offline\n");
  636. return -ENOMEM;
  637. }
  638. memset (l.fcmds, 0, count * sizeof(fcp_cmnd));
  639. FCND(("Initializing OFFLINE packets\n"))
  640. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  641. fc->state = FC_STATE_UNINITED;
  642. fcmd = l.fcmds + i;
  643. fc->login = fcmd;
  644. fc->ls = (void *)&l;
  645. fcmd->did = fc->did;
  646. fcmd->class = FC_CLASS_OFFLINE;
  647. fcmd->proto = PROTO_OFFLINE;
  648. fcmd->token = i;
  649. fcmd->fc = fc;
  650. disable_irq(fc->irq);
  651. ret = fc->hw_enque (fc, fc->login);
  652. enable_irq(fc->irq);
  653. if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
  654. }
  655. l.timer.expires = jiffies + 5 * HZ;
  656. add_timer(&l.timer);
  657. down(&l.sem);
  658. del_timer(&l.timer);
  659. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
  660. fc->ls = NULL;
  661. kfree (l.fcmds);
  662. return 0;
  663. }
  664. int fcp_init(fc_channel *fcchain)
  665. {
  666. fc_channel *fc;
  667. int count=0;
  668. int ret;
  669. for (fc = fcchain; fc; fc = fc->next) {
  670. fc->fcp_register = fcp_register;
  671. count++;
  672. }
  673. ret = fcp_initialize (fcchain, count);
  674. if (ret)
  675. return ret;
  676. if (!fc_channels)
  677. fc_channels = fcchain;
  678. else {
  679. for (fc = fc_channels; fc->next; fc = fc->next);
  680. fc->next = fcchain;
  681. }
  682. return ret;
  683. }
  684. void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
  685. {
  686. fc_channel *fc;
  687. fc_channel *fcx;
  688. for (fc = fcchain; --count && fc->next; fc = fc->next);
  689. if (count) {
  690. printk("FC: nothing to release\n");
  691. return;
  692. }
  693. if (fc_channels == fcchain)
  694. fc_channels = fc->next;
  695. else {
  696. for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
  697. fcx->next = fc->next;
  698. }
  699. fc->next = NULL;
  700. /*
  701. * We've just grabbed fcchain out of the fc_channel list
  702. * and zero-terminated it, while destroying the count.
  703. *
  704. * Freeing the fc's is the low level driver's responsibility.
  705. */
  706. }
  707. static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
  708. {
  709. unsigned long flags;
  710. if (FCP_CMND(SCpnt)->done)
  711. FCP_CMND(SCpnt)->done(SCpnt);
  712. }
  713. static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
  714. {
  715. long i;
  716. fcp_cmd *cmd;
  717. u32 fcp_cntl;
  718. if (prepare) {
  719. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  720. set_bit (i, fc->scsi_bitmap);
  721. fcmd->token = i;
  722. cmd = fc->scsi_cmd_pool + i;
  723. if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
  724. /* Invalid channel/id/lun and couldn't map it into fcp_addr */
  725. clear_bit (i, fc->scsi_bitmap);
  726. SCpnt->result = (DID_BAD_TARGET << 16);
  727. SCpnt->scsi_done(SCpnt);
  728. return 0;
  729. }
  730. fc->scsi_free--;
  731. fc->cmd_slots[fcmd->token] = fcmd;
  732. if (SCpnt->device->tagged_supported) {
  733. if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
  734. fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
  735. fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
  736. } else
  737. fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
  738. } else
  739. fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
  740. if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
  741. cmd->fcp_cntl = fcp_cntl;
  742. fcmd->data = (dma_addr_t)NULL;
  743. } else {
  744. switch (SCpnt->cmnd[0]) {
  745. case WRITE_6:
  746. case WRITE_10:
  747. case WRITE_12:
  748. cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
  749. default:
  750. cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
  751. }
  752. if (!SCpnt->use_sg) {
  753. cmd->fcp_data_len = SCpnt->request_bufflen;
  754. fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
  755. SCpnt->request_bufflen,
  756. SCpnt->sc_data_direction);
  757. } else {
  758. struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
  759. int nents;
  760. FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
  761. nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
  762. SCpnt->sc_data_direction);
  763. if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
  764. fcmd->data = sg_dma_address(sg);
  765. cmd->fcp_data_len = sg_dma_len(sg);
  766. }
  767. }
  768. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  769. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  770. 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)))
  771. }
  772. FCD(("Trying to enque %p\n", fcmd))
  773. if (!fc->scsi_que) {
  774. if (!fc->hw_enque (fc, fcmd)) {
  775. FCD(("hw_enque succeeded for %p\n", fcmd))
  776. return 0;
  777. }
  778. }
  779. FCD(("Putting into que1 %p\n", fcmd))
  780. fcp_scsi_insert_queue (fc, fcmd);
  781. return 0;
  782. }
  783. int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
  784. {
  785. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  786. fc_channel *fc = FC_SCMND(SCpnt);
  787. FCD(("Entering SCSI queuecommand %p\n", fcmd))
  788. if (SCpnt->done != fcp_scsi_done) {
  789. fcmd->done = SCpnt->done;
  790. SCpnt->done = fcp_scsi_done;
  791. SCpnt->scsi_done = done;
  792. fcmd->proto = TYPE_SCSI_FCP;
  793. if (!fc->scsi_free) {
  794. FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
  795. #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
  796. printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
  797. #endif
  798. return 1;
  799. }
  800. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
  801. }
  802. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
  803. }
  804. void fcp_queue_empty(fc_channel *fc)
  805. {
  806. fcp_cmnd *fcmd;
  807. FCD(("Queue empty\n"))
  808. while ((fcmd = fc->scsi_que)) {
  809. /* The hw told us we can try again queue some packet */
  810. if (fc->hw_enque (fc, fcmd))
  811. break;
  812. fcp_scsi_remove_queue (fc, fcmd);
  813. }
  814. }
  815. int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
  816. {
  817. /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
  818. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  819. fc_channel *fc = FC_SCMND(SCpnt);
  820. /*
  821. * We react to abort requests by simply forgetting
  822. * about the command and pretending everything's sweet.
  823. * This may or may not be silly. We can't, however,
  824. * immediately reuse the command's cmd_slots slot,
  825. * as its result may arrive later and we cannot
  826. * check whether it is the aborted one, can't we?
  827. *
  828. * Therefore, after the first few aborts are done,
  829. * we tell the scsi error handler to do something clever.
  830. * It will eventually call host reset, refreshing
  831. * cmd_slots for us.
  832. *
  833. * There is a theoretical chance that we sometimes allow
  834. * more than can_queue packets to the jungle this way,
  835. * but the worst outcome possible is a series of
  836. * more aborts and eventually the dev_reset catharsis.
  837. */
  838. if (++fc->abort_count < (fc->can_queue >> 1)) {
  839. unsigned long flags;
  840. SCpnt->result = DID_ABORT;
  841. fcmd->done(SCpnt);
  842. printk("FC: soft abort\n");
  843. return SUCCESS;
  844. } else {
  845. printk("FC: hard abort refused\n");
  846. return FAILED;
  847. }
  848. }
  849. void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
  850. {
  851. fc_channel *fc = FC_SCMND(SCpnt);
  852. fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
  853. up(fc->rst_pkt->device->host->eh_action);
  854. }
  855. #define FCP_RESET_TIMEOUT (2*HZ)
  856. int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
  857. {
  858. fcp_cmd *cmd;
  859. fcp_cmnd *fcmd;
  860. fc_channel *fc = FC_SCMND(SCpnt);
  861. DECLARE_MUTEX_LOCKED(sem);
  862. if (!fc->rst_pkt) {
  863. fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
  864. if (!fc->rst_pkt) return FAILED;
  865. fcmd = FCP_CMND(fc->rst_pkt);
  866. fcmd->token = 0;
  867. cmd = fc->scsi_cmd_pool + 0;
  868. FCD(("Preparing rst packet\n"))
  869. fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
  870. fc->rst_pkt->device = SCpnt->device;
  871. fc->rst_pkt->cmd_len = 0;
  872. fc->cmd_slots[0] = fcmd;
  873. cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
  874. fcmd->data = (dma_addr_t)NULL;
  875. fcmd->proto = TYPE_SCSI_FCP;
  876. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  877. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  878. 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)))
  879. } else {
  880. fcmd = FCP_CMND(fc->rst_pkt);
  881. if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
  882. return FAILED; /* or SUCCESS. Only these */
  883. }
  884. fc->rst_pkt->done = NULL;
  885. fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
  886. init_timer(&fc->rst_pkt->eh_timeout);
  887. fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
  888. fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
  889. fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
  890. add_timer(&fc->rst_pkt->eh_timeout);
  891. /*
  892. * Set up the semaphore so we wait for the command to complete.
  893. */
  894. fc->rst_pkt->device->host->eh_action = &sem;
  895. fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
  896. fc->rst_pkt->done = fcp_scsi_reset_done;
  897. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  898. fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
  899. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  900. down(&sem);
  901. fc->rst_pkt->device->host->eh_action = NULL;
  902. del_timer(&fc->rst_pkt->eh_timeout);
  903. /*
  904. * See if timeout. If so, tell the host to forget about it.
  905. * In other words, we don't want a callback any more.
  906. */
  907. if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
  908. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  909. return FAILED;
  910. }
  911. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  912. return SUCCESS;
  913. }
  914. static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  915. {
  916. fc_channel *fc = FC_SCMND(SCpnt);
  917. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  918. int i;
  919. printk ("FC: host reset\n");
  920. for (i=0; i < fc->can_queue; i++) {
  921. if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
  922. SCpnt->result = DID_RESET;
  923. fcmd->done(SCpnt);
  924. fc->cmd_slots[i] = NULL;
  925. }
  926. }
  927. fc->reset(fc);
  928. fc->abort_count = 0;
  929. if (fcp_initialize(fc, 1)) return SUCCESS;
  930. else return FAILED;
  931. }
  932. int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  933. {
  934. int rc;
  935. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  936. rc = __fcp_scsi_host_reset(SCpnt);
  937. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  938. return rc;
  939. }
  940. static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
  941. {
  942. long i;
  943. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  944. set_bit (i, fc->scsi_bitmap);
  945. fcmd->token = i;
  946. fc->scsi_free--;
  947. fc->cmd_slots[fcmd->token] = fcmd;
  948. return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  949. }
  950. static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
  951. {
  952. fcp_cmnd _fcmd, *fcmd;
  953. fc_hdr *fch;
  954. lse l;
  955. int i;
  956. fcmd = &_fcmd;
  957. memset(fcmd, 0, sizeof(fcmd));
  958. FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
  959. fch = &fcmd->fch;
  960. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
  961. FILL_FCHDR_SID(fch, fc->sid);
  962. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  963. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  964. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  965. fch->param = 0;
  966. fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
  967. fcmd->rsp = fcmd->cmd + len;
  968. fcmd->cmdlen = len;
  969. fcmd->rsplen = len;
  970. fcmd->data = (dma_addr_t)NULL;
  971. fcmd->fc = fc;
  972. fcmd->class = FC_CLASS_SIMPLE;
  973. fcmd->proto = TYPE_EXTENDED_LS;
  974. memset (&l, 0, sizeof(lse));
  975. l.magic = LSEMAGIC;
  976. init_MUTEX_LOCKED(&l.sem);
  977. l.timer.function = fcp_login_timeout;
  978. l.timer.data = (unsigned long)&l;
  979. l.status = FC_STATUS_TIMED_OUT;
  980. fcmd->ls = (void *)&l;
  981. disable_irq(fc->irq);
  982. fcp_els_queue_it(fc, fcmd);
  983. enable_irq(fc->irq);
  984. for (i = 0;;) {
  985. l.timer.expires = jiffies + 5 * HZ;
  986. add_timer(&l.timer);
  987. down(&l.sem);
  988. del_timer(&l.timer);
  989. if (l.status != FC_STATUS_TIMED_OUT) break;
  990. if (++i == 3) break;
  991. disable_irq(fc->irq);
  992. fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  993. enable_irq(fc->irq);
  994. }
  995. clear_bit(fcmd->token, fc->scsi_bitmap);
  996. fc->scsi_free++;
  997. dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
  998. return l.status;
  999. }
  1000. int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
  1001. {
  1002. logi *l;
  1003. int status;
  1004. l = (logi *)kmalloc(2 * sizeof(logi), GFP_KERNEL);
  1005. if (!l) return -ENOMEM;
  1006. memset(l, 0, 2 * sizeof(logi));
  1007. l->code = LS_PLOGI;
  1008. memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  1009. memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  1010. memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
  1011. memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
  1012. status = fc_do_els(fc, alpa, l, sizeof(logi));
  1013. if (status == FC_STATUS_OK) {
  1014. if (l[1].code == LS_ACC) {
  1015. #ifdef FCDEBUG
  1016. u32 *u = (u32 *)&l[1].nport_wwn;
  1017. FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
  1018. u[0], u[1], u[2], u[3]))
  1019. #endif
  1020. memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
  1021. memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
  1022. } else
  1023. status = FC_STATUS_BAD_RSP;
  1024. }
  1025. kfree(l);
  1026. return status;
  1027. }
  1028. typedef struct {
  1029. unsigned int code;
  1030. unsigned params[4];
  1031. } prli;
  1032. int fc_do_prli(fc_channel *fc, unsigned char alpa)
  1033. {
  1034. prli *p;
  1035. int status;
  1036. p = (prli *)kmalloc(2 * sizeof(prli), GFP_KERNEL);
  1037. if (!p) return -ENOMEM;
  1038. memset(p, 0, 2 * sizeof(prli));
  1039. p->code = LS_PRLI;
  1040. p->params[0] = 0x08002000;
  1041. p->params[3] = 0x00000022;
  1042. status = fc_do_els(fc, alpa, p, sizeof(prli));
  1043. if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
  1044. status = FC_STATUS_BAD_RSP;
  1045. kfree(p);
  1046. return status;
  1047. }
  1048. MODULE_LICENSE("GPL");