fc_fcp.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/fc/fc_fc2.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_encode.h>
  37. MODULE_AUTHOR("Open-FCoE.org");
  38. MODULE_DESCRIPTION("libfc");
  39. MODULE_LICENSE("GPL v2");
  40. unsigned int fc_debug_logging;
  41. module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
  42. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  43. static struct kmem_cache *scsi_pkt_cachep;
  44. /* SRB state definitions */
  45. #define FC_SRB_FREE 0 /* cmd is free */
  46. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  47. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  48. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  49. #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
  50. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  51. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  52. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  53. #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
  54. #define FC_SRB_READ (1 << 1)
  55. #define FC_SRB_WRITE (1 << 0)
  56. /*
  57. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  58. * that the command has been retruned to the scsi layer.
  59. */
  60. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  61. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  62. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  63. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  64. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  65. struct fc_fcp_internal {
  66. mempool_t *scsi_pkt_pool;
  67. struct list_head scsi_pkt_queue;
  68. u8 throttled;
  69. };
  70. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  71. /*
  72. * function prototypes
  73. * FC scsi I/O related functions
  74. */
  75. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  76. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  77. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  78. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  79. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  80. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
  81. static void fc_timeout_error(struct fc_fcp_pkt *);
  82. static void fc_fcp_timeout(unsigned long data);
  83. static void fc_fcp_rec(struct fc_fcp_pkt *);
  84. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  85. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  86. static void fc_io_compl(struct fc_fcp_pkt *);
  87. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  88. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  89. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  90. /*
  91. * command status codes
  92. */
  93. #define FC_COMPLETE 0
  94. #define FC_CMD_ABORTED 1
  95. #define FC_CMD_RESET 2
  96. #define FC_CMD_PLOGO 3
  97. #define FC_SNS_RCV 4
  98. #define FC_TRANS_ERR 5
  99. #define FC_DATA_OVRRUN 6
  100. #define FC_DATA_UNDRUN 7
  101. #define FC_ERROR 8
  102. #define FC_HRD_ERROR 9
  103. #define FC_CMD_TIME_OUT 10
  104. /*
  105. * Error recovery timeout values.
  106. */
  107. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  108. #define FC_SCSI_TM_TOV (10 * HZ)
  109. #define FC_SCSI_REC_TOV (2 * HZ)
  110. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  111. #define FC_MAX_ERROR_CNT 5
  112. #define FC_MAX_RECOV_RETRY 3
  113. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  114. /**
  115. * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
  116. * @lp: fc lport struct
  117. * @gfp: gfp flags for allocation
  118. *
  119. * This is used by upper layer scsi driver.
  120. * Return Value : scsi_pkt structure or null on allocation failure.
  121. * Context : call from process context. no locking required.
  122. */
  123. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
  124. {
  125. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  126. struct fc_fcp_pkt *fsp;
  127. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  128. if (fsp) {
  129. memset(fsp, 0, sizeof(*fsp));
  130. fsp->lp = lp;
  131. atomic_set(&fsp->ref_cnt, 1);
  132. init_timer(&fsp->timer);
  133. INIT_LIST_HEAD(&fsp->list);
  134. spin_lock_init(&fsp->scsi_pkt_lock);
  135. }
  136. return fsp;
  137. }
  138. /**
  139. * fc_fcp_pkt_release() - release hold on scsi_pkt packet
  140. * @fsp: fcp packet struct
  141. *
  142. * This is used by upper layer scsi driver.
  143. * Context : call from process and interrupt context.
  144. * no locking required
  145. */
  146. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  147. {
  148. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  149. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  150. mempool_free(fsp, si->scsi_pkt_pool);
  151. }
  152. }
  153. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  154. {
  155. atomic_inc(&fsp->ref_cnt);
  156. }
  157. /**
  158. * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
  159. * @seq: exchange sequence
  160. * @fsp: fcp packet struct
  161. *
  162. * Release hold on scsi_pkt packet set to keep scsi_pkt
  163. * till EM layer exch resource is not freed.
  164. * Context : called from from EM layer.
  165. * no locking required
  166. */
  167. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  168. {
  169. fc_fcp_pkt_release(fsp);
  170. }
  171. /**
  172. * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
  173. * @fsp: fcp packet
  174. *
  175. * We should only return error if we return a command to scsi-ml before
  176. * getting a response. This can happen in cases where we send a abort, but
  177. * do not wait for the response and the abort and command can be passing
  178. * each other on the wire/network-layer.
  179. *
  180. * Note: this function locks the packet and gets a reference to allow
  181. * callers to call the completion function while the lock is held and
  182. * not have to worry about the packets refcount.
  183. *
  184. * TODO: Maybe we should just have callers grab/release the lock and
  185. * have a function that they call to verify the fsp and grab a ref if
  186. * needed.
  187. */
  188. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  189. {
  190. spin_lock_bh(&fsp->scsi_pkt_lock);
  191. if (fsp->state & FC_SRB_COMPL) {
  192. spin_unlock_bh(&fsp->scsi_pkt_lock);
  193. return -EPERM;
  194. }
  195. fc_fcp_pkt_hold(fsp);
  196. return 0;
  197. }
  198. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  199. {
  200. spin_unlock_bh(&fsp->scsi_pkt_lock);
  201. fc_fcp_pkt_release(fsp);
  202. }
  203. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  204. {
  205. if (!(fsp->state & FC_SRB_COMPL))
  206. mod_timer(&fsp->timer, jiffies + delay);
  207. }
  208. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  209. {
  210. if (!fsp->seq_ptr)
  211. return -EINVAL;
  212. fsp->state |= FC_SRB_ABORT_PENDING;
  213. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  214. }
  215. /*
  216. * Retry command.
  217. * An abort isn't needed.
  218. */
  219. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  220. {
  221. if (fsp->seq_ptr) {
  222. fsp->lp->tt.exch_done(fsp->seq_ptr);
  223. fsp->seq_ptr = NULL;
  224. }
  225. fsp->state &= ~FC_SRB_ABORT_PENDING;
  226. fsp->io_status = 0;
  227. fsp->status_code = FC_ERROR;
  228. fc_fcp_complete_locked(fsp);
  229. }
  230. /*
  231. * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
  232. * transfer for a read I/O indicated by the fc_fcp_pkt.
  233. * @fsp: ptr to the fc_fcp_pkt
  234. *
  235. * This is called in exch_seq_send() when we have a newly allocated
  236. * exchange with a valid exchange id to setup ddp.
  237. *
  238. * returns: none
  239. */
  240. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  241. {
  242. struct fc_lport *lp;
  243. if (!fsp)
  244. return;
  245. lp = fsp->lp;
  246. if ((fsp->req_flags & FC_SRB_READ) &&
  247. (lp->lro_enabled) && (lp->tt.ddp_setup)) {
  248. if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
  249. scsi_sg_count(fsp->cmd)))
  250. fsp->xfer_ddp = xid;
  251. }
  252. }
  253. /*
  254. * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
  255. * DDP related resources for this I/O if it is initialized
  256. * as a ddp transfer
  257. * @fsp: ptr to the fc_fcp_pkt
  258. *
  259. * returns: none
  260. */
  261. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  262. {
  263. struct fc_lport *lp;
  264. if (!fsp)
  265. return;
  266. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  267. return;
  268. lp = fsp->lp;
  269. if (lp->tt.ddp_done) {
  270. fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
  271. fsp->xfer_ddp = FC_XID_UNKNOWN;
  272. }
  273. }
  274. /*
  275. * Receive SCSI data from target.
  276. * Called after receiving solicited data.
  277. */
  278. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  279. {
  280. struct scsi_cmnd *sc = fsp->cmd;
  281. struct fc_lport *lp = fsp->lp;
  282. struct fcoe_dev_stats *stats;
  283. struct fc_frame_header *fh;
  284. size_t start_offset;
  285. size_t offset;
  286. u32 crc;
  287. u32 copy_len = 0;
  288. size_t len;
  289. void *buf;
  290. struct scatterlist *sg;
  291. size_t remaining;
  292. fh = fc_frame_header_get(fp);
  293. offset = ntohl(fh->fh_parm_offset);
  294. start_offset = offset;
  295. len = fr_len(fp) - sizeof(*fh);
  296. buf = fc_frame_payload_get(fp, 0);
  297. /* if this I/O is ddped, update xfer len */
  298. fc_fcp_ddp_done(fsp);
  299. if (offset + len > fsp->data_len) {
  300. /* this should never happen */
  301. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  302. fc_frame_crc_check(fp))
  303. goto crc_err;
  304. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  305. "data_len %x\n", len, offset, fsp->data_len);
  306. fc_fcp_retry_cmd(fsp);
  307. return;
  308. }
  309. if (offset != fsp->xfer_len)
  310. fsp->state |= FC_SRB_DISCONTIG;
  311. crc = 0;
  312. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  313. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  314. sg = scsi_sglist(sc);
  315. remaining = len;
  316. while (remaining > 0 && sg) {
  317. size_t off;
  318. void *page_addr;
  319. size_t sg_bytes;
  320. if (offset >= sg->length) {
  321. offset -= sg->length;
  322. sg = sg_next(sg);
  323. continue;
  324. }
  325. sg_bytes = min(remaining, sg->length - offset);
  326. /*
  327. * The scatterlist item may be bigger than PAGE_SIZE,
  328. * but we are limited to mapping PAGE_SIZE at a time.
  329. */
  330. off = offset + sg->offset;
  331. sg_bytes = min(sg_bytes, (size_t)
  332. (PAGE_SIZE - (off & ~PAGE_MASK)));
  333. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
  334. KM_SOFTIRQ0);
  335. if (!page_addr)
  336. break; /* XXX panic? */
  337. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  338. crc = crc32(crc, buf, sg_bytes);
  339. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
  340. sg_bytes);
  341. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  342. buf += sg_bytes;
  343. offset += sg_bytes;
  344. remaining -= sg_bytes;
  345. copy_len += sg_bytes;
  346. }
  347. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  348. buf = fc_frame_payload_get(fp, 0);
  349. if (len % 4) {
  350. crc = crc32(crc, buf + len, 4 - (len % 4));
  351. len += 4 - (len % 4);
  352. }
  353. if (~crc != le32_to_cpu(fr_crc(fp))) {
  354. crc_err:
  355. stats = fc_lport_get_stats(lp);
  356. stats->ErrorFrames++;
  357. /* FIXME - per cpu count, not total count! */
  358. if (stats->InvalidCRCCount++ < 5)
  359. printk(KERN_WARNING "libfc: CRC error on data "
  360. "frame for port (%6x)\n",
  361. fc_host_port_id(lp->host));
  362. /*
  363. * Assume the frame is total garbage.
  364. * We may have copied it over the good part
  365. * of the buffer.
  366. * If so, we need to retry the entire operation.
  367. * Otherwise, ignore it.
  368. */
  369. if (fsp->state & FC_SRB_DISCONTIG)
  370. fc_fcp_retry_cmd(fsp);
  371. return;
  372. }
  373. }
  374. if (fsp->xfer_contig_end == start_offset)
  375. fsp->xfer_contig_end += copy_len;
  376. fsp->xfer_len += copy_len;
  377. /*
  378. * In the very rare event that this data arrived after the response
  379. * and completes the transfer, call the completion handler.
  380. */
  381. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  382. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  383. fc_fcp_complete_locked(fsp);
  384. }
  385. /**
  386. * fc_fcp_send_data() - Send SCSI data to target.
  387. * @fsp: ptr to fc_fcp_pkt
  388. * @sp: ptr to this sequence
  389. * @offset: starting offset for this data request
  390. * @seq_blen: the burst length for this data request
  391. *
  392. * Called after receiving a Transfer Ready data descriptor.
  393. * if LLD is capable of seq offload then send down seq_blen
  394. * size of data in single frame, otherwise send multiple FC
  395. * frames of max FC frame payload supported by target port.
  396. *
  397. * Returns : 0 for success.
  398. */
  399. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  400. size_t offset, size_t seq_blen)
  401. {
  402. struct fc_exch *ep;
  403. struct scsi_cmnd *sc;
  404. struct scatterlist *sg;
  405. struct fc_frame *fp = NULL;
  406. struct fc_lport *lp = fsp->lp;
  407. size_t remaining;
  408. size_t t_blen;
  409. size_t tlen;
  410. size_t sg_bytes;
  411. size_t frame_offset, fh_parm_offset;
  412. int error;
  413. void *data = NULL;
  414. void *page_addr;
  415. int using_sg = lp->sg_supp;
  416. u32 f_ctl;
  417. WARN_ON(seq_blen <= 0);
  418. if (unlikely(offset + seq_blen > fsp->data_len)) {
  419. /* this should never happen */
  420. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  421. "offset %zx\n", seq_blen, offset);
  422. fc_fcp_send_abort(fsp);
  423. return 0;
  424. } else if (offset != fsp->xfer_len) {
  425. /* Out of Order Data Request - no problem, but unexpected. */
  426. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  427. "seq_blen %zx offset %zx\n", seq_blen, offset);
  428. }
  429. /*
  430. * if LLD is capable of seq_offload then set transport
  431. * burst length (t_blen) to seq_blen, otherwise set t_blen
  432. * to max FC frame payload previously set in fsp->max_payload.
  433. */
  434. t_blen = fsp->max_payload;
  435. if (lp->seq_offload) {
  436. t_blen = min(seq_blen, (size_t)lp->lso_max);
  437. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  438. fsp, seq_blen, lp->lso_max, t_blen);
  439. }
  440. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  441. if (t_blen > 512)
  442. t_blen &= ~(512 - 1); /* round down to block size */
  443. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  444. sc = fsp->cmd;
  445. remaining = seq_blen;
  446. fh_parm_offset = frame_offset = offset;
  447. tlen = 0;
  448. seq = lp->tt.seq_start_next(seq);
  449. f_ctl = FC_FC_REL_OFF;
  450. WARN_ON(!seq);
  451. sg = scsi_sglist(sc);
  452. while (remaining > 0 && sg) {
  453. if (offset >= sg->length) {
  454. offset -= sg->length;
  455. sg = sg_next(sg);
  456. continue;
  457. }
  458. if (!fp) {
  459. tlen = min(t_blen, remaining);
  460. /*
  461. * TODO. Temporary workaround. fc_seq_send() can't
  462. * handle odd lengths in non-linear skbs.
  463. * This will be the final fragment only.
  464. */
  465. if (tlen % 4)
  466. using_sg = 0;
  467. if (using_sg) {
  468. fp = _fc_frame_alloc(lp, 0);
  469. if (!fp)
  470. return -ENOMEM;
  471. } else {
  472. fp = fc_frame_alloc(lp, tlen);
  473. if (!fp)
  474. return -ENOMEM;
  475. data = (void *)(fr_hdr(fp)) +
  476. sizeof(struct fc_frame_header);
  477. }
  478. fh_parm_offset = frame_offset;
  479. fr_max_payload(fp) = fsp->max_payload;
  480. }
  481. sg_bytes = min(tlen, sg->length - offset);
  482. if (using_sg) {
  483. get_page(sg_page(sg));
  484. skb_fill_page_desc(fp_skb(fp),
  485. skb_shinfo(fp_skb(fp))->nr_frags,
  486. sg_page(sg), sg->offset + offset,
  487. sg_bytes);
  488. fp_skb(fp)->data_len += sg_bytes;
  489. fr_len(fp) += sg_bytes;
  490. fp_skb(fp)->truesize += PAGE_SIZE;
  491. } else {
  492. size_t off = offset + sg->offset;
  493. /*
  494. * The scatterlist item may be bigger than PAGE_SIZE,
  495. * but we must not cross pages inside the kmap.
  496. */
  497. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  498. (off & ~PAGE_MASK)));
  499. page_addr = kmap_atomic(sg_page(sg) +
  500. (off >> PAGE_SHIFT),
  501. KM_SOFTIRQ0);
  502. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  503. sg_bytes);
  504. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  505. data += sg_bytes;
  506. }
  507. offset += sg_bytes;
  508. frame_offset += sg_bytes;
  509. tlen -= sg_bytes;
  510. remaining -= sg_bytes;
  511. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  512. (tlen))
  513. continue;
  514. /*
  515. * Send sequence with transfer sequence initiative in case
  516. * this is last FCP frame of the sequence.
  517. */
  518. if (remaining == 0)
  519. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  520. ep = fc_seq_exch(seq);
  521. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  522. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  523. /*
  524. * send fragment using for a sequence.
  525. */
  526. error = lp->tt.seq_send(lp, seq, fp);
  527. if (error) {
  528. WARN_ON(1); /* send error should be rare */
  529. fc_fcp_retry_cmd(fsp);
  530. return 0;
  531. }
  532. fp = NULL;
  533. }
  534. fsp->xfer_len += seq_blen; /* premature count? */
  535. return 0;
  536. }
  537. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  538. {
  539. int ba_done = 1;
  540. struct fc_ba_rjt *brp;
  541. struct fc_frame_header *fh;
  542. fh = fc_frame_header_get(fp);
  543. switch (fh->fh_r_ctl) {
  544. case FC_RCTL_BA_ACC:
  545. break;
  546. case FC_RCTL_BA_RJT:
  547. brp = fc_frame_payload_get(fp, sizeof(*brp));
  548. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  549. break;
  550. /* fall thru */
  551. default:
  552. /*
  553. * we will let the command timeout
  554. * and scsi-ml recover in this case,
  555. * therefore cleared the ba_done flag.
  556. */
  557. ba_done = 0;
  558. }
  559. if (ba_done) {
  560. fsp->state |= FC_SRB_ABORTED;
  561. fsp->state &= ~FC_SRB_ABORT_PENDING;
  562. if (fsp->wait_for_comp)
  563. complete(&fsp->tm_done);
  564. else
  565. fc_fcp_complete_locked(fsp);
  566. }
  567. }
  568. /**
  569. * fc_fcp_reduce_can_queue() - drop can_queue
  570. * @lp: lport to drop queueing for
  571. *
  572. * If we are getting memory allocation failures, then we may
  573. * be trying to execute too many commands. We let the running
  574. * commands complete or timeout, then try again with a reduced
  575. * can_queue. Eventually we will hit the point where we run
  576. * on all reserved structs.
  577. */
  578. static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
  579. {
  580. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  581. unsigned long flags;
  582. int can_queue;
  583. spin_lock_irqsave(lp->host->host_lock, flags);
  584. if (si->throttled)
  585. goto done;
  586. si->throttled = 1;
  587. can_queue = lp->host->can_queue;
  588. can_queue >>= 1;
  589. if (!can_queue)
  590. can_queue = 1;
  591. lp->host->can_queue = can_queue;
  592. shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
  593. "Reducing can_queue to %d.\n", can_queue);
  594. done:
  595. spin_unlock_irqrestore(lp->host->host_lock, flags);
  596. }
  597. /**
  598. * fc_fcp_recv() - Reveive FCP frames
  599. * @seq: The sequence the frame is on
  600. * @fp: The FC frame
  601. * @arg: The related FCP packet
  602. *
  603. * Return : None
  604. * Context : called from Soft IRQ context
  605. * can not called holding list lock
  606. */
  607. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  608. {
  609. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  610. struct fc_lport *lport = fsp->lp;
  611. struct fc_frame_header *fh;
  612. struct fcp_txrdy *dd;
  613. u8 r_ctl;
  614. int rc = 0;
  615. if (IS_ERR(fp))
  616. goto errout;
  617. fh = fc_frame_header_get(fp);
  618. r_ctl = fh->fh_r_ctl;
  619. if (!(lport->state & LPORT_ST_READY))
  620. goto out;
  621. if (fc_fcp_lock_pkt(fsp))
  622. goto out;
  623. fsp->last_pkt_time = jiffies;
  624. if (fh->fh_type == FC_TYPE_BLS) {
  625. fc_fcp_abts_resp(fsp, fp);
  626. goto unlock;
  627. }
  628. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  629. goto unlock;
  630. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  631. /*
  632. * received XFER RDY from the target
  633. * need to send data to the target
  634. */
  635. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  636. dd = fc_frame_payload_get(fp, sizeof(*dd));
  637. WARN_ON(!dd);
  638. rc = fc_fcp_send_data(fsp, seq,
  639. (size_t) ntohl(dd->ft_data_ro),
  640. (size_t) ntohl(dd->ft_burst_len));
  641. if (!rc)
  642. seq->rec_data = fsp->xfer_len;
  643. else if (rc == -ENOMEM)
  644. fsp->state |= FC_SRB_NOMEM;
  645. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  646. /*
  647. * received a DATA frame
  648. * next we will copy the data to the system buffer
  649. */
  650. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  651. fc_fcp_recv_data(fsp, fp);
  652. seq->rec_data = fsp->xfer_contig_end;
  653. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  654. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  655. fc_fcp_resp(fsp, fp);
  656. } else {
  657. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  658. }
  659. unlock:
  660. fc_fcp_unlock_pkt(fsp);
  661. out:
  662. fc_frame_free(fp);
  663. errout:
  664. if (IS_ERR(fp))
  665. fc_fcp_error(fsp, fp);
  666. else if (rc == -ENOMEM)
  667. fc_fcp_reduce_can_queue(lport);
  668. }
  669. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  670. {
  671. struct fc_frame_header *fh;
  672. struct fcp_resp *fc_rp;
  673. struct fcp_resp_ext *rp_ex;
  674. struct fcp_resp_rsp_info *fc_rp_info;
  675. u32 plen;
  676. u32 expected_len;
  677. u32 respl = 0;
  678. u32 snsl = 0;
  679. u8 flags = 0;
  680. plen = fr_len(fp);
  681. fh = (struct fc_frame_header *)fr_hdr(fp);
  682. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  683. goto len_err;
  684. plen -= sizeof(*fh);
  685. fc_rp = (struct fcp_resp *)(fh + 1);
  686. fsp->cdb_status = fc_rp->fr_status;
  687. flags = fc_rp->fr_flags;
  688. fsp->scsi_comp_flags = flags;
  689. expected_len = fsp->data_len;
  690. /* if ddp, update xfer len */
  691. fc_fcp_ddp_done(fsp);
  692. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  693. rp_ex = (void *)(fc_rp + 1);
  694. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  695. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  696. goto len_err;
  697. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  698. if (flags & FCP_RSP_LEN_VAL) {
  699. respl = ntohl(rp_ex->fr_rsp_len);
  700. if (respl != sizeof(*fc_rp_info))
  701. goto len_err;
  702. if (fsp->wait_for_comp) {
  703. /* Abuse cdb_status for rsp code */
  704. fsp->cdb_status = fc_rp_info->rsp_code;
  705. complete(&fsp->tm_done);
  706. /*
  707. * tmfs will not have any scsi cmd so
  708. * exit here
  709. */
  710. return;
  711. } else
  712. goto err;
  713. }
  714. if (flags & FCP_SNS_LEN_VAL) {
  715. snsl = ntohl(rp_ex->fr_sns_len);
  716. if (snsl > SCSI_SENSE_BUFFERSIZE)
  717. snsl = SCSI_SENSE_BUFFERSIZE;
  718. memcpy(fsp->cmd->sense_buffer,
  719. (char *)fc_rp_info + respl, snsl);
  720. }
  721. }
  722. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  723. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  724. goto len_err;
  725. if (flags & FCP_RESID_UNDER) {
  726. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  727. /*
  728. * The cmnd->underflow is the minimum number of
  729. * bytes that must be transfered for this
  730. * command. Provided a sense condition is not
  731. * present, make sure the actual amount
  732. * transferred is at least the underflow value
  733. * or fail.
  734. */
  735. if (!(flags & FCP_SNS_LEN_VAL) &&
  736. (fc_rp->fr_status == 0) &&
  737. (scsi_bufflen(fsp->cmd) -
  738. fsp->scsi_resid) < fsp->cmd->underflow)
  739. goto err;
  740. expected_len -= fsp->scsi_resid;
  741. } else {
  742. fsp->status_code = FC_ERROR;
  743. }
  744. }
  745. }
  746. fsp->state |= FC_SRB_RCV_STATUS;
  747. /*
  748. * Check for missing or extra data frames.
  749. */
  750. if (unlikely(fsp->xfer_len != expected_len)) {
  751. if (fsp->xfer_len < expected_len) {
  752. /*
  753. * Some data may be queued locally,
  754. * Wait a at least one jiffy to see if it is delivered.
  755. * If this expires without data, we may do SRR.
  756. */
  757. fc_fcp_timer_set(fsp, 2);
  758. return;
  759. }
  760. fsp->status_code = FC_DATA_OVRRUN;
  761. FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
  762. "len %x, data len %x\n",
  763. fsp->rport->port_id,
  764. fsp->xfer_len, expected_len, fsp->data_len);
  765. }
  766. fc_fcp_complete_locked(fsp);
  767. return;
  768. len_err:
  769. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  770. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  771. err:
  772. fsp->status_code = FC_ERROR;
  773. fc_fcp_complete_locked(fsp);
  774. }
  775. /**
  776. * fc_fcp_complete_locked() - complete processing of a fcp packet
  777. * @fsp: fcp packet
  778. *
  779. * This function may sleep if a timer is pending. The packet lock must be
  780. * held, and the host lock must not be held.
  781. */
  782. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  783. {
  784. struct fc_lport *lp = fsp->lp;
  785. struct fc_seq *seq;
  786. struct fc_exch *ep;
  787. u32 f_ctl;
  788. if (fsp->state & FC_SRB_ABORT_PENDING)
  789. return;
  790. if (fsp->state & FC_SRB_ABORTED) {
  791. if (!fsp->status_code)
  792. fsp->status_code = FC_CMD_ABORTED;
  793. } else {
  794. /*
  795. * Test for transport underrun, independent of response
  796. * underrun status.
  797. */
  798. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  799. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  800. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  801. fsp->status_code = FC_DATA_UNDRUN;
  802. fsp->io_status = 0;
  803. }
  804. }
  805. seq = fsp->seq_ptr;
  806. if (seq) {
  807. fsp->seq_ptr = NULL;
  808. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  809. struct fc_frame *conf_frame;
  810. struct fc_seq *csp;
  811. csp = lp->tt.seq_start_next(seq);
  812. conf_frame = fc_frame_alloc(fsp->lp, 0);
  813. if (conf_frame) {
  814. f_ctl = FC_FC_SEQ_INIT;
  815. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  816. ep = fc_seq_exch(seq);
  817. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  818. ep->did, ep->sid,
  819. FC_TYPE_FCP, f_ctl, 0);
  820. lp->tt.seq_send(lp, csp, conf_frame);
  821. }
  822. }
  823. lp->tt.exch_done(seq);
  824. }
  825. fc_io_compl(fsp);
  826. }
  827. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  828. {
  829. struct fc_lport *lp = fsp->lp;
  830. if (fsp->seq_ptr) {
  831. lp->tt.exch_done(fsp->seq_ptr);
  832. fsp->seq_ptr = NULL;
  833. }
  834. fsp->status_code = error;
  835. }
  836. /**
  837. * fc_fcp_cleanup_each_cmd() - Cleanup active commads
  838. * @lp: logical port
  839. * @id: target id
  840. * @lun: lun
  841. * @error: fsp status code
  842. *
  843. * If lun or id is -1, they are ignored.
  844. */
  845. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
  846. unsigned int lun, int error)
  847. {
  848. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  849. struct fc_fcp_pkt *fsp;
  850. struct scsi_cmnd *sc_cmd;
  851. unsigned long flags;
  852. spin_lock_irqsave(lp->host->host_lock, flags);
  853. restart:
  854. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  855. sc_cmd = fsp->cmd;
  856. if (id != -1 && scmd_id(sc_cmd) != id)
  857. continue;
  858. if (lun != -1 && sc_cmd->device->lun != lun)
  859. continue;
  860. fc_fcp_pkt_hold(fsp);
  861. spin_unlock_irqrestore(lp->host->host_lock, flags);
  862. if (!fc_fcp_lock_pkt(fsp)) {
  863. fc_fcp_cleanup_cmd(fsp, error);
  864. fc_io_compl(fsp);
  865. fc_fcp_unlock_pkt(fsp);
  866. }
  867. fc_fcp_pkt_release(fsp);
  868. spin_lock_irqsave(lp->host->host_lock, flags);
  869. /*
  870. * while we dropped the lock multiple pkts could
  871. * have been released, so we have to start over.
  872. */
  873. goto restart;
  874. }
  875. spin_unlock_irqrestore(lp->host->host_lock, flags);
  876. }
  877. static void fc_fcp_abort_io(struct fc_lport *lp)
  878. {
  879. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
  880. }
  881. /**
  882. * fc_fcp_pkt_send() - send a fcp packet to the lower level.
  883. * @lp: fc lport
  884. * @fsp: fc packet.
  885. *
  886. * This is called by upper layer protocol.
  887. * Return : zero for success and -1 for failure
  888. * Context : called from queuecommand which can be called from process
  889. * or scsi soft irq.
  890. * Locks : called with the host lock and irqs disabled.
  891. */
  892. static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  893. {
  894. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  895. int rc;
  896. fsp->cmd->SCp.ptr = (char *)fsp;
  897. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  898. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  899. int_to_scsilun(fsp->cmd->device->lun,
  900. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  901. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  902. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  903. spin_unlock_irq(lp->host->host_lock);
  904. rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
  905. spin_lock_irq(lp->host->host_lock);
  906. if (rc)
  907. list_del(&fsp->list);
  908. return rc;
  909. }
  910. static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  911. void (*resp)(struct fc_seq *,
  912. struct fc_frame *fp,
  913. void *arg))
  914. {
  915. struct fc_frame *fp;
  916. struct fc_seq *seq;
  917. struct fc_rport *rport;
  918. struct fc_rport_libfc_priv *rp;
  919. const size_t len = sizeof(fsp->cdb_cmd);
  920. int rc = 0;
  921. if (fc_fcp_lock_pkt(fsp))
  922. return 0;
  923. fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
  924. if (!fp) {
  925. rc = -1;
  926. goto unlock;
  927. }
  928. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  929. fr_fsp(fp) = fsp;
  930. rport = fsp->rport;
  931. fsp->max_payload = rport->maxframe_size;
  932. rp = rport->dd_data;
  933. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  934. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  935. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  936. seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
  937. if (!seq) {
  938. fc_frame_free(fp);
  939. rc = -1;
  940. goto unlock;
  941. }
  942. fsp->last_pkt_time = jiffies;
  943. fsp->seq_ptr = seq;
  944. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  945. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  946. fc_fcp_timer_set(fsp,
  947. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  948. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  949. unlock:
  950. fc_fcp_unlock_pkt(fsp);
  951. return rc;
  952. }
  953. /*
  954. * transport error handler
  955. */
  956. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  957. {
  958. int error = PTR_ERR(fp);
  959. if (fc_fcp_lock_pkt(fsp))
  960. return;
  961. if (error == -FC_EX_CLOSED) {
  962. fc_fcp_retry_cmd(fsp);
  963. goto unlock;
  964. }
  965. /*
  966. * clear abort pending, because the lower layer
  967. * decided to force completion.
  968. */
  969. fsp->state &= ~FC_SRB_ABORT_PENDING;
  970. fsp->status_code = FC_CMD_PLOGO;
  971. fc_fcp_complete_locked(fsp);
  972. unlock:
  973. fc_fcp_unlock_pkt(fsp);
  974. }
  975. /*
  976. * Scsi abort handler- calls to send an abort
  977. * and then wait for abort completion
  978. */
  979. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  980. {
  981. int rc = FAILED;
  982. if (fc_fcp_send_abort(fsp))
  983. return FAILED;
  984. init_completion(&fsp->tm_done);
  985. fsp->wait_for_comp = 1;
  986. spin_unlock_bh(&fsp->scsi_pkt_lock);
  987. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  988. spin_lock_bh(&fsp->scsi_pkt_lock);
  989. fsp->wait_for_comp = 0;
  990. if (!rc) {
  991. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  992. rc = FAILED;
  993. } else if (fsp->state & FC_SRB_ABORTED) {
  994. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  995. rc = SUCCESS;
  996. fc_fcp_complete_locked(fsp);
  997. }
  998. return rc;
  999. }
  1000. /*
  1001. * Retry LUN reset after resource allocation failed.
  1002. */
  1003. static void fc_lun_reset_send(unsigned long data)
  1004. {
  1005. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1006. struct fc_lport *lp = fsp->lp;
  1007. if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
  1008. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1009. return;
  1010. if (fc_fcp_lock_pkt(fsp))
  1011. return;
  1012. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1013. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1014. fc_fcp_unlock_pkt(fsp);
  1015. }
  1016. }
  1017. /*
  1018. * Scsi device reset handler- send a LUN RESET to the device
  1019. * and wait for reset reply
  1020. */
  1021. static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  1022. unsigned int id, unsigned int lun)
  1023. {
  1024. int rc;
  1025. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1026. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1027. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1028. fsp->wait_for_comp = 1;
  1029. init_completion(&fsp->tm_done);
  1030. fc_lun_reset_send((unsigned long)fsp);
  1031. /*
  1032. * wait for completion of reset
  1033. * after that make sure all commands are terminated
  1034. */
  1035. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1036. spin_lock_bh(&fsp->scsi_pkt_lock);
  1037. fsp->state |= FC_SRB_COMPL;
  1038. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1039. del_timer_sync(&fsp->timer);
  1040. spin_lock_bh(&fsp->scsi_pkt_lock);
  1041. if (fsp->seq_ptr) {
  1042. lp->tt.exch_done(fsp->seq_ptr);
  1043. fsp->seq_ptr = NULL;
  1044. }
  1045. fsp->wait_for_comp = 0;
  1046. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1047. if (!rc) {
  1048. FC_SCSI_DBG(lp, "lun reset failed\n");
  1049. return FAILED;
  1050. }
  1051. /* cdb_status holds the tmf's rsp code */
  1052. if (fsp->cdb_status != FCP_TMF_CMPL)
  1053. return FAILED;
  1054. FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun);
  1055. fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
  1056. return SUCCESS;
  1057. }
  1058. /*
  1059. * Task Managment response handler
  1060. */
  1061. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1062. {
  1063. struct fc_fcp_pkt *fsp = arg;
  1064. struct fc_frame_header *fh;
  1065. if (IS_ERR(fp)) {
  1066. /*
  1067. * If there is an error just let it timeout or wait
  1068. * for TMF to be aborted if it timedout.
  1069. *
  1070. * scsi-eh will escalate for when either happens.
  1071. */
  1072. return;
  1073. }
  1074. if (fc_fcp_lock_pkt(fsp))
  1075. return;
  1076. /*
  1077. * raced with eh timeout handler.
  1078. */
  1079. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1080. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1081. return;
  1082. }
  1083. fh = fc_frame_header_get(fp);
  1084. if (fh->fh_type != FC_TYPE_BLS)
  1085. fc_fcp_resp(fsp, fp);
  1086. fsp->seq_ptr = NULL;
  1087. fsp->lp->tt.exch_done(seq);
  1088. fc_frame_free(fp);
  1089. fc_fcp_unlock_pkt(fsp);
  1090. }
  1091. static void fc_fcp_cleanup(struct fc_lport *lp)
  1092. {
  1093. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
  1094. }
  1095. /*
  1096. * fc_fcp_timeout: called by OS timer function.
  1097. *
  1098. * The timer has been inactivated and must be reactivated if desired
  1099. * using fc_fcp_timer_set().
  1100. *
  1101. * Algorithm:
  1102. *
  1103. * If REC is supported, just issue it, and return. The REC exchange will
  1104. * complete or time out, and recovery can continue at that point.
  1105. *
  1106. * Otherwise, if the response has been received without all the data,
  1107. * it has been ER_TIMEOUT since the response was received.
  1108. *
  1109. * If the response has not been received,
  1110. * we see if data was received recently. If it has been, we continue waiting,
  1111. * otherwise, we abort the command.
  1112. */
  1113. static void fc_fcp_timeout(unsigned long data)
  1114. {
  1115. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1116. struct fc_rport *rport = fsp->rport;
  1117. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1118. if (fc_fcp_lock_pkt(fsp))
  1119. return;
  1120. if (fsp->cdb_cmd.fc_tm_flags)
  1121. goto unlock;
  1122. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1123. if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1124. fc_fcp_rec(fsp);
  1125. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1126. jiffies))
  1127. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1128. else if (fsp->state & FC_SRB_RCV_STATUS)
  1129. fc_fcp_complete_locked(fsp);
  1130. else
  1131. fc_timeout_error(fsp);
  1132. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1133. unlock:
  1134. fc_fcp_unlock_pkt(fsp);
  1135. }
  1136. /*
  1137. * Send a REC ELS request
  1138. */
  1139. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1140. {
  1141. struct fc_lport *lp;
  1142. struct fc_frame *fp;
  1143. struct fc_rport *rport;
  1144. struct fc_rport_libfc_priv *rp;
  1145. lp = fsp->lp;
  1146. rport = fsp->rport;
  1147. rp = rport->dd_data;
  1148. if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
  1149. fsp->status_code = FC_HRD_ERROR;
  1150. fsp->io_status = 0;
  1151. fc_fcp_complete_locked(fsp);
  1152. return;
  1153. }
  1154. fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
  1155. if (!fp)
  1156. goto retry;
  1157. fr_seq(fp) = fsp->seq_ptr;
  1158. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1159. fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
  1160. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1161. if (lp->tt.elsct_send(lp, rport->port_id, fp, ELS_REC, fc_fcp_rec_resp,
  1162. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1163. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1164. return;
  1165. }
  1166. fc_frame_free(fp);
  1167. retry:
  1168. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1169. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1170. else
  1171. fc_timeout_error(fsp);
  1172. }
  1173. /*
  1174. * Receive handler for REC ELS frame
  1175. * if it is a reject then let the scsi layer to handle
  1176. * the timeout. if it is a LS_ACC then if the io was not completed
  1177. * then set the timeout and return otherwise complete the exchange
  1178. * and tell the scsi layer to restart the I/O.
  1179. */
  1180. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1181. {
  1182. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1183. struct fc_els_rec_acc *recp;
  1184. struct fc_els_ls_rjt *rjt;
  1185. u32 e_stat;
  1186. u8 opcode;
  1187. u32 offset;
  1188. enum dma_data_direction data_dir;
  1189. enum fc_rctl r_ctl;
  1190. struct fc_rport_libfc_priv *rp;
  1191. if (IS_ERR(fp)) {
  1192. fc_fcp_rec_error(fsp, fp);
  1193. return;
  1194. }
  1195. if (fc_fcp_lock_pkt(fsp))
  1196. goto out;
  1197. fsp->recov_retry = 0;
  1198. opcode = fc_frame_payload_op(fp);
  1199. if (opcode == ELS_LS_RJT) {
  1200. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1201. switch (rjt->er_reason) {
  1202. default:
  1203. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1204. "reason %d expl %d\n",
  1205. fsp->rport->port_id, rjt->er_reason,
  1206. rjt->er_explan);
  1207. /* fall through */
  1208. case ELS_RJT_UNSUP:
  1209. FC_FCP_DBG(fsp, "device does not support REC\n");
  1210. rp = fsp->rport->dd_data;
  1211. /*
  1212. * if we do not spport RECs or got some bogus
  1213. * reason then resetup timer so we check for
  1214. * making progress.
  1215. */
  1216. rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1217. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1218. break;
  1219. case ELS_RJT_LOGIC:
  1220. case ELS_RJT_UNAB:
  1221. /*
  1222. * If no data transfer, the command frame got dropped
  1223. * so we just retry. If data was transferred, we
  1224. * lost the response but the target has no record,
  1225. * so we abort and retry.
  1226. */
  1227. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1228. fsp->xfer_len == 0) {
  1229. fc_fcp_retry_cmd(fsp);
  1230. break;
  1231. }
  1232. fc_timeout_error(fsp);
  1233. break;
  1234. }
  1235. } else if (opcode == ELS_LS_ACC) {
  1236. if (fsp->state & FC_SRB_ABORTED)
  1237. goto unlock_out;
  1238. data_dir = fsp->cmd->sc_data_direction;
  1239. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1240. offset = ntohl(recp->reca_fc4value);
  1241. e_stat = ntohl(recp->reca_e_stat);
  1242. if (e_stat & ESB_ST_COMPLETE) {
  1243. /*
  1244. * The exchange is complete.
  1245. *
  1246. * For output, we must've lost the response.
  1247. * For input, all data must've been sent.
  1248. * We lost may have lost the response
  1249. * (and a confirmation was requested) and maybe
  1250. * some data.
  1251. *
  1252. * If all data received, send SRR
  1253. * asking for response. If partial data received,
  1254. * or gaps, SRR requests data at start of gap.
  1255. * Recovery via SRR relies on in-order-delivery.
  1256. */
  1257. if (data_dir == DMA_TO_DEVICE) {
  1258. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1259. } else if (fsp->xfer_contig_end == offset) {
  1260. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1261. } else {
  1262. offset = fsp->xfer_contig_end;
  1263. r_ctl = FC_RCTL_DD_SOL_DATA;
  1264. }
  1265. fc_fcp_srr(fsp, r_ctl, offset);
  1266. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1267. /*
  1268. * The remote port has the initiative, so just
  1269. * keep waiting for it to complete.
  1270. */
  1271. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1272. } else {
  1273. /*
  1274. * The exchange is incomplete, we have seq. initiative.
  1275. * Lost response with requested confirmation,
  1276. * lost confirmation, lost transfer ready or
  1277. * lost write data.
  1278. *
  1279. * For output, if not all data was received, ask
  1280. * for transfer ready to be repeated.
  1281. *
  1282. * If we received or sent all the data, send SRR to
  1283. * request response.
  1284. *
  1285. * If we lost a response, we may have lost some read
  1286. * data as well.
  1287. */
  1288. r_ctl = FC_RCTL_DD_SOL_DATA;
  1289. if (data_dir == DMA_TO_DEVICE) {
  1290. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1291. if (offset < fsp->data_len)
  1292. r_ctl = FC_RCTL_DD_DATA_DESC;
  1293. } else if (offset == fsp->xfer_contig_end) {
  1294. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1295. } else if (fsp->xfer_contig_end < offset) {
  1296. offset = fsp->xfer_contig_end;
  1297. }
  1298. fc_fcp_srr(fsp, r_ctl, offset);
  1299. }
  1300. }
  1301. unlock_out:
  1302. fc_fcp_unlock_pkt(fsp);
  1303. out:
  1304. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1305. fc_frame_free(fp);
  1306. }
  1307. /*
  1308. * Handle error response or timeout for REC exchange.
  1309. */
  1310. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1311. {
  1312. int error = PTR_ERR(fp);
  1313. if (fc_fcp_lock_pkt(fsp))
  1314. goto out;
  1315. switch (error) {
  1316. case -FC_EX_CLOSED:
  1317. fc_fcp_retry_cmd(fsp);
  1318. break;
  1319. default:
  1320. FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
  1321. fsp, fsp->rport->port_id, error);
  1322. fsp->status_code = FC_CMD_PLOGO;
  1323. /* fall through */
  1324. case -FC_EX_TIMEOUT:
  1325. /*
  1326. * Assume REC or LS_ACC was lost.
  1327. * The exchange manager will have aborted REC, so retry.
  1328. */
  1329. FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
  1330. fsp->rport->port_id, error, fsp->recov_retry,
  1331. FC_MAX_RECOV_RETRY);
  1332. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1333. fc_fcp_rec(fsp);
  1334. else
  1335. fc_timeout_error(fsp);
  1336. break;
  1337. }
  1338. fc_fcp_unlock_pkt(fsp);
  1339. out:
  1340. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1341. }
  1342. /*
  1343. * Time out error routine:
  1344. * abort's the I/O close the exchange and
  1345. * send completion notification to scsi layer
  1346. */
  1347. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1348. {
  1349. fsp->status_code = FC_CMD_TIME_OUT;
  1350. fsp->cdb_status = 0;
  1351. fsp->io_status = 0;
  1352. /*
  1353. * if this fails then we let the scsi command timer fire and
  1354. * scsi-ml escalate.
  1355. */
  1356. fc_fcp_send_abort(fsp);
  1357. }
  1358. /*
  1359. * Sequence retransmission request.
  1360. * This is called after receiving status but insufficient data, or
  1361. * when expecting status but the request has timed out.
  1362. */
  1363. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1364. {
  1365. struct fc_lport *lp = fsp->lp;
  1366. struct fc_rport *rport;
  1367. struct fc_rport_libfc_priv *rp;
  1368. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1369. struct fc_seq *seq;
  1370. struct fcp_srr *srr;
  1371. struct fc_frame *fp;
  1372. u8 cdb_op;
  1373. rport = fsp->rport;
  1374. rp = rport->dd_data;
  1375. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1376. if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
  1377. goto retry; /* shouldn't happen */
  1378. fp = fc_frame_alloc(lp, sizeof(*srr));
  1379. if (!fp)
  1380. goto retry;
  1381. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1382. memset(srr, 0, sizeof(*srr));
  1383. srr->srr_op = ELS_SRR;
  1384. srr->srr_ox_id = htons(ep->oxid);
  1385. srr->srr_rx_id = htons(ep->rxid);
  1386. srr->srr_r_ctl = r_ctl;
  1387. srr->srr_rel_off = htonl(offset);
  1388. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1389. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  1390. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1391. seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
  1392. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1393. if (!seq) {
  1394. fc_frame_free(fp);
  1395. goto retry;
  1396. }
  1397. fsp->recov_seq = seq;
  1398. fsp->xfer_len = offset;
  1399. fsp->xfer_contig_end = offset;
  1400. fsp->state &= ~FC_SRB_RCV_STATUS;
  1401. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1402. return;
  1403. retry:
  1404. fc_fcp_retry_cmd(fsp);
  1405. }
  1406. /*
  1407. * Handle response from SRR.
  1408. */
  1409. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1410. {
  1411. struct fc_fcp_pkt *fsp = arg;
  1412. struct fc_frame_header *fh;
  1413. if (IS_ERR(fp)) {
  1414. fc_fcp_srr_error(fsp, fp);
  1415. return;
  1416. }
  1417. if (fc_fcp_lock_pkt(fsp))
  1418. goto out;
  1419. fh = fc_frame_header_get(fp);
  1420. /*
  1421. * BUG? fc_fcp_srr_error calls exch_done which would release
  1422. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1423. * then fc_exch_timeout would be sending an abort. The exch_done
  1424. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1425. * an abort response though.
  1426. */
  1427. if (fh->fh_type == FC_TYPE_BLS) {
  1428. fc_fcp_unlock_pkt(fsp);
  1429. return;
  1430. }
  1431. fsp->recov_seq = NULL;
  1432. switch (fc_frame_payload_op(fp)) {
  1433. case ELS_LS_ACC:
  1434. fsp->recov_retry = 0;
  1435. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1436. break;
  1437. case ELS_LS_RJT:
  1438. default:
  1439. fc_timeout_error(fsp);
  1440. break;
  1441. }
  1442. fc_fcp_unlock_pkt(fsp);
  1443. fsp->lp->tt.exch_done(seq);
  1444. out:
  1445. fc_frame_free(fp);
  1446. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1447. }
  1448. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1449. {
  1450. if (fc_fcp_lock_pkt(fsp))
  1451. goto out;
  1452. fsp->lp->tt.exch_done(fsp->recov_seq);
  1453. fsp->recov_seq = NULL;
  1454. switch (PTR_ERR(fp)) {
  1455. case -FC_EX_TIMEOUT:
  1456. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1457. fc_fcp_rec(fsp);
  1458. else
  1459. fc_timeout_error(fsp);
  1460. break;
  1461. case -FC_EX_CLOSED: /* e.g., link failure */
  1462. /* fall through */
  1463. default:
  1464. fc_fcp_retry_cmd(fsp);
  1465. break;
  1466. }
  1467. fc_fcp_unlock_pkt(fsp);
  1468. out:
  1469. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1470. }
  1471. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
  1472. {
  1473. /* lock ? */
  1474. return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
  1475. }
  1476. /**
  1477. * fc_queuecommand - The queuecommand function of the scsi template
  1478. * @cmd: struct scsi_cmnd to be executed
  1479. * @done: Callback function to be called when cmd is completed
  1480. *
  1481. * this is the i/o strategy routine, called by the scsi layer
  1482. * this routine is called with holding the host_lock.
  1483. */
  1484. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1485. {
  1486. struct fc_lport *lp;
  1487. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1488. struct fc_fcp_pkt *fsp;
  1489. struct fc_rport_libfc_priv *rp;
  1490. int rval;
  1491. int rc = 0;
  1492. struct fcoe_dev_stats *stats;
  1493. lp = shost_priv(sc_cmd->device->host);
  1494. rval = fc_remote_port_chkready(rport);
  1495. if (rval) {
  1496. sc_cmd->result = rval;
  1497. done(sc_cmd);
  1498. goto out;
  1499. }
  1500. if (!*(struct fc_remote_port **)rport->dd_data) {
  1501. /*
  1502. * rport is transitioning from blocked/deleted to
  1503. * online
  1504. */
  1505. sc_cmd->result = DID_IMM_RETRY << 16;
  1506. done(sc_cmd);
  1507. goto out;
  1508. }
  1509. rp = rport->dd_data;
  1510. if (!fc_fcp_lport_queue_ready(lp)) {
  1511. rc = SCSI_MLQUEUE_HOST_BUSY;
  1512. goto out;
  1513. }
  1514. fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
  1515. if (fsp == NULL) {
  1516. rc = SCSI_MLQUEUE_HOST_BUSY;
  1517. goto out;
  1518. }
  1519. /*
  1520. * build the libfc request pkt
  1521. */
  1522. fsp->cmd = sc_cmd; /* save the cmd */
  1523. fsp->lp = lp; /* save the softc ptr */
  1524. fsp->rport = rport; /* set the remote port ptr */
  1525. fsp->xfer_ddp = FC_XID_UNKNOWN;
  1526. sc_cmd->scsi_done = done;
  1527. /*
  1528. * set up the transfer length
  1529. */
  1530. fsp->data_len = scsi_bufflen(sc_cmd);
  1531. fsp->xfer_len = 0;
  1532. /*
  1533. * setup the data direction
  1534. */
  1535. stats = fc_lport_get_stats(lp);
  1536. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1537. fsp->req_flags = FC_SRB_READ;
  1538. stats->InputRequests++;
  1539. stats->InputMegabytes = fsp->data_len;
  1540. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1541. fsp->req_flags = FC_SRB_WRITE;
  1542. stats->OutputRequests++;
  1543. stats->OutputMegabytes = fsp->data_len;
  1544. } else {
  1545. fsp->req_flags = 0;
  1546. stats->ControlRequests++;
  1547. }
  1548. fsp->tgt_flags = rp->flags;
  1549. init_timer(&fsp->timer);
  1550. fsp->timer.data = (unsigned long)fsp;
  1551. /*
  1552. * send it to the lower layer
  1553. * if we get -1 return then put the request in the pending
  1554. * queue.
  1555. */
  1556. rval = fc_fcp_pkt_send(lp, fsp);
  1557. if (rval != 0) {
  1558. fsp->state = FC_SRB_FREE;
  1559. fc_fcp_pkt_release(fsp);
  1560. rc = SCSI_MLQUEUE_HOST_BUSY;
  1561. }
  1562. out:
  1563. return rc;
  1564. }
  1565. EXPORT_SYMBOL(fc_queuecommand);
  1566. /**
  1567. * fc_io_compl() - Handle responses for completed commands
  1568. * @fsp: scsi packet
  1569. *
  1570. * Translates a error to a Linux SCSI error.
  1571. *
  1572. * The fcp packet lock must be held when calling.
  1573. */
  1574. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1575. {
  1576. struct fc_fcp_internal *si;
  1577. struct scsi_cmnd *sc_cmd;
  1578. struct fc_lport *lp;
  1579. unsigned long flags;
  1580. /* release outstanding ddp context */
  1581. fc_fcp_ddp_done(fsp);
  1582. fsp->state |= FC_SRB_COMPL;
  1583. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1584. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1585. del_timer_sync(&fsp->timer);
  1586. spin_lock_bh(&fsp->scsi_pkt_lock);
  1587. }
  1588. lp = fsp->lp;
  1589. si = fc_get_scsi_internal(lp);
  1590. spin_lock_irqsave(lp->host->host_lock, flags);
  1591. if (!fsp->cmd) {
  1592. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1593. return;
  1594. }
  1595. /*
  1596. * if a command timed out while we had to try and throttle IO
  1597. * and it is now getting cleaned up, then we are about to
  1598. * try again so clear the throttled flag incase we get more
  1599. * time outs.
  1600. */
  1601. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1602. si->throttled = 0;
  1603. sc_cmd = fsp->cmd;
  1604. fsp->cmd = NULL;
  1605. if (!sc_cmd->SCp.ptr) {
  1606. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1607. return;
  1608. }
  1609. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1610. switch (fsp->status_code) {
  1611. case FC_COMPLETE:
  1612. if (fsp->cdb_status == 0) {
  1613. /*
  1614. * good I/O status
  1615. */
  1616. sc_cmd->result = DID_OK << 16;
  1617. if (fsp->scsi_resid)
  1618. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1619. } else if (fsp->cdb_status == QUEUE_FULL) {
  1620. struct scsi_device *tmp_sdev;
  1621. struct scsi_device *sdev = sc_cmd->device;
  1622. shost_for_each_device(tmp_sdev, sdev->host) {
  1623. if (tmp_sdev->id != sdev->id)
  1624. continue;
  1625. if (tmp_sdev->queue_depth > 1) {
  1626. scsi_track_queue_full(tmp_sdev,
  1627. tmp_sdev->
  1628. queue_depth - 1);
  1629. }
  1630. }
  1631. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1632. } else {
  1633. /*
  1634. * transport level I/O was ok but scsi
  1635. * has non zero status
  1636. */
  1637. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1638. }
  1639. break;
  1640. case FC_ERROR:
  1641. sc_cmd->result = DID_ERROR << 16;
  1642. break;
  1643. case FC_DATA_UNDRUN:
  1644. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1645. /*
  1646. * scsi status is good but transport level
  1647. * underrun.
  1648. */
  1649. sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
  1650. DID_OK : DID_ERROR) << 16;
  1651. } else {
  1652. /*
  1653. * scsi got underrun, this is an error
  1654. */
  1655. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1656. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1657. }
  1658. break;
  1659. case FC_DATA_OVRRUN:
  1660. /*
  1661. * overrun is an error
  1662. */
  1663. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1664. break;
  1665. case FC_CMD_ABORTED:
  1666. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1667. break;
  1668. case FC_CMD_TIME_OUT:
  1669. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1670. break;
  1671. case FC_CMD_RESET:
  1672. sc_cmd->result = (DID_RESET << 16);
  1673. break;
  1674. case FC_HRD_ERROR:
  1675. sc_cmd->result = (DID_NO_CONNECT << 16);
  1676. break;
  1677. default:
  1678. sc_cmd->result = (DID_ERROR << 16);
  1679. break;
  1680. }
  1681. list_del(&fsp->list);
  1682. sc_cmd->SCp.ptr = NULL;
  1683. sc_cmd->scsi_done(sc_cmd);
  1684. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1685. /* release ref from initial allocation in queue command */
  1686. fc_fcp_pkt_release(fsp);
  1687. }
  1688. /**
  1689. * fc_fcp_complete() - complete processing of a fcp packet
  1690. * @fsp: fcp packet
  1691. *
  1692. * This function may sleep if a fsp timer is pending.
  1693. * The host lock must not be held by caller.
  1694. */
  1695. void fc_fcp_complete(struct fc_fcp_pkt *fsp)
  1696. {
  1697. if (fc_fcp_lock_pkt(fsp))
  1698. return;
  1699. fc_fcp_complete_locked(fsp);
  1700. fc_fcp_unlock_pkt(fsp);
  1701. }
  1702. EXPORT_SYMBOL(fc_fcp_complete);
  1703. /**
  1704. * fc_eh_abort() - Abort a command
  1705. * @sc_cmd: scsi command to abort
  1706. *
  1707. * From scsi host template.
  1708. * send ABTS to the target device and wait for the response
  1709. * sc_cmd is the pointer to the command to be aborted.
  1710. */
  1711. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1712. {
  1713. struct fc_fcp_pkt *fsp;
  1714. struct fc_lport *lp;
  1715. int rc = FAILED;
  1716. unsigned long flags;
  1717. lp = shost_priv(sc_cmd->device->host);
  1718. if (lp->state != LPORT_ST_READY)
  1719. return rc;
  1720. else if (!lp->link_up)
  1721. return rc;
  1722. spin_lock_irqsave(lp->host->host_lock, flags);
  1723. fsp = CMD_SP(sc_cmd);
  1724. if (!fsp) {
  1725. /* command completed while scsi eh was setting up */
  1726. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1727. return SUCCESS;
  1728. }
  1729. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1730. fc_fcp_pkt_hold(fsp);
  1731. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1732. if (fc_fcp_lock_pkt(fsp)) {
  1733. /* completed while we were waiting for timer to be deleted */
  1734. rc = SUCCESS;
  1735. goto release_pkt;
  1736. }
  1737. rc = fc_fcp_pkt_abort(fsp);
  1738. fc_fcp_unlock_pkt(fsp);
  1739. release_pkt:
  1740. fc_fcp_pkt_release(fsp);
  1741. return rc;
  1742. }
  1743. EXPORT_SYMBOL(fc_eh_abort);
  1744. /**
  1745. * fc_eh_device_reset() Reset a single LUN
  1746. * @sc_cmd: scsi command
  1747. *
  1748. * Set from scsi host template to send tm cmd to the target and wait for the
  1749. * response.
  1750. */
  1751. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1752. {
  1753. struct fc_lport *lp;
  1754. struct fc_fcp_pkt *fsp;
  1755. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1756. int rc = FAILED;
  1757. struct fc_rport_libfc_priv *rp;
  1758. int rval;
  1759. rval = fc_remote_port_chkready(rport);
  1760. if (rval)
  1761. goto out;
  1762. rp = rport->dd_data;
  1763. lp = shost_priv(sc_cmd->device->host);
  1764. if (lp->state != LPORT_ST_READY)
  1765. return rc;
  1766. FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id);
  1767. fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
  1768. if (fsp == NULL) {
  1769. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1770. sc_cmd->result = DID_NO_CONNECT << 16;
  1771. goto out;
  1772. }
  1773. /*
  1774. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1775. * the sc passed in is not setup for execution like when sent
  1776. * through the queuecommand callout.
  1777. */
  1778. fsp->lp = lp; /* save the softc ptr */
  1779. fsp->rport = rport; /* set the remote port ptr */
  1780. /*
  1781. * flush outstanding commands
  1782. */
  1783. rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1784. fsp->state = FC_SRB_FREE;
  1785. fc_fcp_pkt_release(fsp);
  1786. out:
  1787. return rc;
  1788. }
  1789. EXPORT_SYMBOL(fc_eh_device_reset);
  1790. /**
  1791. * fc_eh_host_reset() - The reset function will reset the ports on the host.
  1792. * @sc_cmd: scsi command
  1793. */
  1794. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1795. {
  1796. struct Scsi_Host *shost = sc_cmd->device->host;
  1797. struct fc_lport *lp = shost_priv(shost);
  1798. unsigned long wait_tmo;
  1799. FC_SCSI_DBG(lp, "Resetting host\n");
  1800. lp->tt.lport_reset(lp);
  1801. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1802. while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
  1803. msleep(1000);
  1804. if (fc_fcp_lport_queue_ready(lp)) {
  1805. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1806. "on port (%6x)\n", fc_host_port_id(lp->host));
  1807. return SUCCESS;
  1808. } else {
  1809. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1810. "port (%6x) is not ready.\n",
  1811. fc_host_port_id(lp->host));
  1812. return FAILED;
  1813. }
  1814. }
  1815. EXPORT_SYMBOL(fc_eh_host_reset);
  1816. /**
  1817. * fc_slave_alloc() - configure queue depth
  1818. * @sdev: scsi device
  1819. *
  1820. * Configures queue depth based on host's cmd_per_len. If not set
  1821. * then we use the libfc default.
  1822. */
  1823. int fc_slave_alloc(struct scsi_device *sdev)
  1824. {
  1825. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1826. int queue_depth;
  1827. if (!rport || fc_remote_port_chkready(rport))
  1828. return -ENXIO;
  1829. if (sdev->tagged_supported) {
  1830. if (sdev->host->hostt->cmd_per_lun)
  1831. queue_depth = sdev->host->hostt->cmd_per_lun;
  1832. else
  1833. queue_depth = FC_FCP_DFLT_QUEUE_DEPTH;
  1834. scsi_activate_tcq(sdev, queue_depth);
  1835. }
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL(fc_slave_alloc);
  1839. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1840. {
  1841. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1842. return sdev->queue_depth;
  1843. }
  1844. EXPORT_SYMBOL(fc_change_queue_depth);
  1845. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1846. {
  1847. if (sdev->tagged_supported) {
  1848. scsi_set_tag_type(sdev, tag_type);
  1849. if (tag_type)
  1850. scsi_activate_tcq(sdev, sdev->queue_depth);
  1851. else
  1852. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1853. } else
  1854. tag_type = 0;
  1855. return tag_type;
  1856. }
  1857. EXPORT_SYMBOL(fc_change_queue_type);
  1858. void fc_fcp_destroy(struct fc_lport *lp)
  1859. {
  1860. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  1861. if (!list_empty(&si->scsi_pkt_queue))
  1862. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1863. "port (%6x)\n", fc_host_port_id(lp->host));
  1864. mempool_destroy(si->scsi_pkt_pool);
  1865. kfree(si);
  1866. lp->scsi_priv = NULL;
  1867. }
  1868. EXPORT_SYMBOL(fc_fcp_destroy);
  1869. int fc_fcp_init(struct fc_lport *lp)
  1870. {
  1871. int rc;
  1872. struct fc_fcp_internal *si;
  1873. if (!lp->tt.fcp_cmd_send)
  1874. lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1875. if (!lp->tt.fcp_cleanup)
  1876. lp->tt.fcp_cleanup = fc_fcp_cleanup;
  1877. if (!lp->tt.fcp_abort_io)
  1878. lp->tt.fcp_abort_io = fc_fcp_abort_io;
  1879. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1880. if (!si)
  1881. return -ENOMEM;
  1882. lp->scsi_priv = si;
  1883. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1884. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1885. if (!si->scsi_pkt_pool) {
  1886. rc = -ENOMEM;
  1887. goto free_internal;
  1888. }
  1889. return 0;
  1890. free_internal:
  1891. kfree(si);
  1892. return rc;
  1893. }
  1894. EXPORT_SYMBOL(fc_fcp_init);
  1895. static int __init libfc_init(void)
  1896. {
  1897. int rc;
  1898. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1899. sizeof(struct fc_fcp_pkt),
  1900. 0, SLAB_HWCACHE_ALIGN, NULL);
  1901. if (scsi_pkt_cachep == NULL) {
  1902. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1903. "module load failed!");
  1904. return -ENOMEM;
  1905. }
  1906. rc = fc_setup_exch_mgr();
  1907. if (rc)
  1908. goto destroy_pkt_cache;
  1909. rc = fc_setup_rport();
  1910. if (rc)
  1911. goto destroy_em;
  1912. return rc;
  1913. destroy_em:
  1914. fc_destroy_exch_mgr();
  1915. destroy_pkt_cache:
  1916. kmem_cache_destroy(scsi_pkt_cachep);
  1917. return rc;
  1918. }
  1919. static void __exit libfc_exit(void)
  1920. {
  1921. kmem_cache_destroy(scsi_pkt_cachep);
  1922. fc_destroy_exch_mgr();
  1923. fc_destroy_rport();
  1924. }
  1925. module_init(libfc_init);
  1926. module_exit(libfc_exit);