zfcp_scsi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * zfcp device driver
  3. *
  4. * Interface to Linux SCSI midlayer.
  5. *
  6. * Copyright IBM Corporation 2002, 2008
  7. */
  8. #include "zfcp_ext.h"
  9. #include <asm/atomic.h>
  10. /* Find start of Sense Information in FCP response unit*/
  11. char *zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  12. {
  13. char *fcp_sns_info_ptr;
  14. fcp_sns_info_ptr = (unsigned char *) &fcp_rsp_iu[1];
  15. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  16. fcp_sns_info_ptr += fcp_rsp_iu->fcp_rsp_len;
  17. return fcp_sns_info_ptr;
  18. }
  19. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  20. {
  21. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  22. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  23. unit->device = NULL;
  24. zfcp_erp_unit_failed(unit, 12, NULL);
  25. zfcp_unit_put(unit);
  26. }
  27. static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
  28. {
  29. if (sdp->tagged_supported)
  30. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, 32);
  31. else
  32. scsi_adjust_queue_depth(sdp, 0, 1);
  33. return 0;
  34. }
  35. static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  36. {
  37. set_host_byte(scpnt, result);
  38. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  39. zfcp_scsi_dbf_event_result("fail", 4,
  40. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  41. scpnt, NULL);
  42. /* return directly */
  43. scpnt->scsi_done(scpnt);
  44. }
  45. static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  46. void (*done) (struct scsi_cmnd *))
  47. {
  48. struct zfcp_unit *unit;
  49. struct zfcp_adapter *adapter;
  50. int status;
  51. int ret;
  52. /* reset the status for this request */
  53. scpnt->result = 0;
  54. scpnt->host_scribble = NULL;
  55. scpnt->scsi_done = done;
  56. /*
  57. * figure out adapter and target device
  58. * (stored there by zfcp_scsi_slave_alloc)
  59. */
  60. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  61. unit = scpnt->device->hostdata;
  62. BUG_ON(!adapter || (adapter != unit->port->adapter));
  63. BUG_ON(!scpnt->scsi_done);
  64. if (unlikely(!unit)) {
  65. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  66. return 0;
  67. }
  68. status = atomic_read(&unit->status);
  69. if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
  70. !(status & ZFCP_STATUS_COMMON_RUNNING))) {
  71. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  72. return 0;;
  73. }
  74. ret = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, 0,
  75. ZFCP_REQ_AUTO_CLEANUP);
  76. if (unlikely(ret == -EBUSY))
  77. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  78. else if (unlikely(ret < 0))
  79. return SCSI_MLQUEUE_HOST_BUSY;
  80. return ret;
  81. }
  82. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
  83. int channel, unsigned int id,
  84. unsigned int lun)
  85. {
  86. struct zfcp_port *port;
  87. struct zfcp_unit *unit;
  88. int scsi_lun;
  89. list_for_each_entry(port, &adapter->port_list_head, list) {
  90. if (!port->rport || (id != port->rport->scsi_target_id))
  91. continue;
  92. list_for_each_entry(unit, &port->unit_list_head, list) {
  93. scsi_lun = scsilun_to_int(
  94. (struct scsi_lun *)&unit->fcp_lun);
  95. if (lun == scsi_lun)
  96. return unit;
  97. }
  98. }
  99. return NULL;
  100. }
  101. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  102. {
  103. struct zfcp_adapter *adapter;
  104. struct zfcp_unit *unit;
  105. unsigned long flags;
  106. int retval = -ENXIO;
  107. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  108. if (!adapter)
  109. goto out;
  110. read_lock_irqsave(&zfcp_data.config_lock, flags);
  111. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  112. if (unit &&
  113. (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_REGISTERED)) {
  114. sdp->hostdata = unit;
  115. unit->device = sdp;
  116. zfcp_unit_get(unit);
  117. retval = 0;
  118. }
  119. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  120. out:
  121. return retval;
  122. }
  123. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  124. {
  125. struct Scsi_Host *scsi_host;
  126. struct zfcp_adapter *adapter;
  127. struct zfcp_unit *unit;
  128. struct zfcp_fsf_req *fsf_req;
  129. unsigned long flags;
  130. unsigned long old_req_id = (unsigned long) scpnt->host_scribble;
  131. int retval = SUCCESS;
  132. scsi_host = scpnt->device->host;
  133. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  134. unit = scpnt->device->hostdata;
  135. /* avoid race condition between late normal completion and abort */
  136. write_lock_irqsave(&adapter->abort_lock, flags);
  137. /* Check whether corresponding fsf_req is still pending */
  138. spin_lock(&adapter->req_list_lock);
  139. fsf_req = zfcp_reqlist_find(adapter, old_req_id);
  140. spin_unlock(&adapter->req_list_lock);
  141. if (!fsf_req) {
  142. write_unlock_irqrestore(&adapter->abort_lock, flags);
  143. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  144. return retval;
  145. }
  146. fsf_req->data = NULL;
  147. /* don't access old fsf_req after releasing the abort_lock */
  148. write_unlock_irqrestore(&adapter->abort_lock, flags);
  149. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  150. if (!fsf_req) {
  151. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  152. old_req_id);
  153. retval = FAILED;
  154. return retval;
  155. }
  156. __wait_event(fsf_req->completion_wq,
  157. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  158. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  159. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  160. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  161. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  162. } else {
  163. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  164. retval = FAILED;
  165. }
  166. zfcp_fsf_req_free(fsf_req);
  167. return retval;
  168. }
  169. static int zfcp_task_mgmt_function(struct zfcp_unit *unit, u8 tm_flags,
  170. struct scsi_cmnd *scpnt)
  171. {
  172. struct zfcp_adapter *adapter = unit->port->adapter;
  173. struct zfcp_fsf_req *fsf_req;
  174. int retval = SUCCESS;
  175. /* issue task management function */
  176. fsf_req = zfcp_fsf_send_fcp_ctm(adapter, unit, tm_flags, 0);
  177. if (!fsf_req) {
  178. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  179. return FAILED;
  180. }
  181. __wait_event(fsf_req->completion_wq,
  182. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  183. /*
  184. * check completion status of task management function
  185. */
  186. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  187. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  188. retval = FAILED;
  189. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  190. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  191. retval = FAILED;
  192. } else
  193. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  194. zfcp_fsf_req_free(fsf_req);
  195. return retval;
  196. }
  197. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  198. {
  199. struct zfcp_unit *unit = scpnt->device->hostdata;
  200. if (!unit) {
  201. WARN_ON(1);
  202. return SUCCESS;
  203. }
  204. return zfcp_task_mgmt_function(unit, FCP_LOGICAL_UNIT_RESET, scpnt);
  205. }
  206. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  207. {
  208. struct zfcp_unit *unit = scpnt->device->hostdata;
  209. if (!unit) {
  210. WARN_ON(1);
  211. return SUCCESS;
  212. }
  213. return zfcp_task_mgmt_function(unit, FCP_TARGET_RESET, scpnt);
  214. }
  215. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  216. {
  217. struct zfcp_unit *unit;
  218. struct zfcp_adapter *adapter;
  219. unit = scpnt->device->hostdata;
  220. adapter = unit->port->adapter;
  221. zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
  222. zfcp_erp_wait(adapter);
  223. return SUCCESS;
  224. }
  225. int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  226. {
  227. struct ccw_dev_id dev_id;
  228. if (adapter->scsi_host)
  229. return 0;
  230. ccw_device_get_id(adapter->ccw_device, &dev_id);
  231. /* register adapter as SCSI host with mid layer of SCSI stack */
  232. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  233. sizeof (struct zfcp_adapter *));
  234. if (!adapter->scsi_host) {
  235. dev_err(&adapter->ccw_device->dev,
  236. "Registering the FCP device with the "
  237. "SCSI stack failed\n");
  238. return -EIO;
  239. }
  240. /* tell the SCSI stack some characteristics of this adapter */
  241. adapter->scsi_host->max_id = 1;
  242. adapter->scsi_host->max_lun = 1;
  243. adapter->scsi_host->max_channel = 0;
  244. adapter->scsi_host->unique_id = dev_id.devno;
  245. adapter->scsi_host->max_cmd_len = 255;
  246. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  247. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  248. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  249. scsi_host_put(adapter->scsi_host);
  250. return -EIO;
  251. }
  252. return 0;
  253. }
  254. void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  255. {
  256. struct Scsi_Host *shost;
  257. struct zfcp_port *port;
  258. shost = adapter->scsi_host;
  259. if (!shost)
  260. return;
  261. read_lock_irq(&zfcp_data.config_lock);
  262. list_for_each_entry(port, &adapter->port_list_head, list)
  263. if (port->rport)
  264. port->rport = NULL;
  265. read_unlock_irq(&zfcp_data.config_lock);
  266. fc_remove_host(shost);
  267. scsi_remove_host(shost);
  268. scsi_host_put(shost);
  269. adapter->scsi_host = NULL;
  270. return;
  271. }
  272. static struct fc_host_statistics*
  273. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  274. {
  275. struct fc_host_statistics *fc_stats;
  276. if (!adapter->fc_stats) {
  277. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  278. if (!fc_stats)
  279. return NULL;
  280. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  281. }
  282. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  283. return adapter->fc_stats;
  284. }
  285. static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  286. struct fsf_qtcb_bottom_port *data,
  287. struct fsf_qtcb_bottom_port *old)
  288. {
  289. fc_stats->seconds_since_last_reset =
  290. data->seconds_since_last_reset - old->seconds_since_last_reset;
  291. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  292. fc_stats->tx_words = data->tx_words - old->tx_words;
  293. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  294. fc_stats->rx_words = data->rx_words - old->rx_words;
  295. fc_stats->lip_count = data->lip - old->lip;
  296. fc_stats->nos_count = data->nos - old->nos;
  297. fc_stats->error_frames = data->error_frames - old->error_frames;
  298. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  299. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  300. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  301. fc_stats->loss_of_signal_count =
  302. data->loss_of_signal - old->loss_of_signal;
  303. fc_stats->prim_seq_protocol_err_count =
  304. data->psp_error_counts - old->psp_error_counts;
  305. fc_stats->invalid_tx_word_count =
  306. data->invalid_tx_words - old->invalid_tx_words;
  307. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  308. fc_stats->fcp_input_requests =
  309. data->input_requests - old->input_requests;
  310. fc_stats->fcp_output_requests =
  311. data->output_requests - old->output_requests;
  312. fc_stats->fcp_control_requests =
  313. data->control_requests - old->control_requests;
  314. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  315. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  316. }
  317. static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  318. struct fsf_qtcb_bottom_port *data)
  319. {
  320. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  321. fc_stats->tx_frames = data->tx_frames;
  322. fc_stats->tx_words = data->tx_words;
  323. fc_stats->rx_frames = data->rx_frames;
  324. fc_stats->rx_words = data->rx_words;
  325. fc_stats->lip_count = data->lip;
  326. fc_stats->nos_count = data->nos;
  327. fc_stats->error_frames = data->error_frames;
  328. fc_stats->dumped_frames = data->dumped_frames;
  329. fc_stats->link_failure_count = data->link_failure;
  330. fc_stats->loss_of_sync_count = data->loss_of_sync;
  331. fc_stats->loss_of_signal_count = data->loss_of_signal;
  332. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  333. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  334. fc_stats->invalid_crc_count = data->invalid_crcs;
  335. fc_stats->fcp_input_requests = data->input_requests;
  336. fc_stats->fcp_output_requests = data->output_requests;
  337. fc_stats->fcp_control_requests = data->control_requests;
  338. fc_stats->fcp_input_megabytes = data->input_mb;
  339. fc_stats->fcp_output_megabytes = data->output_mb;
  340. }
  341. static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
  342. {
  343. struct zfcp_adapter *adapter;
  344. struct fc_host_statistics *fc_stats;
  345. struct fsf_qtcb_bottom_port *data;
  346. int ret;
  347. adapter = (struct zfcp_adapter *)host->hostdata[0];
  348. fc_stats = zfcp_init_fc_host_stats(adapter);
  349. if (!fc_stats)
  350. return NULL;
  351. data = kzalloc(sizeof(*data), GFP_KERNEL);
  352. if (!data)
  353. return NULL;
  354. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  355. if (ret) {
  356. kfree(data);
  357. return NULL;
  358. }
  359. if (adapter->stats_reset &&
  360. ((jiffies/HZ - adapter->stats_reset) <
  361. data->seconds_since_last_reset))
  362. zfcp_adjust_fc_host_stats(fc_stats, data,
  363. adapter->stats_reset_data);
  364. else
  365. zfcp_set_fc_host_stats(fc_stats, data);
  366. kfree(data);
  367. return fc_stats;
  368. }
  369. static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  370. {
  371. struct zfcp_adapter *adapter;
  372. struct fsf_qtcb_bottom_port *data;
  373. int ret;
  374. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  375. data = kzalloc(sizeof(*data), GFP_KERNEL);
  376. if (!data)
  377. return;
  378. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  379. if (ret)
  380. kfree(data);
  381. else {
  382. adapter->stats_reset = jiffies/HZ;
  383. kfree(adapter->stats_reset_data);
  384. adapter->stats_reset_data = data; /* finally freed in
  385. adapter_dequeue */
  386. }
  387. }
  388. static void zfcp_get_host_port_state(struct Scsi_Host *shost)
  389. {
  390. struct zfcp_adapter *adapter =
  391. (struct zfcp_adapter *)shost->hostdata[0];
  392. int status = atomic_read(&adapter->status);
  393. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  394. !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
  395. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  396. else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
  397. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  398. else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
  399. fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
  400. else
  401. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  402. }
  403. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  404. {
  405. rport->dev_loss_tmo = timeout;
  406. }
  407. struct fc_function_template zfcp_transport_functions = {
  408. .show_starget_port_id = 1,
  409. .show_starget_port_name = 1,
  410. .show_starget_node_name = 1,
  411. .show_rport_supported_classes = 1,
  412. .show_rport_maxframe_size = 1,
  413. .show_rport_dev_loss_tmo = 1,
  414. .show_host_node_name = 1,
  415. .show_host_port_name = 1,
  416. .show_host_permanent_port_name = 1,
  417. .show_host_supported_classes = 1,
  418. .show_host_supported_speeds = 1,
  419. .show_host_maxframe_size = 1,
  420. .show_host_serial_number = 1,
  421. .get_fc_host_stats = zfcp_get_fc_host_stats,
  422. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  423. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  424. .get_host_port_state = zfcp_get_host_port_state,
  425. .show_host_port_state = 1,
  426. /* no functions registered for following dynamic attributes but
  427. directly set by LLDD */
  428. .show_host_port_type = 1,
  429. .show_host_speed = 1,
  430. .show_host_port_id = 1,
  431. .disable_target_scan = 1,
  432. };
  433. struct zfcp_data zfcp_data = {
  434. .scsi_host_template = {
  435. .name = "zfcp",
  436. .module = THIS_MODULE,
  437. .proc_name = "zfcp",
  438. .slave_alloc = zfcp_scsi_slave_alloc,
  439. .slave_configure = zfcp_scsi_slave_configure,
  440. .slave_destroy = zfcp_scsi_slave_destroy,
  441. .queuecommand = zfcp_scsi_queuecommand,
  442. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  443. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  444. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  445. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  446. .can_queue = 4096,
  447. .this_id = -1,
  448. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  449. .cmd_per_lun = 1,
  450. .use_clustering = 1,
  451. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  452. .max_sectors = (ZFCP_MAX_SBALES_PER_REQ * 8),
  453. .shost_attrs = zfcp_sysfs_shost_attrs,
  454. },
  455. };