fc_fcp.c 53 KB

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