fc_fcp.c 53 KB

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