libiscsi.c 51 KB

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