libiscsi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <asm/unaligned.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  47. #define SNA32_CHECK 2147483648UL
  48. static int iscsi_sna_lt(u32 n1, u32 n2)
  49. {
  50. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  54. static int iscsi_sna_lte(u32 n1, u32 n2)
  55. {
  56. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  57. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  58. }
  59. void
  60. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  61. {
  62. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  63. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  64. /*
  65. * standard specifies this check for when to update expected and
  66. * max sequence numbers
  67. */
  68. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  69. return;
  70. if (exp_cmdsn != session->exp_cmdsn &&
  71. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  72. session->exp_cmdsn = exp_cmdsn;
  73. if (max_cmdsn != session->max_cmdsn &&
  74. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  75. session->max_cmdsn = max_cmdsn;
  76. /*
  77. * if the window closed with IO queued, then kick the
  78. * xmit thread
  79. */
  80. if (!list_empty(&session->leadconn->xmitqueue) ||
  81. __kfifo_len(session->leadconn->mgmtqueue))
  82. scsi_queue_work(session->host,
  83. &session->leadconn->xmitwork);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  87. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  88. struct iscsi_data *hdr)
  89. {
  90. struct iscsi_conn *conn = ctask->conn;
  91. memset(hdr, 0, sizeof(struct iscsi_data));
  92. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  93. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  94. ctask->unsol_datasn++;
  95. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  96. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  97. hdr->itt = ctask->hdr->itt;
  98. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  99. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  100. if (ctask->unsol_count > conn->max_xmit_dlength) {
  101. hton24(hdr->dlength, conn->max_xmit_dlength);
  102. ctask->data_count = conn->max_xmit_dlength;
  103. ctask->unsol_offset += ctask->data_count;
  104. hdr->flags = 0;
  105. } else {
  106. hton24(hdr->dlength, ctask->unsol_count);
  107. ctask->data_count = ctask->unsol_count;
  108. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  112. /**
  113. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  114. * @ctask: iscsi cmd task
  115. *
  116. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  117. * fields like dlength or final based on how much data it sends
  118. */
  119. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  120. {
  121. struct iscsi_conn *conn = ctask->conn;
  122. struct iscsi_session *session = conn->session;
  123. struct iscsi_cmd *hdr = ctask->hdr;
  124. struct scsi_cmnd *sc = ctask->sc;
  125. hdr->opcode = ISCSI_OP_SCSI_CMD;
  126. hdr->flags = ISCSI_ATTR_SIMPLE;
  127. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  128. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  129. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  130. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  131. session->cmdsn++;
  132. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  133. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  134. if (sc->cmd_len < MAX_COMMAND_SIZE)
  135. memset(&hdr->cdb[sc->cmd_len], 0,
  136. MAX_COMMAND_SIZE - sc->cmd_len);
  137. ctask->data_count = 0;
  138. ctask->imm_count = 0;
  139. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  140. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  141. /*
  142. * Write counters:
  143. *
  144. * imm_count bytes to be sent right after
  145. * SCSI PDU Header
  146. *
  147. * unsol_count bytes(as Data-Out) to be sent
  148. * without R2T ack right after
  149. * immediate data
  150. *
  151. * r2t_data_count bytes to be sent via R2T ack's
  152. *
  153. * pad_count bytes to be sent as zero-padding
  154. */
  155. ctask->unsol_count = 0;
  156. ctask->unsol_offset = 0;
  157. ctask->unsol_datasn = 0;
  158. if (session->imm_data_en) {
  159. if (sc->request_bufflen >= session->first_burst)
  160. ctask->imm_count = min(session->first_burst,
  161. conn->max_xmit_dlength);
  162. else
  163. ctask->imm_count = min(sc->request_bufflen,
  164. conn->max_xmit_dlength);
  165. hton24(ctask->hdr->dlength, ctask->imm_count);
  166. } else
  167. zero_data(ctask->hdr->dlength);
  168. if (!session->initial_r2t_en) {
  169. ctask->unsol_count = min((session->first_burst),
  170. (sc->request_bufflen)) - ctask->imm_count;
  171. ctask->unsol_offset = ctask->imm_count;
  172. }
  173. if (!ctask->unsol_count)
  174. /* No unsolicit Data-Out's */
  175. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  176. } else {
  177. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  178. zero_data(hdr->dlength);
  179. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  180. hdr->flags |= ISCSI_FLAG_CMD_READ;
  181. }
  182. conn->scsicmd_pdus_cnt++;
  183. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  184. "cmdsn %d win %d]\n",
  185. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  186. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  187. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  188. }
  189. /**
  190. * iscsi_complete_command - return command back to scsi-ml
  191. * @ctask: iscsi cmd task
  192. *
  193. * Must be called with session lock.
  194. * This function returns the scsi command to scsi-ml and returns
  195. * the cmd task to the pool of available cmd tasks.
  196. */
  197. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  198. {
  199. struct iscsi_session *session = ctask->conn->session;
  200. struct scsi_cmnd *sc = ctask->sc;
  201. ctask->state = ISCSI_TASK_COMPLETED;
  202. ctask->sc = NULL;
  203. /* SCSI eh reuses commands to verify us */
  204. sc->SCp.ptr = NULL;
  205. list_del_init(&ctask->running);
  206. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  207. sc->scsi_done(sc);
  208. }
  209. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  210. {
  211. atomic_inc(&ctask->refcount);
  212. }
  213. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  214. {
  215. if (atomic_dec_and_test(&ctask->refcount))
  216. iscsi_complete_command(ctask);
  217. }
  218. /**
  219. * iscsi_cmd_rsp - SCSI Command Response processing
  220. * @conn: iscsi connection
  221. * @hdr: iscsi header
  222. * @ctask: scsi command task
  223. * @data: cmd data buffer
  224. * @datalen: len of buffer
  225. *
  226. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  227. * then completes the command and task.
  228. **/
  229. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  230. struct iscsi_cmd_task *ctask, char *data,
  231. int datalen)
  232. {
  233. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  234. struct iscsi_session *session = conn->session;
  235. struct scsi_cmnd *sc = ctask->sc;
  236. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  237. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  238. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  239. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  240. sc->result = DID_ERROR << 16;
  241. goto out;
  242. }
  243. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  244. uint16_t senselen;
  245. if (datalen < 2) {
  246. invalid_datalen:
  247. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  248. "invalid data buffer size of %d\n", datalen);
  249. sc->result = DID_BAD_TARGET << 16;
  250. goto out;
  251. }
  252. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  253. if (datalen < senselen)
  254. goto invalid_datalen;
  255. memcpy(sc->sense_buffer, data + 2,
  256. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  257. debug_scsi("copied %d bytes of sense\n",
  258. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  259. }
  260. if (sc->sc_data_direction == DMA_TO_DEVICE)
  261. goto out;
  262. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  263. int res_count = be32_to_cpu(rhdr->residual_count);
  264. if (res_count > 0 && res_count <= sc->request_bufflen)
  265. sc->resid = res_count;
  266. else
  267. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  268. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  269. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  270. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  271. sc->resid = be32_to_cpu(rhdr->residual_count);
  272. out:
  273. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  274. (long)sc, sc->result, ctask->itt);
  275. conn->scsirsp_pdus_cnt++;
  276. __iscsi_put_ctask(ctask);
  277. }
  278. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  279. {
  280. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  281. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  282. conn->tmfrsp_pdus_cnt++;
  283. if (conn->tmabort_state != TMABORT_INITIAL)
  284. return;
  285. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  286. conn->tmabort_state = TMABORT_SUCCESS;
  287. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  288. conn->tmabort_state = TMABORT_NOT_FOUND;
  289. else
  290. conn->tmabort_state = TMABORT_FAILED;
  291. wake_up(&conn->ehwait);
  292. }
  293. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  294. char *data, int datalen)
  295. {
  296. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  297. struct iscsi_hdr rejected_pdu;
  298. uint32_t itt;
  299. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  300. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  301. if (ntoh24(reject->dlength) > datalen)
  302. return ISCSI_ERR_PROTO;
  303. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  304. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  305. itt = get_itt(rejected_pdu.itt);
  306. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  307. "due to DataDigest error.\n", itt,
  308. rejected_pdu.opcode);
  309. }
  310. }
  311. return 0;
  312. }
  313. /**
  314. * __iscsi_complete_pdu - complete pdu
  315. * @conn: iscsi conn
  316. * @hdr: iscsi header
  317. * @data: data buffer
  318. * @datalen: len of data buffer
  319. *
  320. * Completes pdu processing by freeing any resources allocated at
  321. * queuecommand or send generic. session lock must be held and verify
  322. * itt must have been called.
  323. */
  324. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  325. char *data, int datalen)
  326. {
  327. struct iscsi_session *session = conn->session;
  328. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  329. struct iscsi_cmd_task *ctask;
  330. struct iscsi_mgmt_task *mtask;
  331. uint32_t itt;
  332. if (hdr->itt != RESERVED_ITT)
  333. itt = get_itt(hdr->itt);
  334. else
  335. itt = ~0U;
  336. if (itt < session->cmds_max) {
  337. ctask = session->cmds[itt];
  338. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  339. opcode, conn->id, ctask->itt, datalen);
  340. switch(opcode) {
  341. case ISCSI_OP_SCSI_CMD_RSP:
  342. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  343. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  344. datalen);
  345. break;
  346. case ISCSI_OP_SCSI_DATA_IN:
  347. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  348. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  349. conn->scsirsp_pdus_cnt++;
  350. __iscsi_put_ctask(ctask);
  351. }
  352. break;
  353. case ISCSI_OP_R2T:
  354. /* LLD handles this for now */
  355. break;
  356. default:
  357. rc = ISCSI_ERR_BAD_OPCODE;
  358. break;
  359. }
  360. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  361. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  362. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  363. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  364. opcode, conn->id, mtask->itt, datalen);
  365. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  366. switch(opcode) {
  367. case ISCSI_OP_LOGOUT_RSP:
  368. if (datalen) {
  369. rc = ISCSI_ERR_PROTO;
  370. break;
  371. }
  372. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  373. /* fall through */
  374. case ISCSI_OP_LOGIN_RSP:
  375. case ISCSI_OP_TEXT_RSP:
  376. /*
  377. * login related PDU's exp_statsn is handled in
  378. * userspace
  379. */
  380. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  381. rc = ISCSI_ERR_CONN_FAILED;
  382. list_del(&mtask->running);
  383. if (conn->login_mtask != mtask)
  384. __kfifo_put(session->mgmtpool.queue,
  385. (void*)&mtask, sizeof(void*));
  386. break;
  387. case ISCSI_OP_SCSI_TMFUNC_RSP:
  388. if (datalen) {
  389. rc = ISCSI_ERR_PROTO;
  390. break;
  391. }
  392. iscsi_tmf_rsp(conn, hdr);
  393. break;
  394. case ISCSI_OP_NOOP_IN:
  395. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  396. rc = ISCSI_ERR_PROTO;
  397. break;
  398. }
  399. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  400. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  401. rc = ISCSI_ERR_CONN_FAILED;
  402. list_del(&mtask->running);
  403. if (conn->login_mtask != mtask)
  404. __kfifo_put(session->mgmtpool.queue,
  405. (void*)&mtask, sizeof(void*));
  406. break;
  407. default:
  408. rc = ISCSI_ERR_BAD_OPCODE;
  409. break;
  410. }
  411. } else if (itt == ~0U) {
  412. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  413. switch(opcode) {
  414. case ISCSI_OP_NOOP_IN:
  415. if (datalen) {
  416. rc = ISCSI_ERR_PROTO;
  417. break;
  418. }
  419. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  420. break;
  421. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  422. rc = ISCSI_ERR_CONN_FAILED;
  423. break;
  424. case ISCSI_OP_REJECT:
  425. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  426. break;
  427. case ISCSI_OP_ASYNC_EVENT:
  428. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  429. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  430. rc = ISCSI_ERR_CONN_FAILED;
  431. break;
  432. default:
  433. rc = ISCSI_ERR_BAD_OPCODE;
  434. break;
  435. }
  436. } else
  437. rc = ISCSI_ERR_BAD_ITT;
  438. return rc;
  439. }
  440. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  441. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  442. char *data, int datalen)
  443. {
  444. int rc;
  445. spin_lock(&conn->session->lock);
  446. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  447. spin_unlock(&conn->session->lock);
  448. return rc;
  449. }
  450. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  451. /* verify itt (itt encoding: age+cid+itt) */
  452. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  453. uint32_t *ret_itt)
  454. {
  455. struct iscsi_session *session = conn->session;
  456. struct iscsi_cmd_task *ctask;
  457. uint32_t itt;
  458. if (hdr->itt != RESERVED_ITT) {
  459. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  460. (session->age << ISCSI_AGE_SHIFT)) {
  461. printk(KERN_ERR "iscsi: received itt %x expected "
  462. "session age (%x)\n", (__force u32)hdr->itt,
  463. session->age & ISCSI_AGE_MASK);
  464. return ISCSI_ERR_BAD_ITT;
  465. }
  466. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  467. (conn->id << ISCSI_CID_SHIFT)) {
  468. printk(KERN_ERR "iscsi: received itt %x, expected "
  469. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  470. return ISCSI_ERR_BAD_ITT;
  471. }
  472. itt = get_itt(hdr->itt);
  473. } else
  474. itt = ~0U;
  475. if (itt < session->cmds_max) {
  476. ctask = session->cmds[itt];
  477. if (!ctask->sc) {
  478. printk(KERN_INFO "iscsi: dropping ctask with "
  479. "itt 0x%x\n", ctask->itt);
  480. /* force drop */
  481. return ISCSI_ERR_NO_SCSI_CMD;
  482. }
  483. if (ctask->sc->SCp.phase != session->age) {
  484. printk(KERN_ERR "iscsi: ctask's session age %d, "
  485. "expected %d\n", ctask->sc->SCp.phase,
  486. session->age);
  487. return ISCSI_ERR_SESSION_FAILED;
  488. }
  489. }
  490. *ret_itt = itt;
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  494. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  495. {
  496. struct iscsi_session *session = conn->session;
  497. unsigned long flags;
  498. spin_lock_irqsave(&session->lock, flags);
  499. if (session->state == ISCSI_STATE_FAILED) {
  500. spin_unlock_irqrestore(&session->lock, flags);
  501. return;
  502. }
  503. if (conn->stop_stage == 0)
  504. session->state = ISCSI_STATE_FAILED;
  505. spin_unlock_irqrestore(&session->lock, flags);
  506. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  507. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  508. iscsi_conn_error(conn->cls_conn, err);
  509. }
  510. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  511. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  512. struct iscsi_mgmt_task *mtask)
  513. {
  514. struct iscsi_session *session = conn->session;
  515. struct iscsi_hdr *hdr = mtask->hdr;
  516. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  517. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  518. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  519. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  520. /*
  521. * pre-format CmdSN for outgoing PDU.
  522. */
  523. nop->cmdsn = cpu_to_be32(session->cmdsn);
  524. if (hdr->itt != RESERVED_ITT) {
  525. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  526. if (conn->c_stage == ISCSI_CONN_STARTED &&
  527. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  528. session->cmdsn++;
  529. }
  530. if (session->tt->init_mgmt_task)
  531. session->tt->init_mgmt_task(conn, mtask);
  532. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  533. hdr->opcode, hdr->itt, mtask->data_count);
  534. }
  535. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  536. {
  537. struct iscsi_hdr *hdr = conn->mtask->hdr;
  538. int rc, was_logout = 0;
  539. spin_unlock_bh(&conn->session->lock);
  540. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  541. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  542. iscsi_block_session(session_to_cls(conn->session));
  543. was_logout = 1;
  544. }
  545. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  546. spin_lock_bh(&conn->session->lock);
  547. if (rc)
  548. return rc;
  549. /* done with this in-progress mtask */
  550. conn->mtask = NULL;
  551. if (was_logout) {
  552. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  553. return -ENODATA;
  554. }
  555. return 0;
  556. }
  557. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  558. {
  559. struct iscsi_session *session = conn->session;
  560. /*
  561. * Check for iSCSI window and take care of CmdSN wrap-around
  562. */
  563. if (!iscsi_sna_lte(session->cmdsn, session->max_cmdsn)) {
  564. debug_scsi("iSCSI CmdSN closed. MaxCmdSN %u CmdSN %u\n",
  565. session->max_cmdsn, session->cmdsn);
  566. return -ENOSPC;
  567. }
  568. return 0;
  569. }
  570. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  571. {
  572. struct iscsi_cmd_task *ctask = conn->ctask;
  573. int rc = 0;
  574. /*
  575. * serialize with TMF AbortTask
  576. */
  577. if (ctask->state == ISCSI_TASK_ABORTING)
  578. goto done;
  579. __iscsi_get_ctask(ctask);
  580. spin_unlock_bh(&conn->session->lock);
  581. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  582. spin_lock_bh(&conn->session->lock);
  583. __iscsi_put_ctask(ctask);
  584. done:
  585. if (!rc)
  586. /* done with this ctask */
  587. conn->ctask = NULL;
  588. return rc;
  589. }
  590. /**
  591. * iscsi_data_xmit - xmit any command into the scheduled connection
  592. * @conn: iscsi connection
  593. *
  594. * Notes:
  595. * The function can return -EAGAIN in which case the caller must
  596. * re-schedule it again later or recover. '0' return code means
  597. * successful xmit.
  598. **/
  599. static int iscsi_data_xmit(struct iscsi_conn *conn)
  600. {
  601. int rc = 0;
  602. spin_lock_bh(&conn->session->lock);
  603. if (unlikely(conn->suspend_tx)) {
  604. debug_scsi("conn %d Tx suspended!\n", conn->id);
  605. spin_unlock_bh(&conn->session->lock);
  606. return -ENODATA;
  607. }
  608. if (conn->ctask) {
  609. rc = iscsi_xmit_ctask(conn);
  610. if (rc)
  611. goto again;
  612. }
  613. if (conn->mtask) {
  614. rc = iscsi_xmit_mtask(conn);
  615. if (rc)
  616. goto again;
  617. }
  618. /*
  619. * process mgmt pdus like nops before commands since we should
  620. * only have one nop-out as a ping from us and targets should not
  621. * overflow us with nop-ins
  622. */
  623. check_mgmt:
  624. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  625. sizeof(void*))) {
  626. iscsi_prep_mtask(conn, conn->mtask);
  627. list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
  628. rc = iscsi_xmit_mtask(conn);
  629. if (rc)
  630. goto again;
  631. }
  632. /* process command queue */
  633. while (!list_empty(&conn->xmitqueue)) {
  634. rc = iscsi_check_cmdsn_window_closed(conn);
  635. if (rc) {
  636. spin_unlock_bh(&conn->session->lock);
  637. return rc;
  638. }
  639. /*
  640. * iscsi tcp may readd the task to the xmitqueue to send
  641. * write data
  642. */
  643. conn->ctask = list_entry(conn->xmitqueue.next,
  644. struct iscsi_cmd_task, running);
  645. if (conn->ctask->state == ISCSI_TASK_PENDING) {
  646. iscsi_prep_scsi_cmd_pdu(conn->ctask);
  647. conn->session->tt->init_cmd_task(conn->ctask);
  648. }
  649. conn->ctask->state = ISCSI_TASK_RUNNING;
  650. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  651. rc = iscsi_xmit_ctask(conn);
  652. if (rc)
  653. goto again;
  654. /*
  655. * we could continuously get new ctask requests so
  656. * we need to check the mgmt queue for nops that need to
  657. * be sent to aviod starvation
  658. */
  659. if (__kfifo_len(conn->mgmtqueue))
  660. goto check_mgmt;
  661. }
  662. spin_unlock_bh(&conn->session->lock);
  663. return -ENODATA;
  664. again:
  665. if (unlikely(conn->suspend_tx))
  666. rc = -ENODATA;
  667. spin_unlock_bh(&conn->session->lock);
  668. return rc;
  669. }
  670. static void iscsi_xmitworker(struct work_struct *work)
  671. {
  672. struct iscsi_conn *conn =
  673. container_of(work, struct iscsi_conn, xmitwork);
  674. int rc;
  675. /*
  676. * serialize Xmit worker on a per-connection basis.
  677. */
  678. do {
  679. rc = iscsi_data_xmit(conn);
  680. } while (rc >= 0 || rc == -EAGAIN);
  681. }
  682. enum {
  683. FAILURE_BAD_HOST = 1,
  684. FAILURE_SESSION_FAILED,
  685. FAILURE_SESSION_FREED,
  686. FAILURE_WINDOW_CLOSED,
  687. FAILURE_OOM,
  688. FAILURE_SESSION_TERMINATE,
  689. FAILURE_SESSION_IN_RECOVERY,
  690. FAILURE_SESSION_RECOVERY_TIMEOUT,
  691. };
  692. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  693. {
  694. struct Scsi_Host *host;
  695. int reason = 0;
  696. struct iscsi_session *session;
  697. struct iscsi_conn *conn;
  698. struct iscsi_cmd_task *ctask = NULL;
  699. sc->scsi_done = done;
  700. sc->result = 0;
  701. sc->SCp.ptr = NULL;
  702. host = sc->device->host;
  703. session = iscsi_hostdata(host->hostdata);
  704. spin_lock(&session->lock);
  705. /*
  706. * ISCSI_STATE_FAILED is a temp. state. The recovery
  707. * code will decide what is best to do with command queued
  708. * during this time
  709. */
  710. if (session->state != ISCSI_STATE_LOGGED_IN &&
  711. session->state != ISCSI_STATE_FAILED) {
  712. /*
  713. * to handle the race between when we set the recovery state
  714. * and block the session we requeue here (commands could
  715. * be entering our queuecommand while a block is starting
  716. * up because the block code is not locked)
  717. */
  718. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  719. reason = FAILURE_SESSION_IN_RECOVERY;
  720. goto reject;
  721. }
  722. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  723. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  724. else if (session->state == ISCSI_STATE_TERMINATE)
  725. reason = FAILURE_SESSION_TERMINATE;
  726. else
  727. reason = FAILURE_SESSION_FREED;
  728. goto fault;
  729. }
  730. conn = session->leadconn;
  731. if (!conn) {
  732. reason = FAILURE_SESSION_FREED;
  733. goto fault;
  734. }
  735. /*
  736. * We check this here and in data xmit, because if we get to the point
  737. * that this check is hitting the window then we have enough IO in
  738. * flight and enough IO waiting to be transmitted it is better
  739. * to let the scsi/block layer queue up.
  740. */
  741. if (iscsi_check_cmdsn_window_closed(conn)) {
  742. reason = FAILURE_WINDOW_CLOSED;
  743. goto reject;
  744. }
  745. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  746. sizeof(void*))) {
  747. reason = FAILURE_OOM;
  748. goto reject;
  749. }
  750. sc->SCp.phase = session->age;
  751. sc->SCp.ptr = (char *)ctask;
  752. atomic_set(&ctask->refcount, 1);
  753. ctask->state = ISCSI_TASK_PENDING;
  754. ctask->mtask = NULL;
  755. ctask->conn = conn;
  756. ctask->sc = sc;
  757. INIT_LIST_HEAD(&ctask->running);
  758. list_add_tail(&ctask->running, &conn->xmitqueue);
  759. spin_unlock(&session->lock);
  760. scsi_queue_work(host, &conn->xmitwork);
  761. return 0;
  762. reject:
  763. spin_unlock(&session->lock);
  764. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  765. return SCSI_MLQUEUE_HOST_BUSY;
  766. fault:
  767. spin_unlock(&session->lock);
  768. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  769. sc->cmnd[0], reason);
  770. sc->result = (DID_NO_CONNECT << 16);
  771. sc->resid = sc->request_bufflen;
  772. sc->scsi_done(sc);
  773. return 0;
  774. }
  775. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  776. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  777. {
  778. if (depth > ISCSI_MAX_CMD_PER_LUN)
  779. depth = ISCSI_MAX_CMD_PER_LUN;
  780. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  781. return sdev->queue_depth;
  782. }
  783. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  784. static struct iscsi_mgmt_task *
  785. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  786. char *data, uint32_t data_size)
  787. {
  788. struct iscsi_session *session = conn->session;
  789. struct iscsi_mgmt_task *mtask;
  790. if (session->state == ISCSI_STATE_TERMINATE)
  791. return NULL;
  792. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  793. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  794. /*
  795. * Login and Text are sent serially, in
  796. * request-followed-by-response sequence.
  797. * Same mtask can be used. Same ITT must be used.
  798. * Note that login_mtask is preallocated at conn_create().
  799. */
  800. mtask = conn->login_mtask;
  801. else {
  802. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  803. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  804. if (!__kfifo_get(session->mgmtpool.queue,
  805. (void*)&mtask, sizeof(void*)))
  806. return NULL;
  807. }
  808. if (data_size) {
  809. memcpy(mtask->data, data, data_size);
  810. mtask->data_count = data_size;
  811. } else
  812. mtask->data_count = 0;
  813. INIT_LIST_HEAD(&mtask->running);
  814. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  815. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  816. return mtask;
  817. }
  818. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  819. char *data, uint32_t data_size)
  820. {
  821. struct iscsi_conn *conn = cls_conn->dd_data;
  822. struct iscsi_session *session = conn->session;
  823. int err = 0;
  824. spin_lock_bh(&session->lock);
  825. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  826. err = -EPERM;
  827. spin_unlock_bh(&session->lock);
  828. scsi_queue_work(session->host, &conn->xmitwork);
  829. return err;
  830. }
  831. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  832. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  833. {
  834. struct iscsi_session *session = class_to_transport_session(cls_session);
  835. struct iscsi_conn *conn = session->leadconn;
  836. spin_lock_bh(&session->lock);
  837. if (session->state != ISCSI_STATE_LOGGED_IN) {
  838. session->state = ISCSI_STATE_RECOVERY_FAILED;
  839. if (conn)
  840. wake_up(&conn->ehwait);
  841. }
  842. spin_unlock_bh(&session->lock);
  843. }
  844. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  845. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  846. {
  847. struct Scsi_Host *host = sc->device->host;
  848. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  849. struct iscsi_conn *conn = session->leadconn;
  850. int fail_session = 0;
  851. spin_lock_bh(&session->lock);
  852. if (session->state == ISCSI_STATE_TERMINATE) {
  853. failed:
  854. debug_scsi("failing host reset: session terminated "
  855. "[CID %d age %d]\n", conn->id, session->age);
  856. spin_unlock_bh(&session->lock);
  857. return FAILED;
  858. }
  859. if (sc->SCp.phase == session->age) {
  860. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  861. conn->id);
  862. fail_session = 1;
  863. }
  864. spin_unlock_bh(&session->lock);
  865. /*
  866. * we drop the lock here but the leadconn cannot be destoyed while
  867. * we are in the scsi eh
  868. */
  869. if (fail_session)
  870. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  871. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  872. wait_event_interruptible(conn->ehwait,
  873. session->state == ISCSI_STATE_TERMINATE ||
  874. session->state == ISCSI_STATE_LOGGED_IN ||
  875. session->state == ISCSI_STATE_RECOVERY_FAILED);
  876. if (signal_pending(current))
  877. flush_signals(current);
  878. spin_lock_bh(&session->lock);
  879. if (session->state == ISCSI_STATE_LOGGED_IN)
  880. printk(KERN_INFO "iscsi: host reset succeeded\n");
  881. else
  882. goto failed;
  883. spin_unlock_bh(&session->lock);
  884. return SUCCESS;
  885. }
  886. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  887. static void iscsi_tmabort_timedout(unsigned long data)
  888. {
  889. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  890. struct iscsi_conn *conn = ctask->conn;
  891. struct iscsi_session *session = conn->session;
  892. spin_lock(&session->lock);
  893. if (conn->tmabort_state == TMABORT_INITIAL) {
  894. conn->tmabort_state = TMABORT_TIMEDOUT;
  895. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  896. ctask->sc, ctask->itt);
  897. /* unblock eh_abort() */
  898. wake_up(&conn->ehwait);
  899. }
  900. spin_unlock(&session->lock);
  901. }
  902. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  903. struct iscsi_cmd_task *ctask)
  904. {
  905. struct iscsi_conn *conn = ctask->conn;
  906. struct iscsi_session *session = conn->session;
  907. struct iscsi_tm *hdr = &conn->tmhdr;
  908. /*
  909. * ctask timed out but session is OK requests must be serialized.
  910. */
  911. memset(hdr, 0, sizeof(struct iscsi_tm));
  912. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  913. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  914. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  915. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  916. hdr->rtt = ctask->hdr->itt;
  917. hdr->refcmdsn = ctask->hdr->cmdsn;
  918. ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  919. NULL, 0);
  920. if (!ctask->mtask) {
  921. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  922. debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
  923. return -EPERM;
  924. }
  925. ctask->state = ISCSI_TASK_ABORTING;
  926. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  927. if (conn->tmabort_state == TMABORT_INITIAL) {
  928. conn->tmfcmd_pdus_cnt++;
  929. conn->tmabort_timer.expires = 20*HZ + jiffies;
  930. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  931. conn->tmabort_timer.data = (unsigned long)ctask;
  932. add_timer(&conn->tmabort_timer);
  933. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  934. }
  935. spin_unlock_bh(&session->lock);
  936. scsi_queue_work(session->host, &conn->xmitwork);
  937. /*
  938. * block eh thread until:
  939. *
  940. * 1) abort response
  941. * 2) abort timeout
  942. * 3) session is terminated or restarted or userspace has
  943. * given up on recovery
  944. */
  945. wait_event_interruptible(conn->ehwait,
  946. sc->SCp.phase != session->age ||
  947. session->state != ISCSI_STATE_LOGGED_IN ||
  948. conn->tmabort_state != TMABORT_INITIAL);
  949. if (signal_pending(current))
  950. flush_signals(current);
  951. del_timer_sync(&conn->tmabort_timer);
  952. spin_lock_bh(&session->lock);
  953. return 0;
  954. }
  955. /*
  956. * session lock must be held
  957. */
  958. static struct iscsi_mgmt_task *
  959. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  960. {
  961. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  962. struct iscsi_mgmt_task *task;
  963. debug_scsi("searching %d tasks\n", nr_tasks);
  964. for (i = 0; i < nr_tasks; i++) {
  965. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  966. debug_scsi("check task %u\n", task->itt);
  967. if (task->itt == itt) {
  968. debug_scsi("matched task\n");
  969. return task;
  970. }
  971. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  972. }
  973. return NULL;
  974. }
  975. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  976. {
  977. struct iscsi_conn *conn = ctask->conn;
  978. struct iscsi_session *session = conn->session;
  979. if (!ctask->mtask)
  980. return -EINVAL;
  981. if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
  982. list_del(&ctask->mtask->running);
  983. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  984. sizeof(void*));
  985. ctask->mtask = NULL;
  986. return 0;
  987. }
  988. /*
  989. * session lock must be held
  990. */
  991. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  992. int err)
  993. {
  994. struct scsi_cmnd *sc;
  995. sc = ctask->sc;
  996. if (!sc)
  997. return;
  998. if (ctask->state != ISCSI_TASK_PENDING)
  999. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1000. iscsi_ctask_mtask_cleanup(ctask);
  1001. sc->result = err;
  1002. sc->resid = sc->request_bufflen;
  1003. if (conn->ctask == ctask)
  1004. conn->ctask = NULL;
  1005. /* release ref from queuecommand */
  1006. __iscsi_put_ctask(ctask);
  1007. }
  1008. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1009. {
  1010. struct iscsi_cmd_task *ctask;
  1011. struct iscsi_conn *conn;
  1012. struct iscsi_session *session;
  1013. int rc;
  1014. /*
  1015. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1016. * got the command.
  1017. */
  1018. if (!sc->SCp.ptr) {
  1019. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1020. return SUCCESS;
  1021. }
  1022. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1023. conn = ctask->conn;
  1024. session = conn->session;
  1025. conn->eh_abort_cnt++;
  1026. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1027. spin_lock_bh(&session->lock);
  1028. /*
  1029. * If we are not logged in or we have started a new session
  1030. * then let the host reset code handle this
  1031. */
  1032. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1033. sc->SCp.phase != session->age)
  1034. goto failed;
  1035. /* ctask completed before time out */
  1036. if (!ctask->sc) {
  1037. debug_scsi("sc completed while abort in progress\n");
  1038. goto success;
  1039. }
  1040. /* what should we do here ? */
  1041. if (conn->ctask == ctask) {
  1042. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1043. "Failing abort\n", sc, ctask->itt);
  1044. goto failed;
  1045. }
  1046. if (ctask->state == ISCSI_TASK_PENDING) {
  1047. fail_command(conn, ctask, DID_ABORT << 16);
  1048. goto success;
  1049. }
  1050. conn->tmabort_state = TMABORT_INITIAL;
  1051. rc = iscsi_exec_abort_task(sc, ctask);
  1052. if (rc || sc->SCp.phase != session->age ||
  1053. session->state != ISCSI_STATE_LOGGED_IN)
  1054. goto failed;
  1055. iscsi_ctask_mtask_cleanup(ctask);
  1056. switch (conn->tmabort_state) {
  1057. case TMABORT_SUCCESS:
  1058. spin_unlock_bh(&session->lock);
  1059. /*
  1060. * clean up task if aborted. grab the recv lock as a writer
  1061. */
  1062. write_lock_bh(conn->recv_lock);
  1063. spin_lock(&session->lock);
  1064. fail_command(conn, ctask, DID_ABORT << 16);
  1065. spin_unlock(&session->lock);
  1066. write_unlock_bh(conn->recv_lock);
  1067. /*
  1068. * make sure xmit thread is not still touching the
  1069. * ctask/scsi_cmnd
  1070. */
  1071. scsi_flush_work(session->host);
  1072. goto success_unlocked;
  1073. case TMABORT_NOT_FOUND:
  1074. if (!ctask->sc) {
  1075. /* ctask completed before tmf abort response */
  1076. debug_scsi("sc completed while abort in progress\n");
  1077. goto success;
  1078. }
  1079. /* fall through */
  1080. default:
  1081. /* timedout or failed */
  1082. spin_unlock_bh(&session->lock);
  1083. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1084. goto failed_unlocked;
  1085. }
  1086. success:
  1087. spin_unlock_bh(&session->lock);
  1088. success_unlocked:
  1089. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1090. return SUCCESS;
  1091. failed:
  1092. spin_unlock_bh(&session->lock);
  1093. failed_unlocked:
  1094. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1095. return FAILED;
  1096. }
  1097. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1098. int
  1099. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1100. {
  1101. int i;
  1102. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1103. if (*items == NULL)
  1104. return -ENOMEM;
  1105. q->max = max;
  1106. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1107. if (q->pool == NULL) {
  1108. kfree(*items);
  1109. return -ENOMEM;
  1110. }
  1111. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1112. GFP_KERNEL, NULL);
  1113. if (q->queue == ERR_PTR(-ENOMEM)) {
  1114. kfree(q->pool);
  1115. kfree(*items);
  1116. return -ENOMEM;
  1117. }
  1118. for (i = 0; i < max; i++) {
  1119. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1120. if (q->pool[i] == NULL) {
  1121. int j;
  1122. for (j = 0; j < i; j++)
  1123. kfree(q->pool[j]);
  1124. kfifo_free(q->queue);
  1125. kfree(q->pool);
  1126. kfree(*items);
  1127. return -ENOMEM;
  1128. }
  1129. memset(q->pool[i], 0, item_size);
  1130. (*items)[i] = q->pool[i];
  1131. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1132. }
  1133. return 0;
  1134. }
  1135. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1136. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1137. {
  1138. int i;
  1139. for (i = 0; i < q->max; i++)
  1140. kfree(items[i]);
  1141. kfree(q->pool);
  1142. kfree(items);
  1143. }
  1144. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1145. /*
  1146. * iSCSI Session's hostdata organization:
  1147. *
  1148. * *------------------* <== hostdata_session(host->hostdata)
  1149. * | ptr to class sess|
  1150. * |------------------| <== iscsi_hostdata(host->hostdata)
  1151. * | iscsi_session |
  1152. * *------------------*
  1153. */
  1154. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1155. _sz % sizeof(unsigned long))
  1156. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1157. /**
  1158. * iscsi_session_setup - create iscsi cls session and host and session
  1159. * @scsit: scsi transport template
  1160. * @iscsit: iscsi transport template
  1161. * @cmds_max: scsi host can queue
  1162. * @qdepth: scsi host cmds per lun
  1163. * @cmd_task_size: LLD ctask private data size
  1164. * @mgmt_task_size: LLD mtask private data size
  1165. * @initial_cmdsn: initial CmdSN
  1166. * @hostno: host no allocated
  1167. *
  1168. * This can be used by software iscsi_transports that allocate
  1169. * a session per scsi host.
  1170. **/
  1171. struct iscsi_cls_session *
  1172. iscsi_session_setup(struct iscsi_transport *iscsit,
  1173. struct scsi_transport_template *scsit,
  1174. uint16_t cmds_max, uint16_t qdepth,
  1175. int cmd_task_size, int mgmt_task_size,
  1176. uint32_t initial_cmdsn, uint32_t *hostno)
  1177. {
  1178. struct Scsi_Host *shost;
  1179. struct iscsi_session *session;
  1180. struct iscsi_cls_session *cls_session;
  1181. int cmd_i;
  1182. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1183. if (qdepth != 0)
  1184. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1185. "Queue depth must be between 1 and %d.\n",
  1186. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1187. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1188. }
  1189. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1190. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1191. if (cmds_max != 0)
  1192. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1193. "can_queue must be a power of 2 and between "
  1194. "2 and %d - setting to %d.\n", cmds_max,
  1195. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1196. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1197. }
  1198. shost = scsi_host_alloc(iscsit->host_template,
  1199. hostdata_privsize(sizeof(*session)));
  1200. if (!shost)
  1201. return NULL;
  1202. /* the iscsi layer takes one task for reserve */
  1203. shost->can_queue = cmds_max - 1;
  1204. shost->cmd_per_lun = qdepth;
  1205. shost->max_id = 1;
  1206. shost->max_channel = 0;
  1207. shost->max_lun = iscsit->max_lun;
  1208. shost->max_cmd_len = iscsit->max_cmd_len;
  1209. shost->transportt = scsit;
  1210. shost->transportt->create_work_queue = 1;
  1211. *hostno = shost->host_no;
  1212. session = iscsi_hostdata(shost->hostdata);
  1213. memset(session, 0, sizeof(struct iscsi_session));
  1214. session->host = shost;
  1215. session->state = ISCSI_STATE_FREE;
  1216. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1217. session->cmds_max = cmds_max;
  1218. session->cmdsn = initial_cmdsn;
  1219. session->exp_cmdsn = initial_cmdsn + 1;
  1220. session->max_cmdsn = initial_cmdsn + 1;
  1221. session->max_r2t = 1;
  1222. session->tt = iscsit;
  1223. /* initialize SCSI PDU commands pool */
  1224. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1225. (void***)&session->cmds,
  1226. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1227. goto cmdpool_alloc_fail;
  1228. /* pre-format cmds pool with ITT */
  1229. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1230. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1231. if (cmd_task_size)
  1232. ctask->dd_data = &ctask[1];
  1233. ctask->itt = cmd_i;
  1234. INIT_LIST_HEAD(&ctask->running);
  1235. }
  1236. spin_lock_init(&session->lock);
  1237. /* initialize immediate command pool */
  1238. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1239. (void***)&session->mgmt_cmds,
  1240. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1241. goto mgmtpool_alloc_fail;
  1242. /* pre-format immediate cmds pool with ITT */
  1243. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1244. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1245. if (mgmt_task_size)
  1246. mtask->dd_data = &mtask[1];
  1247. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1248. INIT_LIST_HEAD(&mtask->running);
  1249. }
  1250. if (scsi_add_host(shost, NULL))
  1251. goto add_host_fail;
  1252. if (!try_module_get(iscsit->owner))
  1253. goto cls_session_fail;
  1254. cls_session = iscsi_create_session(shost, iscsit, 0);
  1255. if (!cls_session)
  1256. goto module_put;
  1257. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1258. return cls_session;
  1259. module_put:
  1260. module_put(iscsit->owner);
  1261. cls_session_fail:
  1262. scsi_remove_host(shost);
  1263. add_host_fail:
  1264. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1265. mgmtpool_alloc_fail:
  1266. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1267. cmdpool_alloc_fail:
  1268. scsi_host_put(shost);
  1269. return NULL;
  1270. }
  1271. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1272. /**
  1273. * iscsi_session_teardown - destroy session, host, and cls_session
  1274. * shost: scsi host
  1275. *
  1276. * This can be used by software iscsi_transports that allocate
  1277. * a session per scsi host.
  1278. **/
  1279. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1280. {
  1281. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1282. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1283. struct module *owner = cls_session->transport->owner;
  1284. scsi_remove_host(shost);
  1285. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1286. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1287. kfree(session->password);
  1288. kfree(session->password_in);
  1289. kfree(session->username);
  1290. kfree(session->username_in);
  1291. kfree(session->targetname);
  1292. kfree(session->hwaddress);
  1293. kfree(session->initiatorname);
  1294. iscsi_destroy_session(cls_session);
  1295. scsi_host_put(shost);
  1296. module_put(owner);
  1297. }
  1298. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1299. /**
  1300. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1301. * @cls_session: iscsi_cls_session
  1302. * @conn_idx: cid
  1303. **/
  1304. struct iscsi_cls_conn *
  1305. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1306. {
  1307. struct iscsi_session *session = class_to_transport_session(cls_session);
  1308. struct iscsi_conn *conn;
  1309. struct iscsi_cls_conn *cls_conn;
  1310. char *data;
  1311. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1312. if (!cls_conn)
  1313. return NULL;
  1314. conn = cls_conn->dd_data;
  1315. memset(conn, 0, sizeof(*conn));
  1316. conn->session = session;
  1317. conn->cls_conn = cls_conn;
  1318. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1319. conn->id = conn_idx;
  1320. conn->exp_statsn = 0;
  1321. conn->tmabort_state = TMABORT_INITIAL;
  1322. INIT_LIST_HEAD(&conn->run_list);
  1323. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1324. INIT_LIST_HEAD(&conn->xmitqueue);
  1325. /* initialize general immediate & non-immediate PDU commands queue */
  1326. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1327. GFP_KERNEL, NULL);
  1328. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1329. goto mgmtqueue_alloc_fail;
  1330. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1331. /* allocate login_mtask used for the login/text sequences */
  1332. spin_lock_bh(&session->lock);
  1333. if (!__kfifo_get(session->mgmtpool.queue,
  1334. (void*)&conn->login_mtask,
  1335. sizeof(void*))) {
  1336. spin_unlock_bh(&session->lock);
  1337. goto login_mtask_alloc_fail;
  1338. }
  1339. spin_unlock_bh(&session->lock);
  1340. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1341. if (!data)
  1342. goto login_mtask_data_alloc_fail;
  1343. conn->login_mtask->data = conn->data = data;
  1344. init_timer(&conn->tmabort_timer);
  1345. init_waitqueue_head(&conn->ehwait);
  1346. return cls_conn;
  1347. login_mtask_data_alloc_fail:
  1348. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1349. sizeof(void*));
  1350. login_mtask_alloc_fail:
  1351. kfifo_free(conn->mgmtqueue);
  1352. mgmtqueue_alloc_fail:
  1353. iscsi_destroy_conn(cls_conn);
  1354. return NULL;
  1355. }
  1356. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1357. /**
  1358. * iscsi_conn_teardown - teardown iscsi connection
  1359. * cls_conn: iscsi class connection
  1360. *
  1361. * TODO: we may need to make this into a two step process
  1362. * like scsi-mls remove + put host
  1363. */
  1364. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1365. {
  1366. struct iscsi_conn *conn = cls_conn->dd_data;
  1367. struct iscsi_session *session = conn->session;
  1368. unsigned long flags;
  1369. spin_lock_bh(&session->lock);
  1370. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1371. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1372. if (session->leadconn == conn) {
  1373. /*
  1374. * leading connection? then give up on recovery.
  1375. */
  1376. session->state = ISCSI_STATE_TERMINATE;
  1377. wake_up(&conn->ehwait);
  1378. }
  1379. spin_unlock_bh(&session->lock);
  1380. /*
  1381. * Block until all in-progress commands for this connection
  1382. * time out or fail.
  1383. */
  1384. for (;;) {
  1385. spin_lock_irqsave(session->host->host_lock, flags);
  1386. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1387. spin_unlock_irqrestore(session->host->host_lock, flags);
  1388. break;
  1389. }
  1390. spin_unlock_irqrestore(session->host->host_lock, flags);
  1391. msleep_interruptible(500);
  1392. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1393. "host_failed %d\n", session->host->host_busy,
  1394. session->host->host_failed);
  1395. /*
  1396. * force eh_abort() to unblock
  1397. */
  1398. wake_up(&conn->ehwait);
  1399. }
  1400. /* flush queued up work because we free the connection below */
  1401. scsi_flush_work(session->host);
  1402. spin_lock_bh(&session->lock);
  1403. kfree(conn->data);
  1404. kfree(conn->persistent_address);
  1405. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1406. sizeof(void*));
  1407. if (session->leadconn == conn) {
  1408. session->leadconn = NULL;
  1409. /* no connections exits.. reset sequencing */
  1410. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1411. }
  1412. spin_unlock_bh(&session->lock);
  1413. kfifo_free(conn->mgmtqueue);
  1414. iscsi_destroy_conn(cls_conn);
  1415. }
  1416. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1417. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1418. {
  1419. struct iscsi_conn *conn = cls_conn->dd_data;
  1420. struct iscsi_session *session = conn->session;
  1421. if (!session) {
  1422. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1423. return -EPERM;
  1424. }
  1425. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1426. session->first_burst > session->max_burst) {
  1427. printk("iscsi: invalid burst lengths: "
  1428. "first_burst %d max_burst %d\n",
  1429. session->first_burst, session->max_burst);
  1430. return -EINVAL;
  1431. }
  1432. spin_lock_bh(&session->lock);
  1433. conn->c_stage = ISCSI_CONN_STARTED;
  1434. session->state = ISCSI_STATE_LOGGED_IN;
  1435. switch(conn->stop_stage) {
  1436. case STOP_CONN_RECOVER:
  1437. /*
  1438. * unblock eh_abort() if it is blocked. re-try all
  1439. * commands after successful recovery
  1440. */
  1441. conn->stop_stage = 0;
  1442. conn->tmabort_state = TMABORT_INITIAL;
  1443. session->age++;
  1444. spin_unlock_bh(&session->lock);
  1445. iscsi_unblock_session(session_to_cls(session));
  1446. wake_up(&conn->ehwait);
  1447. return 0;
  1448. case STOP_CONN_TERM:
  1449. conn->stop_stage = 0;
  1450. break;
  1451. default:
  1452. break;
  1453. }
  1454. spin_unlock_bh(&session->lock);
  1455. return 0;
  1456. }
  1457. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1458. static void
  1459. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1460. {
  1461. struct iscsi_mgmt_task *mtask, *tmp;
  1462. /* handle pending */
  1463. while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1464. if (mtask == conn->login_mtask)
  1465. continue;
  1466. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1467. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1468. sizeof(void*));
  1469. }
  1470. /* handle running */
  1471. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1472. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1473. list_del(&mtask->running);
  1474. if (mtask == conn->login_mtask)
  1475. continue;
  1476. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1477. sizeof(void*));
  1478. }
  1479. conn->mtask = NULL;
  1480. }
  1481. /* Fail commands. Mutex and session lock held and recv side suspended */
  1482. static void fail_all_commands(struct iscsi_conn *conn)
  1483. {
  1484. struct iscsi_cmd_task *ctask, *tmp;
  1485. /* flush pending */
  1486. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1487. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1488. ctask->itt);
  1489. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1490. }
  1491. /* fail all other running */
  1492. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1493. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1494. ctask->sc, ctask->itt);
  1495. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1496. }
  1497. conn->ctask = NULL;
  1498. }
  1499. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1500. struct iscsi_conn *conn, int flag)
  1501. {
  1502. int old_stop_stage;
  1503. spin_lock_bh(&session->lock);
  1504. if (conn->stop_stage == STOP_CONN_TERM) {
  1505. spin_unlock_bh(&session->lock);
  1506. return;
  1507. }
  1508. /*
  1509. * When this is called for the in_login state, we only want to clean
  1510. * up the login task and connection. We do not need to block and set
  1511. * the recovery state again
  1512. */
  1513. if (flag == STOP_CONN_TERM)
  1514. session->state = ISCSI_STATE_TERMINATE;
  1515. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1516. session->state = ISCSI_STATE_IN_RECOVERY;
  1517. old_stop_stage = conn->stop_stage;
  1518. conn->stop_stage = flag;
  1519. conn->c_stage = ISCSI_CONN_STOPPED;
  1520. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1521. spin_unlock_bh(&session->lock);
  1522. scsi_flush_work(session->host);
  1523. write_lock_bh(conn->recv_lock);
  1524. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1525. write_unlock_bh(conn->recv_lock);
  1526. /*
  1527. * for connection level recovery we should not calculate
  1528. * header digest. conn->hdr_size used for optimization
  1529. * in hdr_extract() and will be re-negotiated at
  1530. * set_param() time.
  1531. */
  1532. if (flag == STOP_CONN_RECOVER) {
  1533. conn->hdrdgst_en = 0;
  1534. conn->datadgst_en = 0;
  1535. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1536. old_stop_stage != STOP_CONN_RECOVER) {
  1537. debug_scsi("blocking session\n");
  1538. iscsi_block_session(session_to_cls(session));
  1539. }
  1540. }
  1541. /*
  1542. * flush queues.
  1543. */
  1544. spin_lock_bh(&session->lock);
  1545. fail_all_commands(conn);
  1546. flush_control_queues(session, conn);
  1547. spin_unlock_bh(&session->lock);
  1548. }
  1549. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1550. {
  1551. struct iscsi_conn *conn = cls_conn->dd_data;
  1552. struct iscsi_session *session = conn->session;
  1553. switch (flag) {
  1554. case STOP_CONN_RECOVER:
  1555. case STOP_CONN_TERM:
  1556. iscsi_start_session_recovery(session, conn, flag);
  1557. break;
  1558. default:
  1559. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1560. }
  1561. }
  1562. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1563. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1564. struct iscsi_cls_conn *cls_conn, int is_leading)
  1565. {
  1566. struct iscsi_session *session = class_to_transport_session(cls_session);
  1567. struct iscsi_conn *conn = cls_conn->dd_data;
  1568. spin_lock_bh(&session->lock);
  1569. if (is_leading)
  1570. session->leadconn = conn;
  1571. spin_unlock_bh(&session->lock);
  1572. /*
  1573. * Unblock xmitworker(), Login Phase will pass through.
  1574. */
  1575. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1576. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1577. return 0;
  1578. }
  1579. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1580. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1581. enum iscsi_param param, char *buf, int buflen)
  1582. {
  1583. struct iscsi_conn *conn = cls_conn->dd_data;
  1584. struct iscsi_session *session = conn->session;
  1585. uint32_t value;
  1586. switch(param) {
  1587. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1588. sscanf(buf, "%d", &conn->max_recv_dlength);
  1589. break;
  1590. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1591. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1592. break;
  1593. case ISCSI_PARAM_HDRDGST_EN:
  1594. sscanf(buf, "%d", &conn->hdrdgst_en);
  1595. break;
  1596. case ISCSI_PARAM_DATADGST_EN:
  1597. sscanf(buf, "%d", &conn->datadgst_en);
  1598. break;
  1599. case ISCSI_PARAM_INITIAL_R2T_EN:
  1600. sscanf(buf, "%d", &session->initial_r2t_en);
  1601. break;
  1602. case ISCSI_PARAM_MAX_R2T:
  1603. sscanf(buf, "%d", &session->max_r2t);
  1604. break;
  1605. case ISCSI_PARAM_IMM_DATA_EN:
  1606. sscanf(buf, "%d", &session->imm_data_en);
  1607. break;
  1608. case ISCSI_PARAM_FIRST_BURST:
  1609. sscanf(buf, "%d", &session->first_burst);
  1610. break;
  1611. case ISCSI_PARAM_MAX_BURST:
  1612. sscanf(buf, "%d", &session->max_burst);
  1613. break;
  1614. case ISCSI_PARAM_PDU_INORDER_EN:
  1615. sscanf(buf, "%d", &session->pdu_inorder_en);
  1616. break;
  1617. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1618. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1619. break;
  1620. case ISCSI_PARAM_ERL:
  1621. sscanf(buf, "%d", &session->erl);
  1622. break;
  1623. case ISCSI_PARAM_IFMARKER_EN:
  1624. sscanf(buf, "%d", &value);
  1625. BUG_ON(value);
  1626. break;
  1627. case ISCSI_PARAM_OFMARKER_EN:
  1628. sscanf(buf, "%d", &value);
  1629. BUG_ON(value);
  1630. break;
  1631. case ISCSI_PARAM_EXP_STATSN:
  1632. sscanf(buf, "%u", &conn->exp_statsn);
  1633. break;
  1634. case ISCSI_PARAM_USERNAME:
  1635. kfree(session->username);
  1636. session->username = kstrdup(buf, GFP_KERNEL);
  1637. if (!session->username)
  1638. return -ENOMEM;
  1639. break;
  1640. case ISCSI_PARAM_USERNAME_IN:
  1641. kfree(session->username_in);
  1642. session->username_in = kstrdup(buf, GFP_KERNEL);
  1643. if (!session->username_in)
  1644. return -ENOMEM;
  1645. break;
  1646. case ISCSI_PARAM_PASSWORD:
  1647. kfree(session->password);
  1648. session->password = kstrdup(buf, GFP_KERNEL);
  1649. if (!session->password)
  1650. return -ENOMEM;
  1651. break;
  1652. case ISCSI_PARAM_PASSWORD_IN:
  1653. kfree(session->password_in);
  1654. session->password_in = kstrdup(buf, GFP_KERNEL);
  1655. if (!session->password_in)
  1656. return -ENOMEM;
  1657. break;
  1658. case ISCSI_PARAM_TARGET_NAME:
  1659. /* this should not change between logins */
  1660. if (session->targetname)
  1661. break;
  1662. session->targetname = kstrdup(buf, GFP_KERNEL);
  1663. if (!session->targetname)
  1664. return -ENOMEM;
  1665. break;
  1666. case ISCSI_PARAM_TPGT:
  1667. sscanf(buf, "%d", &session->tpgt);
  1668. break;
  1669. case ISCSI_PARAM_PERSISTENT_PORT:
  1670. sscanf(buf, "%d", &conn->persistent_port);
  1671. break;
  1672. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1673. /*
  1674. * this is the address returned in discovery so it should
  1675. * not change between logins.
  1676. */
  1677. if (conn->persistent_address)
  1678. break;
  1679. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1680. if (!conn->persistent_address)
  1681. return -ENOMEM;
  1682. break;
  1683. default:
  1684. return -ENOSYS;
  1685. }
  1686. return 0;
  1687. }
  1688. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1689. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1690. enum iscsi_param param, char *buf)
  1691. {
  1692. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1693. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1694. int len;
  1695. switch(param) {
  1696. case ISCSI_PARAM_INITIAL_R2T_EN:
  1697. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1698. break;
  1699. case ISCSI_PARAM_MAX_R2T:
  1700. len = sprintf(buf, "%hu\n", session->max_r2t);
  1701. break;
  1702. case ISCSI_PARAM_IMM_DATA_EN:
  1703. len = sprintf(buf, "%d\n", session->imm_data_en);
  1704. break;
  1705. case ISCSI_PARAM_FIRST_BURST:
  1706. len = sprintf(buf, "%u\n", session->first_burst);
  1707. break;
  1708. case ISCSI_PARAM_MAX_BURST:
  1709. len = sprintf(buf, "%u\n", session->max_burst);
  1710. break;
  1711. case ISCSI_PARAM_PDU_INORDER_EN:
  1712. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1713. break;
  1714. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1715. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1716. break;
  1717. case ISCSI_PARAM_ERL:
  1718. len = sprintf(buf, "%d\n", session->erl);
  1719. break;
  1720. case ISCSI_PARAM_TARGET_NAME:
  1721. len = sprintf(buf, "%s\n", session->targetname);
  1722. break;
  1723. case ISCSI_PARAM_TPGT:
  1724. len = sprintf(buf, "%d\n", session->tpgt);
  1725. break;
  1726. case ISCSI_PARAM_USERNAME:
  1727. len = sprintf(buf, "%s\n", session->username);
  1728. break;
  1729. case ISCSI_PARAM_USERNAME_IN:
  1730. len = sprintf(buf, "%s\n", session->username_in);
  1731. break;
  1732. case ISCSI_PARAM_PASSWORD:
  1733. len = sprintf(buf, "%s\n", session->password);
  1734. break;
  1735. case ISCSI_PARAM_PASSWORD_IN:
  1736. len = sprintf(buf, "%s\n", session->password_in);
  1737. break;
  1738. default:
  1739. return -ENOSYS;
  1740. }
  1741. return len;
  1742. }
  1743. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1744. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1745. enum iscsi_param param, char *buf)
  1746. {
  1747. struct iscsi_conn *conn = cls_conn->dd_data;
  1748. int len;
  1749. switch(param) {
  1750. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1751. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1752. break;
  1753. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1754. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1755. break;
  1756. case ISCSI_PARAM_HDRDGST_EN:
  1757. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1758. break;
  1759. case ISCSI_PARAM_DATADGST_EN:
  1760. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1761. break;
  1762. case ISCSI_PARAM_IFMARKER_EN:
  1763. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1764. break;
  1765. case ISCSI_PARAM_OFMARKER_EN:
  1766. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1767. break;
  1768. case ISCSI_PARAM_EXP_STATSN:
  1769. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1770. break;
  1771. case ISCSI_PARAM_PERSISTENT_PORT:
  1772. len = sprintf(buf, "%d\n", conn->persistent_port);
  1773. break;
  1774. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1775. len = sprintf(buf, "%s\n", conn->persistent_address);
  1776. break;
  1777. default:
  1778. return -ENOSYS;
  1779. }
  1780. return len;
  1781. }
  1782. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1783. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1784. char *buf)
  1785. {
  1786. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1787. int len;
  1788. switch (param) {
  1789. case ISCSI_HOST_PARAM_HWADDRESS:
  1790. if (!session->hwaddress)
  1791. len = sprintf(buf, "%s\n", "default");
  1792. else
  1793. len = sprintf(buf, "%s\n", session->hwaddress);
  1794. break;
  1795. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1796. if (!session->initiatorname)
  1797. len = sprintf(buf, "%s\n", "unknown");
  1798. else
  1799. len = sprintf(buf, "%s\n", session->initiatorname);
  1800. break;
  1801. default:
  1802. return -ENOSYS;
  1803. }
  1804. return len;
  1805. }
  1806. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1807. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1808. char *buf, int buflen)
  1809. {
  1810. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1811. switch (param) {
  1812. case ISCSI_HOST_PARAM_HWADDRESS:
  1813. if (!session->hwaddress)
  1814. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1815. break;
  1816. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1817. if (!session->initiatorname)
  1818. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1819. break;
  1820. default:
  1821. return -ENOSYS;
  1822. }
  1823. return 0;
  1824. }
  1825. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1826. MODULE_AUTHOR("Mike Christie");
  1827. MODULE_DESCRIPTION("iSCSI library functions");
  1828. MODULE_LICENSE("GPL");