fc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  711. if (FCP_CMND(SCpnt)->done)
  712. FCP_CMND(SCpnt)->done(SCpnt);
  713. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  714. }
  715. static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
  716. {
  717. long i;
  718. fcp_cmd *cmd;
  719. u32 fcp_cntl;
  720. if (prepare) {
  721. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  722. set_bit (i, fc->scsi_bitmap);
  723. fcmd->token = i;
  724. cmd = fc->scsi_cmd_pool + i;
  725. if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
  726. /* Invalid channel/id/lun and couldn't map it into fcp_addr */
  727. clear_bit (i, fc->scsi_bitmap);
  728. SCpnt->result = (DID_BAD_TARGET << 16);
  729. SCpnt->scsi_done(SCpnt);
  730. return 0;
  731. }
  732. fc->scsi_free--;
  733. fc->cmd_slots[fcmd->token] = fcmd;
  734. if (SCpnt->device->tagged_supported) {
  735. if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
  736. fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
  737. fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
  738. } else
  739. fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
  740. } else
  741. fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
  742. if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
  743. cmd->fcp_cntl = fcp_cntl;
  744. fcmd->data = (dma_addr_t)NULL;
  745. } else {
  746. switch (SCpnt->cmnd[0]) {
  747. case WRITE_6:
  748. case WRITE_10:
  749. case WRITE_12:
  750. cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
  751. default:
  752. cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
  753. }
  754. if (!SCpnt->use_sg) {
  755. cmd->fcp_data_len = SCpnt->request_bufflen;
  756. fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
  757. SCpnt->request_bufflen,
  758. SCpnt->sc_data_direction);
  759. } else {
  760. struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
  761. int nents;
  762. FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
  763. nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
  764. SCpnt->sc_data_direction);
  765. if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
  766. fcmd->data = sg_dma_address(sg);
  767. cmd->fcp_data_len = sg_dma_len(sg);
  768. }
  769. }
  770. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  771. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  772. 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)))
  773. }
  774. FCD(("Trying to enque %p\n", fcmd))
  775. if (!fc->scsi_que) {
  776. if (!fc->hw_enque (fc, fcmd)) {
  777. FCD(("hw_enque succeeded for %p\n", fcmd))
  778. return 0;
  779. }
  780. }
  781. FCD(("Putting into que1 %p\n", fcmd))
  782. fcp_scsi_insert_queue (fc, fcmd);
  783. return 0;
  784. }
  785. int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
  786. {
  787. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  788. fc_channel *fc = FC_SCMND(SCpnt);
  789. FCD(("Entering SCSI queuecommand %p\n", fcmd))
  790. if (SCpnt->done != fcp_scsi_done) {
  791. fcmd->done = SCpnt->done;
  792. SCpnt->done = fcp_scsi_done;
  793. SCpnt->scsi_done = done;
  794. fcmd->proto = TYPE_SCSI_FCP;
  795. if (!fc->scsi_free) {
  796. FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
  797. #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
  798. printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
  799. #endif
  800. return 1;
  801. }
  802. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
  803. }
  804. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
  805. }
  806. void fcp_queue_empty(fc_channel *fc)
  807. {
  808. fcp_cmnd *fcmd;
  809. FCD(("Queue empty\n"))
  810. while ((fcmd = fc->scsi_que)) {
  811. /* The hw told us we can try again queue some packet */
  812. if (fc->hw_enque (fc, fcmd))
  813. break;
  814. fcp_scsi_remove_queue (fc, fcmd);
  815. }
  816. }
  817. int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
  818. {
  819. /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
  820. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  821. fc_channel *fc = FC_SCMND(SCpnt);
  822. /*
  823. * We react to abort requests by simply forgetting
  824. * about the command and pretending everything's sweet.
  825. * This may or may not be silly. We can't, however,
  826. * immediately reuse the command's cmd_slots slot,
  827. * as its result may arrive later and we cannot
  828. * check whether it is the aborted one, can't we?
  829. *
  830. * Therefore, after the first few aborts are done,
  831. * we tell the scsi error handler to do something clever.
  832. * It will eventually call host reset, refreshing
  833. * cmd_slots for us.
  834. *
  835. * There is a theoretical chance that we sometimes allow
  836. * more than can_queue packets to the jungle this way,
  837. * but the worst outcome possible is a series of
  838. * more aborts and eventually the dev_reset catharsis.
  839. */
  840. if (++fc->abort_count < (fc->can_queue >> 1)) {
  841. unsigned long flags;
  842. SCpnt->result = DID_ABORT;
  843. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  844. fcmd->done(SCpnt);
  845. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  846. printk("FC: soft abort\n");
  847. return SUCCESS;
  848. } else {
  849. printk("FC: hard abort refused\n");
  850. return FAILED;
  851. }
  852. }
  853. void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
  854. {
  855. fc_channel *fc = FC_SCMND(SCpnt);
  856. fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
  857. up(fc->rst_pkt->device->host->eh_action);
  858. }
  859. #define FCP_RESET_TIMEOUT (2*HZ)
  860. int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
  861. {
  862. fcp_cmd *cmd;
  863. fcp_cmnd *fcmd;
  864. fc_channel *fc = FC_SCMND(SCpnt);
  865. DECLARE_MUTEX_LOCKED(sem);
  866. if (!fc->rst_pkt) {
  867. fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
  868. if (!fc->rst_pkt) return FAILED;
  869. fcmd = FCP_CMND(fc->rst_pkt);
  870. fcmd->token = 0;
  871. cmd = fc->scsi_cmd_pool + 0;
  872. FCD(("Preparing rst packet\n"))
  873. fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
  874. fc->rst_pkt->device = SCpnt->device;
  875. fc->rst_pkt->cmd_len = 0;
  876. fc->cmd_slots[0] = fcmd;
  877. cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
  878. fcmd->data = (dma_addr_t)NULL;
  879. fcmd->proto = TYPE_SCSI_FCP;
  880. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  881. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  882. 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)))
  883. } else {
  884. fcmd = FCP_CMND(fc->rst_pkt);
  885. if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
  886. return FAILED; /* or SUCCESS. Only these */
  887. }
  888. fc->rst_pkt->done = NULL;
  889. fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
  890. init_timer(&fc->rst_pkt->eh_timeout);
  891. fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
  892. fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
  893. fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
  894. add_timer(&fc->rst_pkt->eh_timeout);
  895. /*
  896. * Set up the semaphore so we wait for the command to complete.
  897. */
  898. fc->rst_pkt->device->host->eh_action = &sem;
  899. fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
  900. fc->rst_pkt->done = fcp_scsi_reset_done;
  901. fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
  902. down(&sem);
  903. fc->rst_pkt->device->host->eh_action = NULL;
  904. del_timer(&fc->rst_pkt->eh_timeout);
  905. /*
  906. * See if timeout. If so, tell the host to forget about it.
  907. * In other words, we don't want a callback any more.
  908. */
  909. if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
  910. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  911. return FAILED;
  912. }
  913. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  914. return SUCCESS;
  915. }
  916. int fcp_scsi_bus_reset(Scsi_Cmnd *SCpnt)
  917. {
  918. printk ("FC: bus reset!\n");
  919. return FAILED;
  920. }
  921. int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  922. {
  923. fc_channel *fc = FC_SCMND(SCpnt);
  924. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  925. int i;
  926. printk ("FC: host reset\n");
  927. for (i=0; i < fc->can_queue; i++) {
  928. if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
  929. SCpnt->result = DID_RESET;
  930. fcmd->done(SCpnt);
  931. fc->cmd_slots[i] = NULL;
  932. }
  933. }
  934. fc->reset(fc);
  935. fc->abort_count = 0;
  936. if (fcp_initialize(fc, 1)) return SUCCESS;
  937. else return FAILED;
  938. }
  939. static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
  940. {
  941. long i;
  942. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  943. set_bit (i, fc->scsi_bitmap);
  944. fcmd->token = i;
  945. fc->scsi_free--;
  946. fc->cmd_slots[fcmd->token] = fcmd;
  947. return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  948. }
  949. static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
  950. {
  951. fcp_cmnd _fcmd, *fcmd;
  952. fc_hdr *fch;
  953. lse l;
  954. int i;
  955. fcmd = &_fcmd;
  956. memset(fcmd, 0, sizeof(fcmd));
  957. FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
  958. fch = &fcmd->fch;
  959. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
  960. FILL_FCHDR_SID(fch, fc->sid);
  961. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  962. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  963. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  964. fch->param = 0;
  965. fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
  966. fcmd->rsp = fcmd->cmd + len;
  967. fcmd->cmdlen = len;
  968. fcmd->rsplen = len;
  969. fcmd->data = (dma_addr_t)NULL;
  970. fcmd->fc = fc;
  971. fcmd->class = FC_CLASS_SIMPLE;
  972. fcmd->proto = TYPE_EXTENDED_LS;
  973. memset (&l, 0, sizeof(lse));
  974. l.magic = LSEMAGIC;
  975. init_MUTEX_LOCKED(&l.sem);
  976. l.timer.function = fcp_login_timeout;
  977. l.timer.data = (unsigned long)&l;
  978. l.status = FC_STATUS_TIMED_OUT;
  979. fcmd->ls = (void *)&l;
  980. disable_irq(fc->irq);
  981. fcp_els_queue_it(fc, fcmd);
  982. enable_irq(fc->irq);
  983. for (i = 0;;) {
  984. l.timer.expires = jiffies + 5 * HZ;
  985. add_timer(&l.timer);
  986. down(&l.sem);
  987. del_timer(&l.timer);
  988. if (l.status != FC_STATUS_TIMED_OUT) break;
  989. if (++i == 3) break;
  990. disable_irq(fc->irq);
  991. fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  992. enable_irq(fc->irq);
  993. }
  994. clear_bit(fcmd->token, fc->scsi_bitmap);
  995. fc->scsi_free++;
  996. dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
  997. return l.status;
  998. }
  999. int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
  1000. {
  1001. logi *l;
  1002. int status;
  1003. l = (logi *)kmalloc(2 * sizeof(logi), GFP_KERNEL);
  1004. if (!l) return -ENOMEM;
  1005. memset(l, 0, 2 * sizeof(logi));
  1006. l->code = LS_PLOGI;
  1007. memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  1008. memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  1009. memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
  1010. memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
  1011. status = fc_do_els(fc, alpa, l, sizeof(logi));
  1012. if (status == FC_STATUS_OK) {
  1013. if (l[1].code == LS_ACC) {
  1014. #ifdef FCDEBUG
  1015. u32 *u = (u32 *)&l[1].nport_wwn;
  1016. FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
  1017. u[0], u[1], u[2], u[3]))
  1018. #endif
  1019. memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
  1020. memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
  1021. } else
  1022. status = FC_STATUS_BAD_RSP;
  1023. }
  1024. kfree(l);
  1025. return status;
  1026. }
  1027. typedef struct {
  1028. unsigned int code;
  1029. unsigned params[4];
  1030. } prli;
  1031. int fc_do_prli(fc_channel *fc, unsigned char alpa)
  1032. {
  1033. prli *p;
  1034. int status;
  1035. p = (prli *)kmalloc(2 * sizeof(prli), GFP_KERNEL);
  1036. if (!p) return -ENOMEM;
  1037. memset(p, 0, 2 * sizeof(prli));
  1038. p->code = LS_PRLI;
  1039. p->params[0] = 0x08002000;
  1040. p->params[3] = 0x00000022;
  1041. status = fc_do_els(fc, alpa, p, sizeof(prli));
  1042. if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
  1043. status = FC_STATUS_BAD_RSP;
  1044. kfree(p);
  1045. return status;
  1046. }
  1047. MODULE_LICENSE("GPL");