libiscsi.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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. * @initial_cmdsn: initial CmdSN
  1162. * @hostno: host no allocated
  1163. *
  1164. * This can be used by software iscsi_transports that allocate
  1165. * a session per scsi host.
  1166. **/
  1167. struct iscsi_cls_session *
  1168. iscsi_session_setup(struct iscsi_transport *iscsit,
  1169. struct scsi_transport_template *scsit,
  1170. int cmd_task_size, int mgmt_task_size,
  1171. uint32_t initial_cmdsn, uint32_t *hostno)
  1172. {
  1173. struct Scsi_Host *shost;
  1174. struct iscsi_session *session;
  1175. struct iscsi_cls_session *cls_session;
  1176. int cmd_i;
  1177. shost = scsi_host_alloc(iscsit->host_template,
  1178. hostdata_privsize(sizeof(*session)));
  1179. if (!shost)
  1180. return NULL;
  1181. shost->max_id = 1;
  1182. shost->max_channel = 0;
  1183. shost->max_lun = iscsit->max_lun;
  1184. shost->max_cmd_len = iscsit->max_cmd_len;
  1185. shost->transportt = scsit;
  1186. shost->transportt->create_work_queue = 1;
  1187. *hostno = shost->host_no;
  1188. session = iscsi_hostdata(shost->hostdata);
  1189. memset(session, 0, sizeof(struct iscsi_session));
  1190. session->host = shost;
  1191. session->state = ISCSI_STATE_FREE;
  1192. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1193. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1194. session->cmdsn = initial_cmdsn;
  1195. session->exp_cmdsn = initial_cmdsn + 1;
  1196. session->max_cmdsn = initial_cmdsn + 1;
  1197. session->max_r2t = 1;
  1198. session->tt = iscsit;
  1199. /* initialize SCSI PDU commands pool */
  1200. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1201. (void***)&session->cmds,
  1202. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1203. goto cmdpool_alloc_fail;
  1204. /* pre-format cmds pool with ITT */
  1205. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1206. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1207. if (cmd_task_size)
  1208. ctask->dd_data = &ctask[1];
  1209. ctask->itt = cmd_i;
  1210. INIT_LIST_HEAD(&ctask->running);
  1211. }
  1212. spin_lock_init(&session->lock);
  1213. /* initialize immediate command pool */
  1214. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1215. (void***)&session->mgmt_cmds,
  1216. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1217. goto mgmtpool_alloc_fail;
  1218. /* pre-format immediate cmds pool with ITT */
  1219. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1220. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1221. if (mgmt_task_size)
  1222. mtask->dd_data = &mtask[1];
  1223. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1224. INIT_LIST_HEAD(&mtask->running);
  1225. }
  1226. if (scsi_add_host(shost, NULL))
  1227. goto add_host_fail;
  1228. if (!try_module_get(iscsit->owner))
  1229. goto cls_session_fail;
  1230. cls_session = iscsi_create_session(shost, iscsit, 0);
  1231. if (!cls_session)
  1232. goto module_put;
  1233. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1234. return cls_session;
  1235. module_put:
  1236. module_put(iscsit->owner);
  1237. cls_session_fail:
  1238. scsi_remove_host(shost);
  1239. add_host_fail:
  1240. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1241. mgmtpool_alloc_fail:
  1242. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1243. cmdpool_alloc_fail:
  1244. scsi_host_put(shost);
  1245. return NULL;
  1246. }
  1247. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1248. /**
  1249. * iscsi_session_teardown - destroy session, host, and cls_session
  1250. * shost: scsi host
  1251. *
  1252. * This can be used by software iscsi_transports that allocate
  1253. * a session per scsi host.
  1254. **/
  1255. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1256. {
  1257. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1258. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1259. struct module *owner = cls_session->transport->owner;
  1260. scsi_remove_host(shost);
  1261. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1262. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1263. kfree(session->password);
  1264. kfree(session->password_in);
  1265. kfree(session->username);
  1266. kfree(session->username_in);
  1267. kfree(session->targetname);
  1268. kfree(session->hwaddress);
  1269. kfree(session->initiatorname);
  1270. iscsi_destroy_session(cls_session);
  1271. scsi_host_put(shost);
  1272. module_put(owner);
  1273. }
  1274. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1275. /**
  1276. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1277. * @cls_session: iscsi_cls_session
  1278. * @conn_idx: cid
  1279. **/
  1280. struct iscsi_cls_conn *
  1281. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1282. {
  1283. struct iscsi_session *session = class_to_transport_session(cls_session);
  1284. struct iscsi_conn *conn;
  1285. struct iscsi_cls_conn *cls_conn;
  1286. char *data;
  1287. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1288. if (!cls_conn)
  1289. return NULL;
  1290. conn = cls_conn->dd_data;
  1291. memset(conn, 0, sizeof(*conn));
  1292. conn->session = session;
  1293. conn->cls_conn = cls_conn;
  1294. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1295. conn->id = conn_idx;
  1296. conn->exp_statsn = 0;
  1297. conn->tmabort_state = TMABORT_INITIAL;
  1298. INIT_LIST_HEAD(&conn->run_list);
  1299. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1300. INIT_LIST_HEAD(&conn->xmitqueue);
  1301. /* initialize general immediate & non-immediate PDU commands queue */
  1302. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1303. GFP_KERNEL, NULL);
  1304. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1305. goto mgmtqueue_alloc_fail;
  1306. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1307. /* allocate login_mtask used for the login/text sequences */
  1308. spin_lock_bh(&session->lock);
  1309. if (!__kfifo_get(session->mgmtpool.queue,
  1310. (void*)&conn->login_mtask,
  1311. sizeof(void*))) {
  1312. spin_unlock_bh(&session->lock);
  1313. goto login_mtask_alloc_fail;
  1314. }
  1315. spin_unlock_bh(&session->lock);
  1316. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1317. if (!data)
  1318. goto login_mtask_data_alloc_fail;
  1319. conn->login_mtask->data = conn->data = data;
  1320. init_timer(&conn->tmabort_timer);
  1321. init_waitqueue_head(&conn->ehwait);
  1322. return cls_conn;
  1323. login_mtask_data_alloc_fail:
  1324. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1325. sizeof(void*));
  1326. login_mtask_alloc_fail:
  1327. kfifo_free(conn->mgmtqueue);
  1328. mgmtqueue_alloc_fail:
  1329. iscsi_destroy_conn(cls_conn);
  1330. return NULL;
  1331. }
  1332. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1333. /**
  1334. * iscsi_conn_teardown - teardown iscsi connection
  1335. * cls_conn: iscsi class connection
  1336. *
  1337. * TODO: we may need to make this into a two step process
  1338. * like scsi-mls remove + put host
  1339. */
  1340. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1341. {
  1342. struct iscsi_conn *conn = cls_conn->dd_data;
  1343. struct iscsi_session *session = conn->session;
  1344. unsigned long flags;
  1345. spin_lock_bh(&session->lock);
  1346. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1347. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1348. if (session->leadconn == conn) {
  1349. /*
  1350. * leading connection? then give up on recovery.
  1351. */
  1352. session->state = ISCSI_STATE_TERMINATE;
  1353. wake_up(&conn->ehwait);
  1354. }
  1355. spin_unlock_bh(&session->lock);
  1356. /*
  1357. * Block until all in-progress commands for this connection
  1358. * time out or fail.
  1359. */
  1360. for (;;) {
  1361. spin_lock_irqsave(session->host->host_lock, flags);
  1362. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1363. spin_unlock_irqrestore(session->host->host_lock, flags);
  1364. break;
  1365. }
  1366. spin_unlock_irqrestore(session->host->host_lock, flags);
  1367. msleep_interruptible(500);
  1368. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1369. "host_failed %d\n", session->host->host_busy,
  1370. session->host->host_failed);
  1371. /*
  1372. * force eh_abort() to unblock
  1373. */
  1374. wake_up(&conn->ehwait);
  1375. }
  1376. /* flush queued up work because we free the connection below */
  1377. scsi_flush_work(session->host);
  1378. spin_lock_bh(&session->lock);
  1379. kfree(conn->data);
  1380. kfree(conn->persistent_address);
  1381. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1382. sizeof(void*));
  1383. if (session->leadconn == conn) {
  1384. session->leadconn = NULL;
  1385. /* no connections exits.. reset sequencing */
  1386. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1387. }
  1388. spin_unlock_bh(&session->lock);
  1389. kfifo_free(conn->mgmtqueue);
  1390. iscsi_destroy_conn(cls_conn);
  1391. }
  1392. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1393. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1394. {
  1395. struct iscsi_conn *conn = cls_conn->dd_data;
  1396. struct iscsi_session *session = conn->session;
  1397. if (!session) {
  1398. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1399. return -EPERM;
  1400. }
  1401. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1402. session->first_burst > session->max_burst) {
  1403. printk("iscsi: invalid burst lengths: "
  1404. "first_burst %d max_burst %d\n",
  1405. session->first_burst, session->max_burst);
  1406. return -EINVAL;
  1407. }
  1408. spin_lock_bh(&session->lock);
  1409. conn->c_stage = ISCSI_CONN_STARTED;
  1410. session->state = ISCSI_STATE_LOGGED_IN;
  1411. switch(conn->stop_stage) {
  1412. case STOP_CONN_RECOVER:
  1413. /*
  1414. * unblock eh_abort() if it is blocked. re-try all
  1415. * commands after successful recovery
  1416. */
  1417. conn->stop_stage = 0;
  1418. conn->tmabort_state = TMABORT_INITIAL;
  1419. session->age++;
  1420. spin_unlock_bh(&session->lock);
  1421. iscsi_unblock_session(session_to_cls(session));
  1422. wake_up(&conn->ehwait);
  1423. return 0;
  1424. case STOP_CONN_TERM:
  1425. conn->stop_stage = 0;
  1426. break;
  1427. default:
  1428. break;
  1429. }
  1430. spin_unlock_bh(&session->lock);
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1434. static void
  1435. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1436. {
  1437. struct iscsi_mgmt_task *mtask, *tmp;
  1438. /* handle pending */
  1439. while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1440. if (mtask == conn->login_mtask)
  1441. continue;
  1442. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1443. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1444. sizeof(void*));
  1445. }
  1446. /* handle running */
  1447. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1448. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1449. list_del(&mtask->running);
  1450. if (mtask == conn->login_mtask)
  1451. continue;
  1452. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1453. sizeof(void*));
  1454. }
  1455. conn->mtask = NULL;
  1456. }
  1457. /* Fail commands. Mutex and session lock held and recv side suspended */
  1458. static void fail_all_commands(struct iscsi_conn *conn)
  1459. {
  1460. struct iscsi_cmd_task *ctask, *tmp;
  1461. /* flush pending */
  1462. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1463. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1464. ctask->itt);
  1465. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1466. }
  1467. /* fail all other running */
  1468. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1469. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1470. ctask->sc, ctask->itt);
  1471. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1472. }
  1473. conn->ctask = NULL;
  1474. }
  1475. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1476. struct iscsi_conn *conn, int flag)
  1477. {
  1478. int old_stop_stage;
  1479. spin_lock_bh(&session->lock);
  1480. if (conn->stop_stage == STOP_CONN_TERM) {
  1481. spin_unlock_bh(&session->lock);
  1482. return;
  1483. }
  1484. /*
  1485. * When this is called for the in_login state, we only want to clean
  1486. * up the login task and connection. We do not need to block and set
  1487. * the recovery state again
  1488. */
  1489. if (flag == STOP_CONN_TERM)
  1490. session->state = ISCSI_STATE_TERMINATE;
  1491. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1492. session->state = ISCSI_STATE_IN_RECOVERY;
  1493. old_stop_stage = conn->stop_stage;
  1494. conn->stop_stage = flag;
  1495. conn->c_stage = ISCSI_CONN_STOPPED;
  1496. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1497. spin_unlock_bh(&session->lock);
  1498. scsi_flush_work(session->host);
  1499. write_lock_bh(conn->recv_lock);
  1500. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1501. write_unlock_bh(conn->recv_lock);
  1502. /*
  1503. * for connection level recovery we should not calculate
  1504. * header digest. conn->hdr_size used for optimization
  1505. * in hdr_extract() and will be re-negotiated at
  1506. * set_param() time.
  1507. */
  1508. if (flag == STOP_CONN_RECOVER) {
  1509. conn->hdrdgst_en = 0;
  1510. conn->datadgst_en = 0;
  1511. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1512. old_stop_stage != STOP_CONN_RECOVER) {
  1513. debug_scsi("blocking session\n");
  1514. iscsi_block_session(session_to_cls(session));
  1515. }
  1516. }
  1517. /*
  1518. * flush queues.
  1519. */
  1520. spin_lock_bh(&session->lock);
  1521. fail_all_commands(conn);
  1522. flush_control_queues(session, conn);
  1523. spin_unlock_bh(&session->lock);
  1524. }
  1525. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1526. {
  1527. struct iscsi_conn *conn = cls_conn->dd_data;
  1528. struct iscsi_session *session = conn->session;
  1529. switch (flag) {
  1530. case STOP_CONN_RECOVER:
  1531. case STOP_CONN_TERM:
  1532. iscsi_start_session_recovery(session, conn, flag);
  1533. break;
  1534. default:
  1535. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1536. }
  1537. }
  1538. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1539. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1540. struct iscsi_cls_conn *cls_conn, int is_leading)
  1541. {
  1542. struct iscsi_session *session = class_to_transport_session(cls_session);
  1543. struct iscsi_conn *conn = cls_conn->dd_data;
  1544. spin_lock_bh(&session->lock);
  1545. if (is_leading)
  1546. session->leadconn = conn;
  1547. spin_unlock_bh(&session->lock);
  1548. /*
  1549. * Unblock xmitworker(), Login Phase will pass through.
  1550. */
  1551. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1552. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1556. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1557. enum iscsi_param param, char *buf, int buflen)
  1558. {
  1559. struct iscsi_conn *conn = cls_conn->dd_data;
  1560. struct iscsi_session *session = conn->session;
  1561. uint32_t value;
  1562. switch(param) {
  1563. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1564. sscanf(buf, "%d", &conn->max_recv_dlength);
  1565. break;
  1566. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1567. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1568. break;
  1569. case ISCSI_PARAM_HDRDGST_EN:
  1570. sscanf(buf, "%d", &conn->hdrdgst_en);
  1571. break;
  1572. case ISCSI_PARAM_DATADGST_EN:
  1573. sscanf(buf, "%d", &conn->datadgst_en);
  1574. break;
  1575. case ISCSI_PARAM_INITIAL_R2T_EN:
  1576. sscanf(buf, "%d", &session->initial_r2t_en);
  1577. break;
  1578. case ISCSI_PARAM_MAX_R2T:
  1579. sscanf(buf, "%d", &session->max_r2t);
  1580. break;
  1581. case ISCSI_PARAM_IMM_DATA_EN:
  1582. sscanf(buf, "%d", &session->imm_data_en);
  1583. break;
  1584. case ISCSI_PARAM_FIRST_BURST:
  1585. sscanf(buf, "%d", &session->first_burst);
  1586. break;
  1587. case ISCSI_PARAM_MAX_BURST:
  1588. sscanf(buf, "%d", &session->max_burst);
  1589. break;
  1590. case ISCSI_PARAM_PDU_INORDER_EN:
  1591. sscanf(buf, "%d", &session->pdu_inorder_en);
  1592. break;
  1593. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1594. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1595. break;
  1596. case ISCSI_PARAM_ERL:
  1597. sscanf(buf, "%d", &session->erl);
  1598. break;
  1599. case ISCSI_PARAM_IFMARKER_EN:
  1600. sscanf(buf, "%d", &value);
  1601. BUG_ON(value);
  1602. break;
  1603. case ISCSI_PARAM_OFMARKER_EN:
  1604. sscanf(buf, "%d", &value);
  1605. BUG_ON(value);
  1606. break;
  1607. case ISCSI_PARAM_EXP_STATSN:
  1608. sscanf(buf, "%u", &conn->exp_statsn);
  1609. break;
  1610. case ISCSI_PARAM_USERNAME:
  1611. kfree(session->username);
  1612. session->username = kstrdup(buf, GFP_KERNEL);
  1613. if (!session->username)
  1614. return -ENOMEM;
  1615. break;
  1616. case ISCSI_PARAM_USERNAME_IN:
  1617. kfree(session->username_in);
  1618. session->username_in = kstrdup(buf, GFP_KERNEL);
  1619. if (!session->username_in)
  1620. return -ENOMEM;
  1621. break;
  1622. case ISCSI_PARAM_PASSWORD:
  1623. kfree(session->password);
  1624. session->password = kstrdup(buf, GFP_KERNEL);
  1625. if (!session->password)
  1626. return -ENOMEM;
  1627. break;
  1628. case ISCSI_PARAM_PASSWORD_IN:
  1629. kfree(session->password_in);
  1630. session->password_in = kstrdup(buf, GFP_KERNEL);
  1631. if (!session->password_in)
  1632. return -ENOMEM;
  1633. break;
  1634. case ISCSI_PARAM_TARGET_NAME:
  1635. /* this should not change between logins */
  1636. if (session->targetname)
  1637. break;
  1638. session->targetname = kstrdup(buf, GFP_KERNEL);
  1639. if (!session->targetname)
  1640. return -ENOMEM;
  1641. break;
  1642. case ISCSI_PARAM_TPGT:
  1643. sscanf(buf, "%d", &session->tpgt);
  1644. break;
  1645. case ISCSI_PARAM_PERSISTENT_PORT:
  1646. sscanf(buf, "%d", &conn->persistent_port);
  1647. break;
  1648. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1649. /*
  1650. * this is the address returned in discovery so it should
  1651. * not change between logins.
  1652. */
  1653. if (conn->persistent_address)
  1654. break;
  1655. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1656. if (!conn->persistent_address)
  1657. return -ENOMEM;
  1658. break;
  1659. default:
  1660. return -ENOSYS;
  1661. }
  1662. return 0;
  1663. }
  1664. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1665. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1666. enum iscsi_param param, char *buf)
  1667. {
  1668. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1669. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1670. int len;
  1671. switch(param) {
  1672. case ISCSI_PARAM_INITIAL_R2T_EN:
  1673. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1674. break;
  1675. case ISCSI_PARAM_MAX_R2T:
  1676. len = sprintf(buf, "%hu\n", session->max_r2t);
  1677. break;
  1678. case ISCSI_PARAM_IMM_DATA_EN:
  1679. len = sprintf(buf, "%d\n", session->imm_data_en);
  1680. break;
  1681. case ISCSI_PARAM_FIRST_BURST:
  1682. len = sprintf(buf, "%u\n", session->first_burst);
  1683. break;
  1684. case ISCSI_PARAM_MAX_BURST:
  1685. len = sprintf(buf, "%u\n", session->max_burst);
  1686. break;
  1687. case ISCSI_PARAM_PDU_INORDER_EN:
  1688. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1689. break;
  1690. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1691. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1692. break;
  1693. case ISCSI_PARAM_ERL:
  1694. len = sprintf(buf, "%d\n", session->erl);
  1695. break;
  1696. case ISCSI_PARAM_TARGET_NAME:
  1697. len = sprintf(buf, "%s\n", session->targetname);
  1698. break;
  1699. case ISCSI_PARAM_TPGT:
  1700. len = sprintf(buf, "%d\n", session->tpgt);
  1701. break;
  1702. case ISCSI_PARAM_USERNAME:
  1703. len = sprintf(buf, "%s\n", session->username);
  1704. break;
  1705. case ISCSI_PARAM_USERNAME_IN:
  1706. len = sprintf(buf, "%s\n", session->username_in);
  1707. break;
  1708. case ISCSI_PARAM_PASSWORD:
  1709. len = sprintf(buf, "%s\n", session->password);
  1710. break;
  1711. case ISCSI_PARAM_PASSWORD_IN:
  1712. len = sprintf(buf, "%s\n", session->password_in);
  1713. break;
  1714. default:
  1715. return -ENOSYS;
  1716. }
  1717. return len;
  1718. }
  1719. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1720. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1721. enum iscsi_param param, char *buf)
  1722. {
  1723. struct iscsi_conn *conn = cls_conn->dd_data;
  1724. int len;
  1725. switch(param) {
  1726. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1727. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1728. break;
  1729. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1730. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1731. break;
  1732. case ISCSI_PARAM_HDRDGST_EN:
  1733. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1734. break;
  1735. case ISCSI_PARAM_DATADGST_EN:
  1736. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1737. break;
  1738. case ISCSI_PARAM_IFMARKER_EN:
  1739. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1740. break;
  1741. case ISCSI_PARAM_OFMARKER_EN:
  1742. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1743. break;
  1744. case ISCSI_PARAM_EXP_STATSN:
  1745. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1746. break;
  1747. case ISCSI_PARAM_PERSISTENT_PORT:
  1748. len = sprintf(buf, "%d\n", conn->persistent_port);
  1749. break;
  1750. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1751. len = sprintf(buf, "%s\n", conn->persistent_address);
  1752. break;
  1753. default:
  1754. return -ENOSYS;
  1755. }
  1756. return len;
  1757. }
  1758. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1759. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1760. char *buf)
  1761. {
  1762. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1763. int len;
  1764. switch (param) {
  1765. case ISCSI_HOST_PARAM_HWADDRESS:
  1766. if (!session->hwaddress)
  1767. len = sprintf(buf, "%s\n", "default");
  1768. else
  1769. len = sprintf(buf, "%s\n", session->hwaddress);
  1770. break;
  1771. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1772. if (!session->initiatorname)
  1773. len = sprintf(buf, "%s\n", "unknown");
  1774. else
  1775. len = sprintf(buf, "%s\n", session->initiatorname);
  1776. break;
  1777. default:
  1778. return -ENOSYS;
  1779. }
  1780. return len;
  1781. }
  1782. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1783. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1784. char *buf, int buflen)
  1785. {
  1786. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1787. switch (param) {
  1788. case ISCSI_HOST_PARAM_HWADDRESS:
  1789. if (!session->hwaddress)
  1790. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1791. break;
  1792. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1793. if (!session->initiatorname)
  1794. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1795. break;
  1796. default:
  1797. return -ENOSYS;
  1798. }
  1799. return 0;
  1800. }
  1801. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1802. MODULE_AUTHOR("Mike Christie");
  1803. MODULE_DESCRIPTION("iSCSI library functions");
  1804. MODULE_LICENSE("GPL");