libiscsi.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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(scsi_bufflen(sc));
  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 (scsi_bufflen(sc) >= session->first_burst)
  160. ctask->imm_count = min(session->first_burst,
  161. conn->max_xmit_dlength);
  162. else
  163. ctask->imm_count = min(scsi_bufflen(sc),
  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. (scsi_bufflen(sc))) - 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, scsi_bufflen(sc),
  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 <= scsi_bufflen(sc))
  265. scsi_set_resid(sc, 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. scsi_set_resid(sc, 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. scsi_set_resid(sc, scsi_bufflen(sc));
  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. scsi_set_resid(sc, scsi_bufflen(sc));
  1003. if (conn->ctask == ctask)
  1004. conn->ctask = NULL;
  1005. /* release ref from queuecommand */
  1006. __iscsi_put_ctask(ctask);
  1007. }
  1008. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1009. {
  1010. struct iscsi_cmd_task *ctask;
  1011. struct iscsi_conn *conn;
  1012. struct iscsi_session *session;
  1013. int rc;
  1014. /*
  1015. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1016. * got the command.
  1017. */
  1018. if (!sc->SCp.ptr) {
  1019. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1020. return SUCCESS;
  1021. }
  1022. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1023. conn = ctask->conn;
  1024. session = conn->session;
  1025. conn->eh_abort_cnt++;
  1026. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1027. spin_lock_bh(&session->lock);
  1028. /*
  1029. * If we are not logged in or we have started a new session
  1030. * then let the host reset code handle this
  1031. */
  1032. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1033. sc->SCp.phase != session->age)
  1034. goto failed;
  1035. /* ctask completed before time out */
  1036. if (!ctask->sc) {
  1037. debug_scsi("sc completed while abort in progress\n");
  1038. goto success;
  1039. }
  1040. /* what should we do here ? */
  1041. if (conn->ctask == ctask) {
  1042. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1043. "Failing abort\n", sc, ctask->itt);
  1044. goto failed;
  1045. }
  1046. if (ctask->state == ISCSI_TASK_PENDING) {
  1047. fail_command(conn, ctask, DID_ABORT << 16);
  1048. goto success;
  1049. }
  1050. conn->tmabort_state = TMABORT_INITIAL;
  1051. rc = iscsi_exec_abort_task(sc, ctask);
  1052. if (rc || sc->SCp.phase != session->age ||
  1053. session->state != ISCSI_STATE_LOGGED_IN)
  1054. goto failed;
  1055. iscsi_ctask_mtask_cleanup(ctask);
  1056. switch (conn->tmabort_state) {
  1057. case TMABORT_SUCCESS:
  1058. spin_unlock_bh(&session->lock);
  1059. /*
  1060. * clean up task if aborted. grab the recv lock as a writer
  1061. */
  1062. write_lock_bh(conn->recv_lock);
  1063. spin_lock(&session->lock);
  1064. fail_command(conn, ctask, DID_ABORT << 16);
  1065. spin_unlock(&session->lock);
  1066. write_unlock_bh(conn->recv_lock);
  1067. /*
  1068. * make sure xmit thread is not still touching the
  1069. * ctask/scsi_cmnd
  1070. */
  1071. scsi_flush_work(session->host);
  1072. goto success_unlocked;
  1073. case TMABORT_NOT_FOUND:
  1074. if (!ctask->sc) {
  1075. /* ctask completed before tmf abort response */
  1076. debug_scsi("sc completed while abort in progress\n");
  1077. goto success;
  1078. }
  1079. /* fall through */
  1080. default:
  1081. /* timedout or failed */
  1082. spin_unlock_bh(&session->lock);
  1083. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1084. goto failed_unlocked;
  1085. }
  1086. success:
  1087. spin_unlock_bh(&session->lock);
  1088. success_unlocked:
  1089. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1090. return SUCCESS;
  1091. failed:
  1092. spin_unlock_bh(&session->lock);
  1093. failed_unlocked:
  1094. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1095. return FAILED;
  1096. }
  1097. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1098. int
  1099. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1100. {
  1101. int i;
  1102. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1103. if (*items == NULL)
  1104. return -ENOMEM;
  1105. q->max = max;
  1106. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1107. if (q->pool == NULL) {
  1108. kfree(*items);
  1109. return -ENOMEM;
  1110. }
  1111. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1112. GFP_KERNEL, NULL);
  1113. if (q->queue == ERR_PTR(-ENOMEM)) {
  1114. kfree(q->pool);
  1115. kfree(*items);
  1116. return -ENOMEM;
  1117. }
  1118. for (i = 0; i < max; i++) {
  1119. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1120. if (q->pool[i] == NULL) {
  1121. int j;
  1122. for (j = 0; j < i; j++)
  1123. kfree(q->pool[j]);
  1124. kfifo_free(q->queue);
  1125. kfree(q->pool);
  1126. kfree(*items);
  1127. return -ENOMEM;
  1128. }
  1129. memset(q->pool[i], 0, item_size);
  1130. (*items)[i] = q->pool[i];
  1131. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1132. }
  1133. return 0;
  1134. }
  1135. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1136. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1137. {
  1138. int i;
  1139. for (i = 0; i < q->max; i++)
  1140. kfree(items[i]);
  1141. kfree(q->pool);
  1142. kfree(items);
  1143. }
  1144. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1145. /*
  1146. * iSCSI Session's hostdata organization:
  1147. *
  1148. * *------------------* <== hostdata_session(host->hostdata)
  1149. * | ptr to class sess|
  1150. * |------------------| <== iscsi_hostdata(host->hostdata)
  1151. * | iscsi_session |
  1152. * *------------------*
  1153. */
  1154. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1155. _sz % sizeof(unsigned long))
  1156. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1157. /**
  1158. * iscsi_session_setup - create iscsi cls session and host and session
  1159. * @scsit: scsi transport template
  1160. * @iscsit: iscsi transport template
  1161. * @cmds_max: scsi host can queue
  1162. * @qdepth: scsi host cmds per lun
  1163. * @cmd_task_size: LLD ctask private data size
  1164. * @mgmt_task_size: LLD mtask private data size
  1165. * @initial_cmdsn: initial CmdSN
  1166. * @hostno: host no allocated
  1167. *
  1168. * This can be used by software iscsi_transports that allocate
  1169. * a session per scsi host.
  1170. **/
  1171. struct iscsi_cls_session *
  1172. iscsi_session_setup(struct iscsi_transport *iscsit,
  1173. struct scsi_transport_template *scsit,
  1174. uint16_t cmds_max, uint16_t qdepth,
  1175. int cmd_task_size, int mgmt_task_size,
  1176. uint32_t initial_cmdsn, uint32_t *hostno)
  1177. {
  1178. struct Scsi_Host *shost;
  1179. struct iscsi_session *session;
  1180. struct iscsi_cls_session *cls_session;
  1181. int cmd_i;
  1182. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1183. if (qdepth != 0)
  1184. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1185. "Queue depth must be between 1 and %d.\n",
  1186. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1187. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1188. }
  1189. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1190. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1191. if (cmds_max != 0)
  1192. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1193. "can_queue must be a power of 2 and between "
  1194. "2 and %d - setting to %d.\n", cmds_max,
  1195. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1196. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1197. }
  1198. shost = scsi_host_alloc(iscsit->host_template,
  1199. hostdata_privsize(sizeof(*session)));
  1200. if (!shost)
  1201. return NULL;
  1202. /* the iscsi layer takes one task for reserve */
  1203. shost->can_queue = cmds_max - 1;
  1204. shost->cmd_per_lun = qdepth;
  1205. shost->max_id = 1;
  1206. shost->max_channel = 0;
  1207. shost->max_lun = iscsit->max_lun;
  1208. shost->max_cmd_len = iscsit->max_cmd_len;
  1209. shost->transportt = scsit;
  1210. shost->transportt->create_work_queue = 1;
  1211. *hostno = shost->host_no;
  1212. session = iscsi_hostdata(shost->hostdata);
  1213. memset(session, 0, sizeof(struct iscsi_session));
  1214. session->host = shost;
  1215. session->state = ISCSI_STATE_FREE;
  1216. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1217. session->cmds_max = cmds_max;
  1218. session->cmdsn = initial_cmdsn;
  1219. session->exp_cmdsn = initial_cmdsn + 1;
  1220. session->max_cmdsn = initial_cmdsn + 1;
  1221. session->max_r2t = 1;
  1222. session->tt = iscsit;
  1223. /* initialize SCSI PDU commands pool */
  1224. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1225. (void***)&session->cmds,
  1226. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1227. goto cmdpool_alloc_fail;
  1228. /* pre-format cmds pool with ITT */
  1229. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1230. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1231. if (cmd_task_size)
  1232. ctask->dd_data = &ctask[1];
  1233. ctask->itt = cmd_i;
  1234. INIT_LIST_HEAD(&ctask->running);
  1235. }
  1236. spin_lock_init(&session->lock);
  1237. /* initialize immediate command pool */
  1238. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1239. (void***)&session->mgmt_cmds,
  1240. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1241. goto mgmtpool_alloc_fail;
  1242. /* pre-format immediate cmds pool with ITT */
  1243. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1244. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1245. if (mgmt_task_size)
  1246. mtask->dd_data = &mtask[1];
  1247. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1248. INIT_LIST_HEAD(&mtask->running);
  1249. }
  1250. if (scsi_add_host(shost, NULL))
  1251. goto add_host_fail;
  1252. if (!try_module_get(iscsit->owner))
  1253. goto cls_session_fail;
  1254. cls_session = iscsi_create_session(shost, iscsit, 0);
  1255. if (!cls_session)
  1256. goto module_put;
  1257. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1258. return cls_session;
  1259. module_put:
  1260. module_put(iscsit->owner);
  1261. cls_session_fail:
  1262. scsi_remove_host(shost);
  1263. add_host_fail:
  1264. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1265. mgmtpool_alloc_fail:
  1266. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1267. cmdpool_alloc_fail:
  1268. scsi_host_put(shost);
  1269. return NULL;
  1270. }
  1271. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1272. /**
  1273. * iscsi_session_teardown - destroy session, host, and cls_session
  1274. * shost: scsi host
  1275. *
  1276. * This can be used by software iscsi_transports that allocate
  1277. * a session per scsi host.
  1278. **/
  1279. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1280. {
  1281. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1282. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1283. struct module *owner = cls_session->transport->owner;
  1284. scsi_remove_host(shost);
  1285. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1286. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1287. kfree(session->password);
  1288. kfree(session->password_in);
  1289. kfree(session->username);
  1290. kfree(session->username_in);
  1291. kfree(session->targetname);
  1292. kfree(session->netdev);
  1293. kfree(session->hwaddress);
  1294. kfree(session->initiatorname);
  1295. iscsi_destroy_session(cls_session);
  1296. scsi_host_put(shost);
  1297. module_put(owner);
  1298. }
  1299. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1300. /**
  1301. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1302. * @cls_session: iscsi_cls_session
  1303. * @conn_idx: cid
  1304. **/
  1305. struct iscsi_cls_conn *
  1306. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1307. {
  1308. struct iscsi_session *session = class_to_transport_session(cls_session);
  1309. struct iscsi_conn *conn;
  1310. struct iscsi_cls_conn *cls_conn;
  1311. char *data;
  1312. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1313. if (!cls_conn)
  1314. return NULL;
  1315. conn = cls_conn->dd_data;
  1316. memset(conn, 0, sizeof(*conn));
  1317. conn->session = session;
  1318. conn->cls_conn = cls_conn;
  1319. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1320. conn->id = conn_idx;
  1321. conn->exp_statsn = 0;
  1322. conn->tmabort_state = TMABORT_INITIAL;
  1323. INIT_LIST_HEAD(&conn->run_list);
  1324. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1325. INIT_LIST_HEAD(&conn->xmitqueue);
  1326. /* initialize general immediate & non-immediate PDU commands queue */
  1327. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1328. GFP_KERNEL, NULL);
  1329. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1330. goto mgmtqueue_alloc_fail;
  1331. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1332. /* allocate login_mtask used for the login/text sequences */
  1333. spin_lock_bh(&session->lock);
  1334. if (!__kfifo_get(session->mgmtpool.queue,
  1335. (void*)&conn->login_mtask,
  1336. sizeof(void*))) {
  1337. spin_unlock_bh(&session->lock);
  1338. goto login_mtask_alloc_fail;
  1339. }
  1340. spin_unlock_bh(&session->lock);
  1341. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1342. if (!data)
  1343. goto login_mtask_data_alloc_fail;
  1344. conn->login_mtask->data = conn->data = data;
  1345. init_timer(&conn->tmabort_timer);
  1346. init_waitqueue_head(&conn->ehwait);
  1347. return cls_conn;
  1348. login_mtask_data_alloc_fail:
  1349. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1350. sizeof(void*));
  1351. login_mtask_alloc_fail:
  1352. kfifo_free(conn->mgmtqueue);
  1353. mgmtqueue_alloc_fail:
  1354. iscsi_destroy_conn(cls_conn);
  1355. return NULL;
  1356. }
  1357. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1358. /**
  1359. * iscsi_conn_teardown - teardown iscsi connection
  1360. * cls_conn: iscsi class connection
  1361. *
  1362. * TODO: we may need to make this into a two step process
  1363. * like scsi-mls remove + put host
  1364. */
  1365. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1366. {
  1367. struct iscsi_conn *conn = cls_conn->dd_data;
  1368. struct iscsi_session *session = conn->session;
  1369. unsigned long flags;
  1370. spin_lock_bh(&session->lock);
  1371. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1372. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1373. if (session->leadconn == conn) {
  1374. /*
  1375. * leading connection? then give up on recovery.
  1376. */
  1377. session->state = ISCSI_STATE_TERMINATE;
  1378. wake_up(&conn->ehwait);
  1379. }
  1380. spin_unlock_bh(&session->lock);
  1381. /*
  1382. * Block until all in-progress commands for this connection
  1383. * time out or fail.
  1384. */
  1385. for (;;) {
  1386. spin_lock_irqsave(session->host->host_lock, flags);
  1387. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1388. spin_unlock_irqrestore(session->host->host_lock, flags);
  1389. break;
  1390. }
  1391. spin_unlock_irqrestore(session->host->host_lock, flags);
  1392. msleep_interruptible(500);
  1393. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1394. "host_failed %d\n", session->host->host_busy,
  1395. session->host->host_failed);
  1396. /*
  1397. * force eh_abort() to unblock
  1398. */
  1399. wake_up(&conn->ehwait);
  1400. }
  1401. /* flush queued up work because we free the connection below */
  1402. scsi_flush_work(session->host);
  1403. spin_lock_bh(&session->lock);
  1404. kfree(conn->data);
  1405. kfree(conn->persistent_address);
  1406. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1407. sizeof(void*));
  1408. if (session->leadconn == conn) {
  1409. session->leadconn = NULL;
  1410. /* no connections exits.. reset sequencing */
  1411. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1412. }
  1413. spin_unlock_bh(&session->lock);
  1414. kfifo_free(conn->mgmtqueue);
  1415. iscsi_destroy_conn(cls_conn);
  1416. }
  1417. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1418. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1419. {
  1420. struct iscsi_conn *conn = cls_conn->dd_data;
  1421. struct iscsi_session *session = conn->session;
  1422. if (!session) {
  1423. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1424. return -EPERM;
  1425. }
  1426. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1427. session->first_burst > session->max_burst) {
  1428. printk("iscsi: invalid burst lengths: "
  1429. "first_burst %d max_burst %d\n",
  1430. session->first_burst, session->max_burst);
  1431. return -EINVAL;
  1432. }
  1433. spin_lock_bh(&session->lock);
  1434. conn->c_stage = ISCSI_CONN_STARTED;
  1435. session->state = ISCSI_STATE_LOGGED_IN;
  1436. switch(conn->stop_stage) {
  1437. case STOP_CONN_RECOVER:
  1438. /*
  1439. * unblock eh_abort() if it is blocked. re-try all
  1440. * commands after successful recovery
  1441. */
  1442. conn->stop_stage = 0;
  1443. conn->tmabort_state = TMABORT_INITIAL;
  1444. session->age++;
  1445. spin_unlock_bh(&session->lock);
  1446. iscsi_unblock_session(session_to_cls(session));
  1447. wake_up(&conn->ehwait);
  1448. return 0;
  1449. case STOP_CONN_TERM:
  1450. conn->stop_stage = 0;
  1451. break;
  1452. default:
  1453. break;
  1454. }
  1455. spin_unlock_bh(&session->lock);
  1456. return 0;
  1457. }
  1458. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1459. static void
  1460. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1461. {
  1462. struct iscsi_mgmt_task *mtask, *tmp;
  1463. /* handle pending */
  1464. while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1465. if (mtask == conn->login_mtask)
  1466. continue;
  1467. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1468. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1469. sizeof(void*));
  1470. }
  1471. /* handle running */
  1472. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1473. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1474. list_del(&mtask->running);
  1475. if (mtask == conn->login_mtask)
  1476. continue;
  1477. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1478. sizeof(void*));
  1479. }
  1480. conn->mtask = NULL;
  1481. }
  1482. /* Fail commands. Mutex and session lock held and recv side suspended */
  1483. static void fail_all_commands(struct iscsi_conn *conn)
  1484. {
  1485. struct iscsi_cmd_task *ctask, *tmp;
  1486. /* flush pending */
  1487. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1488. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1489. ctask->itt);
  1490. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1491. }
  1492. /* fail all other running */
  1493. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1494. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1495. ctask->sc, ctask->itt);
  1496. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1497. }
  1498. conn->ctask = NULL;
  1499. }
  1500. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1501. struct iscsi_conn *conn, int flag)
  1502. {
  1503. int old_stop_stage;
  1504. spin_lock_bh(&session->lock);
  1505. if (conn->stop_stage == STOP_CONN_TERM) {
  1506. spin_unlock_bh(&session->lock);
  1507. return;
  1508. }
  1509. /*
  1510. * When this is called for the in_login state, we only want to clean
  1511. * up the login task and connection. We do not need to block and set
  1512. * the recovery state again
  1513. */
  1514. if (flag == STOP_CONN_TERM)
  1515. session->state = ISCSI_STATE_TERMINATE;
  1516. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1517. session->state = ISCSI_STATE_IN_RECOVERY;
  1518. old_stop_stage = conn->stop_stage;
  1519. conn->stop_stage = flag;
  1520. conn->c_stage = ISCSI_CONN_STOPPED;
  1521. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1522. spin_unlock_bh(&session->lock);
  1523. scsi_flush_work(session->host);
  1524. write_lock_bh(conn->recv_lock);
  1525. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1526. write_unlock_bh(conn->recv_lock);
  1527. /*
  1528. * for connection level recovery we should not calculate
  1529. * header digest. conn->hdr_size used for optimization
  1530. * in hdr_extract() and will be re-negotiated at
  1531. * set_param() time.
  1532. */
  1533. if (flag == STOP_CONN_RECOVER) {
  1534. conn->hdrdgst_en = 0;
  1535. conn->datadgst_en = 0;
  1536. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1537. old_stop_stage != STOP_CONN_RECOVER) {
  1538. debug_scsi("blocking session\n");
  1539. iscsi_block_session(session_to_cls(session));
  1540. }
  1541. }
  1542. /*
  1543. * flush queues.
  1544. */
  1545. spin_lock_bh(&session->lock);
  1546. fail_all_commands(conn);
  1547. flush_control_queues(session, conn);
  1548. spin_unlock_bh(&session->lock);
  1549. }
  1550. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1551. {
  1552. struct iscsi_conn *conn = cls_conn->dd_data;
  1553. struct iscsi_session *session = conn->session;
  1554. switch (flag) {
  1555. case STOP_CONN_RECOVER:
  1556. case STOP_CONN_TERM:
  1557. iscsi_start_session_recovery(session, conn, flag);
  1558. break;
  1559. default:
  1560. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1561. }
  1562. }
  1563. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1564. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1565. struct iscsi_cls_conn *cls_conn, int is_leading)
  1566. {
  1567. struct iscsi_session *session = class_to_transport_session(cls_session);
  1568. struct iscsi_conn *conn = cls_conn->dd_data;
  1569. spin_lock_bh(&session->lock);
  1570. if (is_leading)
  1571. session->leadconn = conn;
  1572. spin_unlock_bh(&session->lock);
  1573. /*
  1574. * Unblock xmitworker(), Login Phase will pass through.
  1575. */
  1576. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1577. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1578. return 0;
  1579. }
  1580. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1581. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1582. enum iscsi_param param, char *buf, int buflen)
  1583. {
  1584. struct iscsi_conn *conn = cls_conn->dd_data;
  1585. struct iscsi_session *session = conn->session;
  1586. uint32_t value;
  1587. switch(param) {
  1588. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1589. sscanf(buf, "%d", &conn->max_recv_dlength);
  1590. break;
  1591. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1592. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1593. break;
  1594. case ISCSI_PARAM_HDRDGST_EN:
  1595. sscanf(buf, "%d", &conn->hdrdgst_en);
  1596. break;
  1597. case ISCSI_PARAM_DATADGST_EN:
  1598. sscanf(buf, "%d", &conn->datadgst_en);
  1599. break;
  1600. case ISCSI_PARAM_INITIAL_R2T_EN:
  1601. sscanf(buf, "%d", &session->initial_r2t_en);
  1602. break;
  1603. case ISCSI_PARAM_MAX_R2T:
  1604. sscanf(buf, "%d", &session->max_r2t);
  1605. break;
  1606. case ISCSI_PARAM_IMM_DATA_EN:
  1607. sscanf(buf, "%d", &session->imm_data_en);
  1608. break;
  1609. case ISCSI_PARAM_FIRST_BURST:
  1610. sscanf(buf, "%d", &session->first_burst);
  1611. break;
  1612. case ISCSI_PARAM_MAX_BURST:
  1613. sscanf(buf, "%d", &session->max_burst);
  1614. break;
  1615. case ISCSI_PARAM_PDU_INORDER_EN:
  1616. sscanf(buf, "%d", &session->pdu_inorder_en);
  1617. break;
  1618. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1619. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1620. break;
  1621. case ISCSI_PARAM_ERL:
  1622. sscanf(buf, "%d", &session->erl);
  1623. break;
  1624. case ISCSI_PARAM_IFMARKER_EN:
  1625. sscanf(buf, "%d", &value);
  1626. BUG_ON(value);
  1627. break;
  1628. case ISCSI_PARAM_OFMARKER_EN:
  1629. sscanf(buf, "%d", &value);
  1630. BUG_ON(value);
  1631. break;
  1632. case ISCSI_PARAM_EXP_STATSN:
  1633. sscanf(buf, "%u", &conn->exp_statsn);
  1634. break;
  1635. case ISCSI_PARAM_USERNAME:
  1636. kfree(session->username);
  1637. session->username = kstrdup(buf, GFP_KERNEL);
  1638. if (!session->username)
  1639. return -ENOMEM;
  1640. break;
  1641. case ISCSI_PARAM_USERNAME_IN:
  1642. kfree(session->username_in);
  1643. session->username_in = kstrdup(buf, GFP_KERNEL);
  1644. if (!session->username_in)
  1645. return -ENOMEM;
  1646. break;
  1647. case ISCSI_PARAM_PASSWORD:
  1648. kfree(session->password);
  1649. session->password = kstrdup(buf, GFP_KERNEL);
  1650. if (!session->password)
  1651. return -ENOMEM;
  1652. break;
  1653. case ISCSI_PARAM_PASSWORD_IN:
  1654. kfree(session->password_in);
  1655. session->password_in = kstrdup(buf, GFP_KERNEL);
  1656. if (!session->password_in)
  1657. return -ENOMEM;
  1658. break;
  1659. case ISCSI_PARAM_TARGET_NAME:
  1660. /* this should not change between logins */
  1661. if (session->targetname)
  1662. break;
  1663. session->targetname = kstrdup(buf, GFP_KERNEL);
  1664. if (!session->targetname)
  1665. return -ENOMEM;
  1666. break;
  1667. case ISCSI_PARAM_TPGT:
  1668. sscanf(buf, "%d", &session->tpgt);
  1669. break;
  1670. case ISCSI_PARAM_PERSISTENT_PORT:
  1671. sscanf(buf, "%d", &conn->persistent_port);
  1672. break;
  1673. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1674. /*
  1675. * this is the address returned in discovery so it should
  1676. * not change between logins.
  1677. */
  1678. if (conn->persistent_address)
  1679. break;
  1680. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1681. if (!conn->persistent_address)
  1682. return -ENOMEM;
  1683. break;
  1684. default:
  1685. return -ENOSYS;
  1686. }
  1687. return 0;
  1688. }
  1689. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1690. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1691. enum iscsi_param param, char *buf)
  1692. {
  1693. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1694. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1695. int len;
  1696. switch(param) {
  1697. case ISCSI_PARAM_INITIAL_R2T_EN:
  1698. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1699. break;
  1700. case ISCSI_PARAM_MAX_R2T:
  1701. len = sprintf(buf, "%hu\n", session->max_r2t);
  1702. break;
  1703. case ISCSI_PARAM_IMM_DATA_EN:
  1704. len = sprintf(buf, "%d\n", session->imm_data_en);
  1705. break;
  1706. case ISCSI_PARAM_FIRST_BURST:
  1707. len = sprintf(buf, "%u\n", session->first_burst);
  1708. break;
  1709. case ISCSI_PARAM_MAX_BURST:
  1710. len = sprintf(buf, "%u\n", session->max_burst);
  1711. break;
  1712. case ISCSI_PARAM_PDU_INORDER_EN:
  1713. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1714. break;
  1715. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1716. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1717. break;
  1718. case ISCSI_PARAM_ERL:
  1719. len = sprintf(buf, "%d\n", session->erl);
  1720. break;
  1721. case ISCSI_PARAM_TARGET_NAME:
  1722. len = sprintf(buf, "%s\n", session->targetname);
  1723. break;
  1724. case ISCSI_PARAM_TPGT:
  1725. len = sprintf(buf, "%d\n", session->tpgt);
  1726. break;
  1727. case ISCSI_PARAM_USERNAME:
  1728. len = sprintf(buf, "%s\n", session->username);
  1729. break;
  1730. case ISCSI_PARAM_USERNAME_IN:
  1731. len = sprintf(buf, "%s\n", session->username_in);
  1732. break;
  1733. case ISCSI_PARAM_PASSWORD:
  1734. len = sprintf(buf, "%s\n", session->password);
  1735. break;
  1736. case ISCSI_PARAM_PASSWORD_IN:
  1737. len = sprintf(buf, "%s\n", session->password_in);
  1738. break;
  1739. default:
  1740. return -ENOSYS;
  1741. }
  1742. return len;
  1743. }
  1744. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1745. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1746. enum iscsi_param param, char *buf)
  1747. {
  1748. struct iscsi_conn *conn = cls_conn->dd_data;
  1749. int len;
  1750. switch(param) {
  1751. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1752. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1753. break;
  1754. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1755. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1756. break;
  1757. case ISCSI_PARAM_HDRDGST_EN:
  1758. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1759. break;
  1760. case ISCSI_PARAM_DATADGST_EN:
  1761. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1762. break;
  1763. case ISCSI_PARAM_IFMARKER_EN:
  1764. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1765. break;
  1766. case ISCSI_PARAM_OFMARKER_EN:
  1767. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1768. break;
  1769. case ISCSI_PARAM_EXP_STATSN:
  1770. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1771. break;
  1772. case ISCSI_PARAM_PERSISTENT_PORT:
  1773. len = sprintf(buf, "%d\n", conn->persistent_port);
  1774. break;
  1775. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1776. len = sprintf(buf, "%s\n", conn->persistent_address);
  1777. break;
  1778. default:
  1779. return -ENOSYS;
  1780. }
  1781. return len;
  1782. }
  1783. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1784. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1785. char *buf)
  1786. {
  1787. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1788. int len;
  1789. switch (param) {
  1790. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1791. if (!session->netdev)
  1792. len = sprintf(buf, "%s\n", "default");
  1793. else
  1794. len = sprintf(buf, "%s\n", session->netdev);
  1795. break;
  1796. case ISCSI_HOST_PARAM_HWADDRESS:
  1797. if (!session->hwaddress)
  1798. len = sprintf(buf, "%s\n", "default");
  1799. else
  1800. len = sprintf(buf, "%s\n", session->hwaddress);
  1801. break;
  1802. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1803. if (!session->initiatorname)
  1804. len = sprintf(buf, "%s\n", "unknown");
  1805. else
  1806. len = sprintf(buf, "%s\n", session->initiatorname);
  1807. break;
  1808. default:
  1809. return -ENOSYS;
  1810. }
  1811. return len;
  1812. }
  1813. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1814. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1815. char *buf, int buflen)
  1816. {
  1817. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1818. switch (param) {
  1819. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1820. if (!session->netdev)
  1821. session->netdev = kstrdup(buf, GFP_KERNEL);
  1822. break;
  1823. case ISCSI_HOST_PARAM_HWADDRESS:
  1824. if (!session->hwaddress)
  1825. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1826. break;
  1827. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1828. if (!session->initiatorname)
  1829. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1830. break;
  1831. default:
  1832. return -ENOSYS;
  1833. }
  1834. return 0;
  1835. }
  1836. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1837. MODULE_AUTHOR("Mike Christie");
  1838. MODULE_DESCRIPTION("iSCSI library functions");
  1839. MODULE_LICENSE("GPL");